在C#中dagagridview绑定list泛型

时间:2023-03-08 21:23:33
在C#中dagagridview绑定list泛型

今天在项目中由于需要使用到datagridview绑定list的数据源,在针对list的添加、删除、修改都可以很好地完成,可是在初始化datagridview时,却发现了问题,绑定数据源后,并没有在列表中显示;最初代码是这样的:

this.DgvData.DataSource = null;
this.DgvData.ReadOnly = true;
this.DgvData.RowHeadersWidth = ;
this.DgvData.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.DgvData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.DgvData.AllowUserToAddRows = false;//去掉空白行
this.DgvData.AutoGenerateColumns = false;
this.DgvData.DataSource = list;

可是怎么调试,在网上也看了半天,也没发现什么好的解决方法.
最后在不断尝试后,至少稍稍做了下处理,就好呢.

 //this.DgvData.DataSource = null;
this.DgvData.ReadOnly = true;
this.DgvData.RowHeadersWidth = ;
this.DgvData.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.DgvData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.DgvData.AllowUserToAddRows = false;//去掉空白行
this.DgvData.AutoGenerateColumns = false;
//this.DgvData.DataSource = list;
this.DgvData.RowCount = list.Count;
for (int i = ; i < list.Count; i++)
{
DgvData[, i].Value = (i + ).ToString();
DgvData[, i].Value = list[i].Name;
DgvData[, i].Value = list[i].DataLength;
DgvData[, i].Value = list[i].Priority;
}

希望借此抛砖引入,大家碰到类似的问题有什么好的解决方案呢!