如何将列表绑定到

如何将列表绑定到

本文介绍了如何将列表绑定到 dataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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; }
        }
    }

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

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.

我还有这个方法 BindGrid(),我想用它来填充 datagridview.这是:

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;
    }

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

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.

推荐答案

使用 BindingList 并设置 DataPropertyName-列的属性.

Use a BindingList and set the DataPropertyName-Property of the column.

尝试以下操作:

...
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
}

这篇关于如何将列表绑定到 dataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:06