2013-01-07 106 views
0

使用DataGridView中的一行:VS2010,NET 3.5的。 我爲我可憐的標題道歉。選擇基於字符串項

我有一個字符串項它,我相信,是在的datagridview可用。現在我想選擇該項目所屬的行。

  tableName = tmp._Table;  //I have my table_name here 

//下面的代碼顯示了我的dgvtablelist的含義。

  dgvTablesList.DataSource = CS.getAllTables(serverName, dbName, authenticationType, logIn, passWord); 

所以,我怎麼會選擇我的表名datagridview的

我沒有任何**指數

回答

1

首先找到搜索值的gridview的行索引:

String searchValue = "your_table_name"; 
int rowIndex = -1; 
foreach(DataGridViewRow row in DataGridView1.Rows) 
{ 
    if(row.Cells[1].Value.ToString().Equals(searchValue)) 
    { 
     rowIndex = row.Index; 
     break; 
    } 
} 

,然後選擇..

dataGridView1.Rows[rowIndex].Selected = true; 
+0

指數超出範圍。必須是非負數且小於集合的大小。 參數名稱:索引........此錯誤方興未艾如果condiotion –

+0

如果你只有一列,改變細胞[1]中的單元格[0] – stuartd

+0

肯定。我做了,解決了。索引找到正確。但最終該行未被選中。 –

0

你應該嘗試執行此操作以獲取所選的DataGridView行。

DataGridView1.CurrentCell = DataGridView.Rows[rowIndex].Cells[0]; 

希望這會有所幫助。