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);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix