Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Populating data in multiple columns using WinForms ListView

$
0
0

I would like to share a brief on how to bind data in multiple columns using ListView in WinForms. It is preferable to go for a ListView rather than a ListBox in Winforms to bind multiple columns. Below is the little tricky code to do so:

// Adding ListView Columns
lstView.Columns.Add("Id", 245, HorizontalAlignment.Left);
lstView.Columns.Add("Name", 241, HorizontalAlignment.Left);
lstView.Columns.Add("Mobile Number", 245, HorizontalAlignment.Left);// Getting the data to a datasource
DataTable dt = businessComponent.GetData(Id);string[] Str = newstring[3];
ListViewItem newItm;foreach (DataRow dataRow in dt.Rows)
{
    Str[0] = dataRow["Id"].ToString();
    Str[1] = dataRow["Name"].ToString();
    Str[2] = dataRow["Mobile"].ToString();
    newItm = new ListViewItem(Str);
    lstView.Items.Add(newItm);
}

 

To show the data in Details form (tabular form) we need to set ‘View’ property of the ListView to Details. By default it is set to LargeIcon out of the 5 values - LargeIcon, Details, SmallIcon, List, Tile.

We can do many more customizations for ListView, like setting the width of the columns, colors, fonts, etc with ListView as it is designed to display the data.

-Mohammed Ghouse Ibne Barq Kadapavi
http://www.ProgrammersVision.blogspot.com
https://sites.google.com/site/BarqKadapavi

 


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>