C# winform DataGridView如何获取选中行/列的某个数据
C# DataGridView如何获取选中行/列的某个数据
DataGridView的几个基本操作:
1、获得某个(指定的)单元格的值:
dataGridView1.Row[i].Cells[j].Value;
2、获得选中的总行数:
dataGridView1.SelectedRows.Count;
3、获得当前选中行的索引:
dataGridView1.CurrentRow.Index;
4、获得当前选中单元格的值:
dataGridView1.CurrentCell.Value;
5、取选中行的数据
string[] str = new string[dataGridView.Rows.Count];
for(int i;i<dataGridView1.Rows.Count;i++)
{
if(dataGridView1.Rows[i].Selected == true)
{
str[i] = dataGridView1.Rows[i].Cells[1].Value.ToString();
}
}
7、获取选中行的某个数据
int a = dataGridView1.SelectedRows.Index;
dataGridView1.Rows[a].Cells[“你想要的某一列的索引,想要几就写几”].Value;
获得某个(指定的)单元格的值: dataGridView1.Row[i].Cells[j].Value; Row[i] 应该是Rows[i]
int a=dataGridView1.CurrentRow.Index;
string str=dataGridView1.Row[a].Cells[“strName”].Value.Tostring();
selectedRows[0]当前选中的行
.cell[列索引].values 就是当前选中行的某个单元格的值
DataGridView1.SelectedCells(0).Value.ToString 取当前选择单元内容
DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString 当前选择单元第N列内容
8、获取选中列的所有数据
//遍历DataTable,取出FCollaborativePosition列所有的数据:
List<string> listFCollaborativePosition = (from d in dtrelas.AsEnumerable() select d.Field<string>("FCollaborativePosition")).ToList();
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步