知道列名,定位到当前行的某列中:(个人认为方法一比较科学)
(1)DataGridViewCell CurrentCell=dg2.CurrentCell;//定位当前行
if(CurrentCell !=null && CurrentCell.OwningColumn.Name=="列名") //定位当前行某列
{
//对选定单元进行操作
}
(2)DataGridView curDgv = (DataGridView)sender;
foreach (DataGridViewRow Row in curDgv.Rows)
{
if (Row != null)
{
foreach (DataGridViewCell cell in Row.Cells)
{
if (curDgv.Columns[cell.ColumnIndex].DataPropertyName.Equals("列名"))
{
//对选定单元进行操作
}
}
}
}

在C#中,使用DataGridView时,可以通过列名快速定位到当前行的特定列。方法一是通过CurrentCell获取,判断OwningColumn.Name是否匹配;方法二是遍历每一行的每个单元格,检查DataPropertyName是否等于目标列名。一旦找到目标列,即可对选定单元格进行操作。
721

被折叠的 条评论
为什么被折叠?



