自动添加行列
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewColumnCollection SS = this.dataGridView1.Columns;
SS.Add("第一列", "姓名");
SS.Add("第二列", "地址");
SS.Add("第三列", "男女");
SS.Add("第四列", "电话");
}
窗体加载的时候,自动生成几列
这个不是我的图片是网上的案例的结果(方便理解用的,第一列 第二次 就是name headertext是姓名 地址 )
2 如果需要修改表格名字
如下:
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewColumnCollection SS = this.dataGridView1.Columns;
SS.Add("第一列", "姓名");
SS.Add("第二列", "地址");
SS.Add("第三列", "男女");
SS.Add("第四列", "电话");
this.dataGridView1.Columns[0].HeaderCell.Value = "姓名";
this.dataGridView1.Columns[1].HeaderCell.Value = "字段3";
this.dataGridView1.Columns[2].HeaderCell.Value = "字段4";
this.dataGridView1.Columns[3].HeaderCell.Value = "字段5";
}
代码自动生成行数
DataGridViewColumnCollection SS = this.dataGridView1.Columns;
SS.Add("第一列", "姓名");
SS.Add("第二列", "地址");
SS.Add("第三列", "男女");
SS.Add("第四列", "电话");
this.dataGridView1.Rows.Add(5)
DataGridViewColumnCollection SS = this.dataGridView1.Columns;
SS.Add("第一列", "姓名");
SS.Add("第二列", "地址");
SS.Add("第三列", "男女");
SS.Add("第四列", "电话");
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = "1";
this.dataGridView1.Rows[index].Cells[1].Value = "2";
this.dataGridView1.Rows[index].Cells[2].Value = "监听";
this.dataGridView1.Rows[index].Cells[3].Value = "151234567";
利用dataGridView1.Rows.Add()事件为DataGridView控件增加新的行,该函数返回添加新行的索引号,即新行的行号,然后可以通过该索引号操作该行的各个单元格,如dataGridView1.Rows[index].Cells[0].Value = “1”。这是很常用也是很简单的方法。
DataGridViewColumnCollection SS = this.dataGridView1.Columns;
SS.Add("第一列", "姓名");
SS.Add("第二列", "地址");
SS.Add("第三列", "男女");
SS.Add("第四列", "电话");
DataGridViewRow row = new DataGridViewRow();
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = "aaa";
row.Cells.Add(textboxcell);
DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();
row.Cells.Add(comboxcell);
DataGridViewTextBoxCell textboxcel22 = new DataGridViewTextBoxCell();
textboxcel22.Value = "aa1a";
row.Cells.Add(textboxcel22);
dataGridView1.Rows.Add(row);
DataGridView 取得或者修改当前单元格的内容:
当前单元格指的是 DataGridView 焦点所在的单元格,它可以通过 DataGridView 对象的 CurrentCell 属性取得。如果当前单元格不存在的时候,返回null
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewColumnCollection SS = this.dataGridView1.Columns;
SS.Add("第一列", "姓名");
SS.Add("第二列", "地址");
SS.Add("第三列", "男女");
SS.Add("第四列", "电话");
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = "1";
this.dataGridView1.Rows[index].Cells[1].Value = "2";
this.dataGridView1.Rows[index].Cells[2].Value = "nan";
this.dataGridView1.Rows[index].Cells[3].Value = "151234567";
int index1 = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index1].Cells[0].Value = "1";
this.dataGridView1.Rows[index1].Cells[1].Value = "2";
this.dataGridView1.Rows[index1].Cells[2].Value = "nv";
this.dataGridView1.Rows[index1].Cells[3].Value = "151234567";
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show(this.dataGridView1.CurrentCell.Value.ToString());
}
// 取得当前单元格的列 Index
Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex);
// 取得当前单元格的行 Index
Console.WriteLine(DataGridView1.CurrentCell.RowIndex);
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewColumnCollection SS = this.dataGridView1.Columns;
SS.Add("第一列", "姓名");
SS.Add("第二列", "地址");
SS.Add("第三列", "男女");
SS.Add("第四列", "电话");
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = "1";
this.dataGridView1.Rows[index].Cells[1].Value = "2";
this.dataGridView1.Rows[index].Cells[2].Value = "nan";
this.dataGridView1.Rows[index].Cells[3].Value = "151234567";
int index1 = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index1].Cells[0].Value = "1";
this.dataGridView1.Rows[index1].Cells[1].Value = "2";
this.dataGridView1.Rows[index1].Cells[2].Value = "nv";
this.dataGridView1.Rows[index1].Cells[3].Value = "151234567";
}
private void button3_Click(object sender, EventArgs e)
{
// 取得当前单元格的列 Index
//Console.WriteLine(dataGridView1.CurrentCell.ColumnIndex);
MessageBox.Show("列"+dataGridView1.CurrentCell.ColumnIndex.ToString()+"行"+dataGridView1.CurrentCell.RowIndex.ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewColumnCollection SS = this.dataGridView1.Columns;
SS.Add("第一列", "数1字");
SS.Add("第二列", "数2字");
SS.Add("第三列", "数3字");
SS.Add("第四列", "数4字");
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = "1";
this.dataGridView1.Rows[index].Cells[1].Value = "2";
this.dataGridView1.Rows[index].Cells[2].Value = "5";
this.dataGridView1.Rows[index].Cells[3].Value = "0";
int index1 = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index1].Cells[0].Value = "1";
this.dataGridView1.Rows[index1].Cells[1].Value = "2";
this.dataGridView1.Rows[index1].Cells[2].Value = "7";
this.dataGridView1.Rows[index1].Cells[3].Value = "1";
}
private void button3_Click(object sender, EventArgs e)
{
int cc = this.dataGridView1.CurrentCell.RowIndex;
int bb= dataGridView1.Rows[cc].Cells.Count;
int sum = 0;
for (int i = 0; i < bb; i++)
{
var tt = dataGridView1.Rows[cc].Cells[i].Value;
int yy = Convert.ToInt32(tt );
if (yy!=null)
{
sum = sum + yy;
}
else
{
MessageBox.Show("vvvvvv");
}
}
MessageBox.Show(sum.ToString());
例如获得第一行第二列的值
dataGridView1.Rows[0].Cells[1].value