C#中DataGridView的一些基本操作
插入列:
dataGridView1.Columns.Add("Column1", "Column1"); dataGridView1.Columns.Add("Column2", "Column2");
插入行
int i=dataGridView1.Rows.Add(); dataGridView1.Rows[i].Cells[0].Value = "A"; dataGridView1.Rows[i].Cells[1].Value = "B";
清空数据
dataGridView1.Rows.Clear(); dataGridView1.Columns.Clear();
定位到数据的末端的两个方法
方法一:
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
方法二:
int u = dataGridView1.Rows.Count - 1; dataGridView1.CurrentCell = dataGridView1[0, u]; dataGridView1.Rows[u].Selected = true;
添加行号
dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders; for (int i = 0; i < dataGridView1.Rows.Count-1; i++) { DataGridViewRow r = dataGridView1.Rows[i]; r.HeaderCell.Value = string.Format("{0}", i + 1); } dataGridView1.Refresh();
获得某个指定单元格的值
dataGridView1.Rows[1].Cells[1].Value;
双击dataGridView获取当前单元值
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) { MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()); }
扫描二维码推送至手机访问