2011-10-05 65 views
33

我有一個datagridview是一個完整的行選擇。 無論單擊行中的哪個單元格,由於它突出顯示整行,我將如何從僅某個單元格獲取數據。Datagridview全行選擇,但獲得單個單元格的值

+0

可能重複:// stackoverflow.com/questions/3552497/how-do-i-get-the-selected-row-data-from-a-data-grid-view-using-selectrows) – CodeSlayer

回答

64

你可以這樣做......

 private void datagridview1_SelectionChanged(object sender, EventArgs e) 
    { 
     if (datagridview1.SelectedCells.Count > 0) 
     { 
      int selectedrowindex = datagridview1.SelectedCells[0].RowIndex; 

      DataGridViewRow selectedRow = datagridview1.Rows[selectedrowindex]; 

       string a = Convert.ToString(selectedRow.Cells["you have to mention you cell corresponding column name"].Value);   


     } 
    } 
+0

+1謝謝!爲我解決了它。 –

+0

大 - 花這個帖子花很多時間 – gbk

+0

@pratap K,DataGridView1_SelectionChanged事件會在表單加載時觸發嗎?但它發生在我的情況。請幫我解決這個問題。如果需要更多的細節,我會把它作爲一個問題發佈。 – Sanjeev4evr

6

在CellClick事件時,可以下面的代碼

string value = 
     datagridviewID.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString(); 

使用波夫代碼,你會得到你cliked單元格的值寫入。如果你想點擊行中得到paricular列的值,只列標更換e.ColumnIndex你想

+0

這是更好的答案,只需要更改' e.ColumnIndex'包含特定的字符串列名或索引,因爲問題是爲它提供的。 –

3

你可以得到的細胞也爲當前選擇CurrentRow

dataGridView1.CurrentRow.Cell[indexorname].FormattedValue 
下參考值

在這裏您可以使用索引或列名稱並獲取值。

+0

謝謝。這個作品對我來說非常好。 –

15

如果你想獲得選定單元格的內容;你需要行和單元格的索引。

int rowindex = dataGridView1.CurrentCell.RowIndex; 
int columnindex = dataGridView1.CurrentCell.ColumnIndex; 

dataGridView1.Rows[rowindex].Cells[columnindex].Value.ToString(); 
2

我只是想指出,你可以使用.selectedindex,使其乾淨了一點

string value = gridview.Rows[gridview.SelectedIndex].Cells[1].Text.ToString(); 
0

使用Cell Click提到將射擊在數據綁定,而不是有用的,如果你想其他方法選定的值,然後窗體關閉。

private void dgvProducts_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if (dgvProducts.SelectedCells.Count > 0) // Checking to see if any cell is selected 
    { 
     int mSelectedRowIndex = dgvProducts.SelectedCells[0].RowIndex; 

     DataGridViewRow mSelectedRow = dgvProducts.Rows[mSelectedRowIndex]; 

     string mCatagoryName = Convert.ToString(mSelectedRow.Cells[1].Value); 

     SomeOtherMethod(mProductName); // Passing the name to where ever you need it 

     this.close(); 
    } 
} 
0

對於那些誰也不會觸發點擊事件,他們可能會使用下面的代碼

public Form1() 
      { 
       InitializeComponent(); 
       this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick); 
      } 
1

只需使用:dataGridView1.CurrentCell.Value.ToString()

 private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e) 
     { 
      MessageBox.Show(dataGridView1.CurrentCell.Value.ToString()); 
     } 

// dataGrid1.Rows[yourRowIndex ].Cells[yourColumnIndex].Value.ToString() 

//Example1:yourRowIndex=dataGridView1.CurrentRow.Index (from selectedRow); 
dataGrid1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString() 

//Example2:yourRowIndex=3,yourColumnIndex=2 (select by programmatically) 
    dataGrid1.Rows[3].Cells[2].Value.ToString() 
0

要根據整個行選擇得到一個單元格的值:

if (dataGridView1.SelectedRows.Count > 0) 
     { 
     foreach (DataGridViewRow row in dataGridView1.Rows) 
      { 
       TextBox1.Text = row.Cells["ColumnName"].Value.ToString(); 
      } 
     } 
else 
     { 
     MessageBox.Show("Please select item!"); 
     } 
    } 
2
string value = dataGridVeiw1.CurrentRow.Cells[1].Value.ToString(); 
0

最簡單的代碼是DataGridView1.SelectedCells(column_index).Value

作爲一個例子,對於第一選擇的小區:

DataGridView1.SelectedCells(0).Value 
0

此得到me數據網格視圖中精確單元格的文本值

private void dataGridView_SelectionChanged(object sender, EventArgs e) 
    { 
     label1.Text = dataGridView.CurrentRow.Cells[#].Value.ToString(); 
    } 

,你可以看到它在標籤和更改#王氏你想要的確切細胞的指數

[我如何使用SelectedRows數據網格視圖中所選行的數據?(HTTP的
相關問題