如何将列表绑定到dataGridView?

Amc*_*tty 49 .net c# binding datagridview

我似乎在圈子里跑来跑去,并且在过去的几个小时里一直这样做.

我想从一个字符串数组填充datagridview.我已经读过它不可能直接了,我需要创建一个将字符串保存为公共属性的自定义类型.所以我上了一堂课:

public class FileName
    {
        private string _value;

        public FileName(string pValue)
        {
            _value = pValue;
        }

        public string Value
        {
            get 
            {
                return _value;
            }
            set { _value = value; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是容器类,它只是一个具有字符串值的属性.我现在想要的是当我将其数据源绑定到List时,该字符串将出现在datagridview中.

我也有这个方法,BindGrid(),我想填充datagridview.这里是:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

最后,问题是:字符串数组填充正常,列表创建正常,但我在datagridview中得到一个空列.我也直接尝试了datasource = list <>,而不是= bindingsource,仍然没有.

我真的很感激你的建议,这让我很疯狂.

谢谢

slo*_*oth 72

使用BindingList并设置列的DataPropertyName -Property.

请尝试以下方法:

...
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 
}
Run Code Online (Sandbox Code Playgroud)

  • 当您真正需要BindingSource的功能时,您应该使用BindingSource.BindingSource的一个常见用途是当用户必须在表单上执行某些输入时,在这种情况下,您可以立即将BindingSource绑定到多个控件.BindingSource可以做更多的事情,但是当你实际上不需要它时,使用像BindingList这样的更简单的方法. (2认同)

Nas*_*ood 12

可能会迟到但对未来有用.如果您不需要设置单元格的自定义属性,只关注标题文本和单元格值,那么此代码将帮助您

public class FileName
{        
     [DisplayName("File Name")] 
     public string FileName {get;set;}
     [DisplayName("Value")] 
     public string Value {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

然后你可以将List绑定为数据源

private void BindGrid()
{
    var filelist = GetFileListOnWebServer().ToList();    
    gvFilesOnServer.DataSource = filelist.ToArray();
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,您可以访问此页面将Class对象的列表绑定为Datasource到DataGridView

希望这会帮助你.


Kev*_*nck 7

我知道这已经老了,但是这让我感到很沮丧.列表中对象的属性必须是实际的"属性",而不仅仅是公共成员.

public class FileName
{        
     public string ThisFieldWorks {get;set;}
     public string ThisFieldDoesNot;
}
Run Code Online (Sandbox Code Playgroud)

  • 您刚刚为我节省了数小时的调试时间。谢谢你! (3认同)