views:

1606

answers:

2

Hi everyone, I seem to be running around in circles and have been doing so in the last hours.

I want to populate a datagridview from an array of strings. I've read its not possible directly, and that I need to create a custom type that holds the string as a public 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; }
        }
    }

this is the container class, and it simply has a property with the value of the string. All I want now is that string to appear in the datagridview, when I bind its datasource to a List.

Also I have this method, BindGrid() which I want to fill the datagridview with. Here it is:

    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 string array fills ok, the list is created ok, but I get an empty column in the datagridview. I also tried datasource= list<> directly, instead of = bindingsource, still nothing.

I would really appreciate an advice, this has been driving me crazy.

Thank you

+2  A: 

Use a BindingList and set the DataPropertyName-Property of the coloum

Try the following:

...
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";

    // Tell the column which property of FileName it should use
    colFileName.DataPropertyName = "Value"; 

    gvFilesOnServer.Columns.Add(colFileName);

    string[] filelist = GetFileListOnWebServer();
    var filenamesList = new BindingList<FileName>(); // <-- BindingList

    for (int i = 0; i < filelist.Length; i++)
        filenamesList.Add(new FileName(filelist[i]));

    //Bind BindingList dirctly to the DataGrid, no need of BindingSource
    gvFilesOnServer.DataSource=filenamesList 
}
dkson
Dkson you have saved me; this worked. How could I not see the "DataPropertyName" of the column? I feel bad for asking such a simple thing, I should have seen it. The only question I still have is when I should actually make use of a BindingSource, as things seem to work without it; but thats what I'll find out in time, with practice.
Andrei
You should usea BindingSource when you really need the functionalityof a BindingSource. A common use of the BindingSource is when the user has to do some inputs on a form in which case you can bind the BindingSource to multiple Controls at once. There's a lot more a BindingSource can do, but when you actually don't need it, use a simpler approach like BindingList.
dkson
A: 

@dkson: Thank you, this saved me from some agonising coding session using a BindingSource. I was trying to add items from a Lookup form, which are added to a DataGridView bound to a BindingSource on a parent Form. - Kodeman

Kodeman