怎么在代码中设置dataGridView中的某个单元格为选定状态
展开全部
完整解决:
/// <summary>
/// 设置单元格
/// </summary>
/// <param name= "value "> 要选择单元格的值 </param>
/// <param name= "colIndex "> 列索引 </param>
private void SetCurrentCell(object value,int colIndex)
{
try
{
foreach (DataGridViewRow dgr in dataGridView1.Rows)
{
//判断单元格值
if (dgr.Cells[colIndex].Value == value)
{
//设置当前单元格
dataGridView1.CurrentCell = dgr.Cells[colIndex];
//设置选中状态
dgr.Cells[colIndex].Selected = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 设置单元格
/// </summary>
/// <param name= "value "> 要选择单元格的值 </param>
/// <param name= "colIndex "> 列索引 </param>
private void SetCurrentCell(object value,int colIndex)
{
try
{
foreach (DataGridViewRow dgr in dataGridView1.Rows)
{
//判断单元格值
if (dgr.Cells[colIndex].Value == value)
{
//设置当前单元格
dataGridView1.CurrentCell = dgr.Cells[colIndex];
//设置选中状态
dgr.Cells[colIndex].Selected = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询