知道列名,定位到當前行的某列中:(個人認爲方法一比較科學)
(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("列名"))
{
//對選定單元進行操作
}
}
}
}