动态创建列和行
DataGridView dataGridView2 = new DataGridView();
dataGridView2.Dock = DockStyle.Fill;
//动态添加列
DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
dataGridViewTextBoxColumn.HeaderText = "索引";
DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn = new DataGridViewCheckBoxColumn();
dataGridViewCheckBoxColumn.HeaderText = "变量";
DataGridViewButtonColumn dataGridViewButtonColumn = new DataGridViewButtonColumn();
dataGridViewButtonColumn.HeaderText = "参数";
DataGridViewComboBoxColumn dataGridViewComboBoxColumn = new DataGridViewComboBoxColumn();
dataGridViewComboBoxColumn.HeaderText = "别名";
DataGridViewImageColumn image = new DataGridViewImageColumn();
image.HeaderText = "别称";
DataGridViewLinkColumn link = new DataGridViewLinkColumn();
link.HeaderText = "雅号";
dataGridView2.Columns.Add(dataGridViewTextBoxColumn);
dataGridView2.Columns.Add(dataGridViewCheckBoxColumn);
dataGridView2.Columns.Add(dataGridViewButtonColumn);
dataGridView2.Columns.Add(dataGridViewComboBoxColumn);
dataGridView2.Columns.Add(image);
dataGridView2.Columns.Add(link);
dataGridView2.Parent = tabPage3;//把dataGridView2放到tabPage3上
//动态添加行
DataGridViewRow row = new DataGridViewRow();//创建行对象
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = "1";
row.Cells.Add(textboxcell);
DataGridViewCheckBoxCell checkBoxCell = new DataGridViewCheckBoxCell();
checkBoxCell.Value = true;
row.Cells.Add(checkBoxCell);
DataGridViewButtonCell buttonCell = new DataGridViewButtonCell();
buttonCell.Value = "浏览"; //添加button没有事件,有什么用啊!
row.Cells.Add(buttonCell);
DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();
comboBoxCell.Items.Add("123");
comboBoxCell.Items.Add("456");
comboBoxCell.Value = "123";//必须是前边添加了
row.Cells.Add(comboBoxCell);
DataGridViewImageCell imageCell = new DataGridViewImageCell();
Image imagetest = Image.FromFile("D:\\vsproject\\csharpproject\\WindowsFormsApplication\\FourTab\\hilworks\\instrment.png");
imageCell.Value = imagetest;
row.Cells.Add(imageCell);
DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
linkCell.Value = "www.baidu.com";
row.Cells.Add(linkCell);
dataGridView2.Rows.Add(row);
// dataGridView2.ReadOnly = true;//取消编辑 或如下图:
也可以手动创建完了列再创建行
//方法一
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = "1";
this.dataGridView1.Rows[index].Cells[1].Value = "糖醋排骨";
this.dataGridView1.Rows[index].Cells[2].Value = true;
// this.dataGridView1[0, 3].Value = "new value";
// this.dataGridView1.Rows[index].Cells[3].Value = "test";
//方法2
DataGridViewRow row = new DataGridViewRow();//创建行对象
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = "aaa";
row.Cells.Add(textboxcell);
DataGridViewButtonCell buttonCell = new DataGridViewButtonCell();
buttonCell.Value = "浏览"; //添加button没有事件,有什么用啊!
row.Cells.Add(buttonCell);
DataGridViewImageCell imageCell = new DataGridViewImageCell();
Image imagetest = Image.FromFile("D:\\vsproject\\csharpproject\\WindowsFormsApplication\\FourTab\\hilworks\\instrment.png");
imageCell.Value = imagetest;
row.Cells.Add(imageCell);
dataGridView1.Rows.Add(row);
/* 方法二我该了,现编注释对不上
方法二比方法一要复杂一些,但是在一些特殊场合非常实用
* 例如,要在新行中的某些单元格添加下拉框、按钮之类的控件时,
* 该方法很有帮助。DataGridViewRow row = new DataGridViewRow();是创建DataGridView的行对象
* DataGridViewTextBoxCell是单元格的内容是个TextBox,
* DataGridViewComboBoxCell是单元格的内容是下拉列表框,
* 同理可知,DataGridViewButtonCell是单元格的内容是个按钮,等等。
* textboxcell是新创建的单元格的对象,可以为该对象添加其属性。
* 然后通过row.Cells.Add(textboxcell)为row对象添加textboxcell单元格。
* 要添加其他的单元格,用同样的方法即可。
* 最后通过dataGridView1.Rows.Add(row)为dataGridView1控件添加新的行row。
*/
总是多一行可以使用属性
AllowUserToAddRows 设成false就没用了
{
if (dataGridView1.Rows[1].Cells[1].Selected)
{
MessageBox.Show("dfddddddddddddsgertfg");
}
}
{
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
this.dataGridView1.Columns[i].Width = dataGridView1.Width / dataGridView1.ColumnCount;
}
}