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.
Amc_rtty Aug 04 '09 at 16:27 2009-08-04 16:27
source share