代码之家  ›  专栏  ›  技术社区  ›  Amc_rtty

如何将列表绑定到DataGridView?

  •  49
  • Amc_rtty  · 技术社区  · 15 年前

    我好像在绕圈子跑,在过去的几个小时里一直在这样做。

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

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

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

    还有这个方法binggrid(),我想用它填充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;
        }
    

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

    我真的很感激你的建议,这让我发疯了。

    谢谢你

    5 回复  |  直到 7 年前
        1
  •  71
  •   sloth    12 年前

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

    尝试以下操作:

    ...
    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 
    }
    
        2
  •  11
  •   Nasir Mahmood    7 年前

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

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

    然后可以将列表绑定为数据源

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

    有关详细信息,请访问此页面 Bind List of Class objects as Datasource to DataGridView

    希望这对你有帮助。

        3
  •  7
  •   Kevin Finck    9 年前

    我知道这是旧的,但这把我挂了一段时间。列表中对象的属性必须是实际的“属性”,而不仅仅是公共成员。

    public class FileName
    {        
         public string ThisFieldWorks {get;set;}
         public string ThisFieldDoesNot;
    }
    
        4
  •  3
  •   PsCraft    12 年前

    您可以使用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;
    

    有关此问题的详细信息,请参见: http://psworld.pl/Programming/BindingListOfString

        5
  •  1
  •   user3214451    9 年前

    如用户927524所述,使用数据表是有效的。 您也可以通过手动添加行来完成此操作,这不需要添加特定的包装类:

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

    无论如何,感谢用户927524清除这种奇怪的行为!!