datagridview常用操作
1、获取行数
dataGridView1.Rows.Count;
2、选中某行
dataGridView1.ClearSelection();// 清除其他行选择
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;//选择最后一行
3、给不同单元格赋值


// Item方法有两个重载 第一个参数可以是 列号索引也可以是 列名 第二个参数只能是行号索引
FrmMain.GridFile.Item("文件名", File.RelevanceFileIndex).Value = RelevanceFileName
FrmMain.GridFile.Item(5, File.RelevanceFileIndex).Value = RelevanceFileName
//给选中的单元格赋值
int row = dataGridView1.SelectedRows[0].Index ;
dataGridView1["X", row].Value = RobotState.XState.pos.ToString("F3");
dataGridView1["Y", row].Value = RobotState.YState.pos.ToString("F3");
// Retrieve the cell value for the cell at column 3, row 7.
检索第3列,第7行的值
String testValue1 = (String)dataGridView1[3, 7].Value;
// Retrieve the cell value for the cell in the Name column at row 4.
在第 4 行的 Name 列中检索单元格的单元格值。
String testValue2 = (String)dataGridView1["Name", 4].Value;
// Retrieve the cell value for the cell at column 3, row 7.
String testValue1 = (String)dataGridView1[3, 7].Value;
// Retrieve the cell value for the cell in the Name column at row 4.
String testValue2 = (String)dataGridView1["Name", 4].Value;
VB中
精准给指定表格赋值 或 读值
DataGrideView(ColumName, rowIndex).Value = RobotState.YState.pos.ToString("F3")
columName 可是列的索引地址 也可以是 列名
rowindex 只能是 行号
也可以整行添加
DataGrideView.Rows.Add() 添加空行返回值是行号
行数 rowIndex =DataGrideView.Rows.Add({NewRowName,“12313”,“223”})
4、隐藏第一列
dataGridView1.RowHeadersVisible = false;
5、提高datagridview加载 速度属性设置
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
6、添加行头序号
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
}

浙公网安备 33010602011771号