1、嵌入Combox
DataGridViewComboBoxColumn cmbColumn = new DataGridViewComboBoxColumn();cmbColumn.Name = \"Type\";cmbColumn.HeaderText = \"Combox\";cmbColumn.Items.Add(\"Item0\");cmbColumn.Items.Add(\"Item1\");cmbColumn.Items.Add(\"Item2\");cmbColumn.Items.Add(\"Item3\");DataGridView1.Columns.Add(cmbColumn);
数据取值
获取第1行的数据
string strValue = DataGridView1.Rows[1].Cell[0].Value.ToString().Trim();
给第1行赋值
//值必须是Item0,Item1,Item2,Item3其中的一个,因为上面已经定义它的值。读取文件来显示时要//注意编码问题string strValue = \"Items0\";DataGridView1.Rows[1].Cell[0].Value = \"Item0\";
2、嵌入CheckBox
DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();checkboxColumn.Name = \"Flag\";checkboxColumn.HeaderText = \"CheckBox\";DataGridView1.Columns.Add(checkboxColumn);
获取第1行的数据
-
字符串类型
string strValue = DataGridView1.Rows[1].Cell[0].Value.ToString();
执行完之后strValue的值就变成\”True\”或\”False\”
-
布尔类型
bool boolValue = (bool)DataGridView1.Rows[1].Cell[0].Value;
执行完之后boolValue的值就是true或false
-
整型
int i = (int)DataGridView1.Rows[1].Cell[0].Value;
-
执行完之后出错
3百分比宽度调整
DataGridView的属性值AutoSizeColumnMode设置为Fill
DataGridView1.Columns[0].FillWeight = 10;DataGridView1.Columns[1].FillWeight = 10;DataGridView1.Columns[2].FillWeight = 5;DataGridView1.Columns[3].FillWeight = 5;DataGridView1.Columns[4].FillWeight = 10;DataGridView1.Columns[5].FillWeight = 20;DataGridView1.Columns[6].FillWeight = 20;DataGridView1.Columns[7].FillWeight = 20;
数值为百分比;控件高度占掉窗体的85%,宽度自适应:
private void frmMain_Load(object sender, EventArgs e){DataGridView1.Dock = DockStyle.Bottom;}private void Form1_Resize(object sender, EventArgs e){DataGridView1.Columns[0].FillWeight = 10;DataGridView1.Columns[1].FillWeight = 10;DataGridView1.Columns[2].FillWeight = 5;DataGridView1.Columns[3].FillWeight = 5;DataGridView1.Columns[4].FillWeight = 10;DataGridView1.Columns[5].FillWeight = 20;DataGridView1.Columns[6].FillWeight = 20;DataGridView1.Columns[7].FillWeight = 20;DataGridView1.Height = This.Height * 85 / 100;}