Step 1: Create a window application
Step 2: Add a Listview(lsvUserDetails) and Button(btnShow)
Add three columns and name it as
i)No
ii)Name
iii)Department
Modify Listview Properties
i) Change the view to Details
ii)Change GridLines to True
Step 3:Create the datatable(dtUserDetails) globally as below
DataTable dtUserDetails = new DataTable();
Step 4: Write a method CreateDatatable
public void CreateDatatable()
{
//Create
a datatable and add three columns
dtUserDetails.Columns.Add("No", typeof(int));
dtUserDetails.Columns.Add("Name", typeof(string));
dtUserDetails.Columns.Add("Department", typeof(string));
//Add
values to the datatable
DataRow
drow1 = dtUserDetails.NewRow();
drow1["No"]
= 1;
drow1["Name"]
= "Vivekanandan";
drow1["Department"]
= "Software Engineer";
DataRow
drow2 = dtUserDetails.NewRow();
drow2["No"]
= 2;
drow2["Name"]
= "Ismail";
drow2["Department"]
= "Software Engineer";
DataRow
drow3 = dtUserDetails.NewRow();
drow3["No"]
= 3;
drow3["Name"]
= "Velmurugan";
drow3["Department"]
= "Team Leader";
dtUserDetails.Rows.Add(drow1);
dtUserDetails.Rows.Add(drow2);
dtUserDetails.Rows.Add(drow3);
}
Step 5: Write a custom class (clsLsvUserDetails)
private class clsLsvUserDetails
: ListViewItem
{
private
DataRow _drData = null;
public
clsLsvUserDetails(DataRow dr): base()
{
_drData = dr;
this.Text
= _drData["No"].ToString();
this.SubItems.Add(_drData["Name"].ToString());
this.SubItems.Add(_drData["Department"].ToString());
}
}
Step 6: On button click event(btnShow_Click) write the below code
private void btnShow_Click(object
sender, EventArgs e)
{
CreateDatatable();
lsvUserDetails.Items.Clear();
clsLsvUserDetails
oItem = null;
if
(dtUserDetails != null)
{
foreach
(DataRow dr in
dtUserDetails.Rows)
{
oItem = new clsLsvUserDetails(dr);
lsvUserDetails.Items.Add(oItem);
}
}
}