How to associate a list with dataGridView?

I seem to be running in circles and have been doing this in the last hours.

I want to populate a data grid from an array of strings. I read that this is not possible directly, and that I need to create a custom type that contains a string as an open property. So, I made a class:

public class FileName { private string _value; public FileName(string pValue) { _value = pValue; } public string Value { get { return _value; } set { _value = value; } } } 

it is a container class, and it just has a property with a string value. All I want now is for this row to appear in the data grid when I link its data source to the list.

I also have this method, BindGrid (), with which I want to populate the data view. Here:

  private void BindGrid() { gvFilesOnServer.AutoGenerateColumns = false; //create the column programatically DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn(); DataGridViewCell cell = new DataGridViewTextBoxCell(); colFileName.CellTemplate = cell; colFileName.Name = "Value"; colFileName.HeaderText = "File Name"; colFileName.ValueType = typeof(FileName); //add the column to the datagridview gvFilesOnServer.Columns.Add(colFileName); //fill the string array string[] filelist = GetFileListOnWebServer(); //try making a List<FileName> from that array List<FileName> filenamesList = new List<FileName>(filelist.Length); for (int i = 0; i < filelist.Length; i++) { filenamesList.Add(new FileName(filelist[i].ToString())); } //try making a bindingsource BindingSource bs = new BindingSource(); bs.DataSource = typeof(FileName); foreach (FileName fn in filenamesList) { bs.Add(fn); } gvFilesOnServer.DataSource = bs; } 

Finally, the problem: the array of rows fills fine, the list is created fine, but I get an empty column in the data view. I also tried datasource = List <> directly and not = bindingsource, but nothing worked.

I would really appreciate advice, it drives me crazy.

+49
c # binding datagridview
Aug 04 '09 at 16:27
source share
5 answers

Use a BindingList and set the DataPropertyName -Property column.

Try the following:

 ... private void BindGrid() { gvFilesOnServer.AutoGenerateColumns = false; //create the column programatically DataGridViewCell cell = new DataGridViewTextBoxCell(); DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn() { CellTemplate = cell, Name = "Value", HeaderText = "File Name", DataPropertyName = "Value" // Tell the column which property of FileName it should use }; gvFilesOnServer.Columns.Add(colFileName); var filelist = GetFileListOnWebServer().ToList(); var filenamesList = new BindingList<FileName>(filelist); // <-- BindingList //Bind BindingList directly to the DataGrid, no need of BindingSource gvFilesOnServer.DataSource = filenamesList } 
+72
Aug 04 '09 at 16:56
source share
— -

may be a bit late, but useful for the future. if you do not need to set custom cell properties and take into account only the header text and cell value, then this code will help you

 public class FileName { [DisplayName("File Name")] public string FileName {get;set;} [DisplayName("Value")] public string Value {get;set;} } 

and then you can bind List as a data source as

 private void BindGrid() { var filelist = GetFileListOnWebServer().ToList(); gvFilesOnServer.DataSource = filelist.ToArray(); } 

for more information, you can visit this page. Bind a list of class objects as a Datasource to a DataGridView

Hope this helps you.

+11
Dec 21 '13 at 5:30
source share

I know this is old, but it delayed me a bit. The properties of an object in your list must be valid "properties", not just public members.

 public class FileName { public string ThisFieldWorks {get;set;} public string ThisFieldDoesNot; } 
+7
Nov 18 '15 at 19:02
source share

Instead of creating a new container class, you can use dataTable.

 DataTable dt = new DataTable(); dt.Columns.Add("My first column Name"); dt.Rows.Add(new object[] { "Item 1" }); dt.Rows.Add(new object[] { "Item number 2" }); dt.Rows.Add(new object[] { "Item number three" }); myDataGridView.DataSource = dt; 

You can find more about this problem here: http://psworld.pl/Programming/BindingListOfString

+3
Oct 18 '12 at 16:28
source share

Use of a DataTable acts as directed by user927524. You can also do this by adding lines manually, which does not require adding a specific packaging class:

 List<string> filenamesList = ...; foreach(string filename in filenamesList) gvFilesOnServer.Rows.Add(new object[]{filename}); 

Anyway, thanks to user927524 for fixing this weird behavior!

+1
Sep 17 '15 at 21:22
source share



All Articles