今天在项目中由于需要使用到datagridview绑定list的数据源,在针对list的添加、删除、修改都可以很好地完成,可是在初始化datagridview时,却发现了问题,绑定数据源后,并没有在列表中显示;最初代码是这样的:
this.DgvData.DataSource = null; this.DgvData.ReadOnly = true; this.DgvData.RowHeadersWidth = 22; 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 = 22; 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 = 0; i < list.Count; i++) { DgvData[0, i].Value = (i + 1).ToString(); DgvData[1, i].Value = list[i].Name; DgvData[2, i].Value = list[i].DataLength; DgvData[3, i].Value = list[i].Priority; }
希望借此抛砖引入,大家碰到类似的问题有什么好的解决方案呢!