Winform自定义dataGridView
作者:互联网
dataGridView中的自定义表格属性:
{
width:控件宽度
HeaderText:头列名,显示在表的最顶行的文字。
Name:列名,通过Name找到这个列的代号。
}
在这里默认定义成:DataGridViewColumn checkColumn = new DataGridViewColumn();时,会报错“该实例未xxxxx”,主要是因为没有定义成具体的实例,所以运行会报错,但编译不会。
定义完,并且设置完属性还要把dgCabinetView.Columns.Add()一个个加进去,就能在dataGridView中显示出这个表格。
public DataGridView CreateDataGridViewColunm()
{
DataGridViewColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Width = 100;
checkColumn.HeaderText = "";
checkColumn.Name = "check";
DataGridViewColumn numColumn = new DataGridViewTextBoxColumn();
numColumn.Width = 100;
numColumn.HeaderText = "";
numColumn.Name = "cabinetNum";
DataGridViewColumn nameColumn = new DataGridViewTextBoxColumn();
nameColumn.Width = 100;
nameColumn.HeaderText = "";
nameColumn.Name = "cabinetName";
DataGridViewColumn floorColunm = new DataGridViewTextBoxColumn();
floorColunm.Width = 100;
floorColunm.HeaderText = "";
floorColunm.Name = "cabinetFloor";
DataGridViewColumn aisleColunm = new DataGridViewTextBoxColumn();
aisleColunm.Width = 100;
aisleColunm.HeaderText = "";
aisleColunm.Name = "cabinetAisle";
DataGridViewColumn editColumn = new DataGridViewButtonColumn();
editColumn.Width = 100;
editColumn.HeaderText = "";
editColumn.Name = "edit";
DataGridViewColumn deleteColumn = new DataGridViewButtonColumn();
deleteColumn.Width = 100;
deleteColumn.HeaderText = "";
deleteColumn.Name = "delete";
this.dgCabinetView.Columns.Add(checkColumn);
this.dgCabinetView.Columns.Add(numColumn);
this.dgCabinetView.Columns.Add(nameColumn);
this.dgCabinetView.Columns.Add(floorColunm);
this.dgCabinetView.Columns.Add(aisleColunm);
this.dgCabinetView.Columns.Add(editColumn);
this.dgCabinetView.Columns.Add(deleteColumn);
return this.dgCabinetView;
}
当然这里定义完表后,不要忘记往里面加数据。
当时,我自己创建的时候,本来是直接在dataGridView中添加列名,想着能不能直接插进数据。再定义完列名后,往里面加数据就报错 “该dataGridView为null”,就打消了我直接使用已创建好的列名来添加数据。
就有了自定义表格列,再往里面加数据。
DataGridViewRow row = new DataGridViewRow();这里是新建一行,如果使用的是在dataGirdView中创建好的行,在新建列添加数据就会报 “该dataGridView为null”的错。
所以就需要自己添加一行的并且带上数据:
DataGridViewRow row = new DataGridViewRow();
DataGridViewCheckBoxCell check = new DataGridViewCheckBoxCell();
check.FlatStyle = FlatStyle.System;
DataGridViewTextBoxCell tbCabinetNum = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell tbCabinetName = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell tbCabinetFloor = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell tbCabinetAisle = new DataGridViewTextBoxCell();
DataGridViewButtonCell buttonEdit = new DataGridViewButtonCell();
DataGridViewButtonCell buttonDelete = new DataGridViewButtonCell();
tbCabinetNum.Value = cabinetList[i].Num; // 获取到当前柜子NUM
tbCabinetName.Value = cabinetList[i].Name; // 获取到当前柜子NAME
tbCabinetFloor.Value = cabinetList[i].Floor; // 获取到当前柜子FLOOR
tbCabinetAisle.Value = cabinetList[i].Aisle; // 获取到当前柜子AIEL
buttonEdit.Value = "编辑"; // 这里得设置button的值,才能在每一行都显示
buttonDelete.Value = "删除";// 同上
row.Cells.Add(check);
row.Cells.Add(tbCabinetNum);
row.Cells.Add(tbCabinetName);
row.Cells.Add(tbCabinetFloor);
row.Cells.Add(tbCabinetAisle);
row.Cells.Add(buttonEdit);
row.Cells.Add(buttonDelete);
//this.dgCabinetView.Rows.Add(row); // 插入作为最后一行显示
this.dgCabinetView.Rows.Insert(0, row); // 插入作为第一行显示
因为这个地方有个列表,所以这采用循环,显示数据。
标签:DataGridViewColumn,Name,自定义,dataGridView,Add,dgCabinetView,new,Winform,row 来源: https://www.cnblogs.com/wen-chen/p/16607663.html