Friday, January 20, 2012

Delete Multiple Rows in .Net Development Through WPF 4 Data Grid

WPF4 Data Grid is used for the .net development application which in turn is used in many other implementation works.  Data Manipulations also form a part along with the Data Representation when the DataGrid is used in the .net development application. Assigning of templates for columns is made possible with the use of the various elements of DataGrid that can beused by offshore .net development team.

Define the classes after the creation of the WPF application



2. In the .XAML window define the XAML code

<Window x:Class="WPF_Application.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow DeleteMultipleRows" Height="426" Width="740"
         Loaded="Window_Loaded">
   
<Grid>
   <TextBlock Height="51" HorizontalAlignment="Left" Margin="37,12,0,0"
       Name="textBlock1" Width="648"
              Text="Employee Information System" VerticalAlignment="Top"
       TextAlignment="Center" FontSize="26" />
   <DataGrid AutoGenerateColumns="False" Height="240"
HorizontalAlignment="Left" Width="648"
              Margin="37,69,0,0" Name="dgEmp" VerticalAlignment="Top" 
              ColumnWidth="*" RowEditEnding="dgEmp_RowEditEnding" >
   <DataGrid.Columns>
   <DataGridTextColumn Binding="{Binding EmpNo}" Header="EmpNo" />
   <DataGridTextColumn Binding="{Binding EmpName}" Header="EmpName" />
<DataGridTextColumn Binding="{Binding Designation}" Header="Desig" />
   <DataGridTextColumn Binding="{Binding Salary}" Header="Salary" />
   <DataGridCheckBoxColumn Header="Delete"></DataGridCheckBoxColumn>
   </DataGrid.Columns>
   </DataGrid>
   <Button Content="Delete Rows" Height="23" HorizontalAlignment="Left" 
Margin="37,327,0,0"
                Name="btnDeleteRows" VerticalAlignment="Top" Width="190"
Click="btnDeleteRows_Click" />
</Grid>
</Window>

The XAML code will bind together the employee class properties and the data grid is individually defined. The offshore .net development team will be able to define the DataGrid CheckBox column through DataGrid and this will be in turn used to delete the rows out of the DataGrid. XAML code will also be used for creating a btnDelete that will complete the delete operation.

3. The mainwindow.XAML.cs is opened and the following code will be written in it

List<int> lstSelectedEmpNo;
EmployeeCollection EmpCollection = new EmployeeCollection();
In the loaded event, write the following code:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
dgEmp.ItemsSource = EmpCollection;
lstSelectedEmpNo = new List<int>();
}

The code shown above binds the Employee collection to the DataGrid.

Write the following code in the RowEditing event:

private void dgEmp_RowEditEnding(object sender,
DataGridRowEditEndingEventArgs e)
{
FrameworkElement element = dgEmp.Columns[4].GetCellContent(e.Row);
if (element.GetType() == typeof(CheckBox))
{
if (((CheckBox)element).IsChecked == true)
{
FrameworkElement cellEmpNo =
dgEmp.Columns[0].GetCellContent(e.Row);
int EmpNo = Convert.ToInt32(((TextBlock)cellEmpNo).Text);
lstSelectedEmpNo.Add(EmpNo);
}
}
}

To delete the rows the following code will be written   

private void btnDeleteRows_Click(object sender, RoutedEventArgs e)
{
try
{
if(lstSelectedEmpNo.Count>0)
{
int count=0;
foreach (int eno in lstSelectedEmpNo)
{
Employee emp = (from ep in EmpCollection
where ep.EmpNo == eno
select ep).First();
EmpCollection.Remove(emp);
count++;
}
MessageBox.Show(count + "Row's Deleted" );
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Looking for a Developer with strong Data Analysis and BI skills? For Microsoft Business Intelligence Software Development, Contact Us for BI Software Development.