开发者

How do I search for a specific cell in a DataGridView?

开发者 https://www.devze.com 2023-03-25 15:34 出处:网络
How can I get the cell index in a DataG开发者_如何学CridView where the column header text = \"something\" and the row contains \"somethings\"?You can\'t access a DataGridView column directly by its na

How can I get the cell index in a DataG开发者_如何学CridView where the column header text = "something" and the row contains "somethings"?


You can't access a DataGridView column directly by its name. You'd need to do something like this:

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;
}


You should iterate through the GridViewRowCollection and then each cell of the individual GridViewRow. If you do that, then you can get the cell's text property. Another option is to use FindControl and extract the appropriate property. You can use the .FindControl() method to find actual controls (label, literal, checkbox, etc) inside cells or go through them by index GridView.Rows.Cells[index]

0

精彩评论

暂无评论...
验证码 换一张
取 消