【问题标题】:How do I search for a specific cell in a DataGridView?如何在 DataGridView 中搜索特定单元格?
【发布时间】:2011-10-18 14:02:45
【问题描述】:

如何在 DataGridView 中获取列标题文本 = "something" 且行包含 "somethings" 的单元格索引?

【问题讨论】:

标签: c# datagridview


【解决方案1】:

您不能直接通过名称访问 DataGridView 列。你需要做这样的事情:

int FindCellRowIndex( string columnName, string rowContent ){
    foreach (DataGridViewRow row in dgView.Rows){             
           foreach (DataGridViewCell cell in row.Cells){
               if( cell.OwningColumn.Name() == columnName && cell.Value != null && Convert.Tostring(cell.Value) == rowContent)
                  return row.Index;
           }             
    }
    return -1;
}

【讨论】:

  • 感谢您的建议。我正在尝试同样的事情,但你的代码中的问题是当我输入 col 而不是下拉菜单单元格属性中的 pres 点时丢失。如果我写 col.Cell 它会给出异常:错误 1 ​​'System.Windows.Forms.DataGridViewColumn' 不包含 'Cells' 的定义并且没有扩展方法 'Cells'
  • 我的错,你不能遍历 DataGridViewColumn 的单元格。但是您可以使用 DataGridViewRow 来做到这一点。我更正了代码,检查一下。问题是现在它必须遍历所有单元格,所以效率不高。如果有人知道更好的方法,请告诉
【解决方案2】:

您应该遍历GridViewRowCollection,然后遍历单个 GridViewRow 的每个单元格。如果你这样做,那么你可以获得单元格的文本属性。另一种选择是使用 FindControl 并提取适当的属性。您可以使用.FindControl() 方法在单元格内查找实际控件(标签、文字、复选框等),或者通过索引GridView.Rows.Cells[index] 来遍历它们

【讨论】:

    猜你喜欢
    • 2019-01-25
    • 2013-11-10
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多