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"
        Title="MainWindow DeleteMultipleRows" Height="426" Width="740"
   <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" >
   <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>
   <Button Content="Delete Rows" Height="23" HorizontalAlignment="Left" 
                Name="btnDeleteRows" VerticalAlignment="Top" Width="190"
Click="btnDeleteRows_Click" />

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 =
int EmpNo = Convert.ToInt32(((TextBlock)cellEmpNo).Text);

To delete the rows the following code will be written   

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