2011-10-05 40 views
33

Tôi có một chế độ xem dữ liệu là một hàng đầy đủ chọn. Làm cách nào để lấy dữ liệu từ một ô nhất định bất kể ô nào trong hàng được nhấp vào vì nó làm nổi bật toàn bộ hàng.Datagridview chọn toàn bộ hàng nhưng nhận được giá trị ô đơn

+0

có thể trùng lặp của [Làm cách nào để nhận dữ liệu hàng đã chọn từ chế độ xem lưới dữ liệu bằng cách sử dụng SelectedRows?] (Http: // stackoverflow.com/questions/3552497/how-do-i-get-the-selected-row-data-from-a-data-grid-view-using-selectedrows) – CodeSlayer

Trả lời

64

Bạn có thể làm như thế này ......

 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 Cảm ơn! Giải quyết nó cho tôi. –

+0

Tuyệt vời - dành nhiều thời gian trong khi đăng bài này – gbk

+0

@pratap K, Liệu sự kiện DataGridView1_SelectionChanged sẽ kích hoạt tại thời điểm tải biểu mẫu? nhưng nó đang xảy ra trong trường hợp của tôi. Xin hãy giúp tôi giải quyết vấn đề này. Nếu cần thêm chi tiết, tôi sẽ đăng nó như một câu hỏi. – Sanjeev4evr

6

Trong trường hợp CellClick bạn có thể viết mã sau

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

Sử dụng mã Bove bạn sẽ nhận được giá trị của ô bạn cliked. Nếu bạn muốn nhận được giá trị của cột paricular ở hàng nhấn vào, chỉ cần thay thế e.ColumnIndex với chỉ số cột bạn muốn

+0

Đây là câu trả lời hay hơn, chỉ cần thay đổi ' e.ColumnIndex' với tên cột hoặc chỉ mục chuỗi cụ thể vì câu hỏi đã được khắc phục. –

3

bạn có thể nhận được các giá trị của các tế bào cũng như các lựa chọn hiện tại được tham chiếu dưới CurrentRow

dataGridView1.CurrentRow.Cell[indexorname].FormattedValue 

Tại đây bạn có thể sử dụng tên chỉ mục hoặc cột và nhận giá trị.

+0

cảm ơn bạn.này hoạt động rất tốt cho tôi. –

15

Nếu bạn muốn nhận được các nội dung của ô được chọn; bạn cần chỉ mục của hàng và ô.

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

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

Tôi chỉ muốn chỉ ra, bạn có thể sử dụng .selectedindex để làm cho nó một chút bụi

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

Sử dụng Cell Click như các phương pháp khác được đề cập sẽ cháy khi dữ liệu ràng buộc, không hữu ích nếu bạn muốn giá trị đã chọn, sau đó là biểu mẫu để đóng.

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

Đối với những người không thể cháy, sự kiện nhấp chuột, họ có thể sử dụng đoạn mã sau

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

Chỉ cần sử dụng:dataGridView1.CurrentCell.Value.ToString()

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

hoặc

// 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

Để có được một giá trị tế bào dựa trên toàn bộ lựa chọn hàng:

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

đang đơn giản nhất là DataGridView1.SelectedCells(column_index).Value

Như một ví dụ, đối với ô được chọn đầu tiên:

DataGridView1.SelectedCells(0).Value 
0

này có được tôi giá trị văn bản của ô chính xác trong chế độ xem lưới dữ liệu

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

bạn có thể thấy trong nhãn và thay đổi # wih chỉ mục của ô chính xác mà bạn muốn

Các vấn đề liên quan