2010-10-11 121 views
0

我有一個DataGridView和一個TextBox。當我點擊DataGridView的單元格時,必須將該值複製到文本框中。但是這並不總是有效的:它只有時有效。textbox上datagridview的值

這裏是我的代碼:

private void gvProductos_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
    //DataGridViewRow row = new DataGridViewRow(); 

    //row = gvProductos.Rows[e.RowIndex]; 
    ////txtDescripcion.Text = string.Empty; 
    //txtDescripcion.Text = row.Cells[1].Value.ToString(); 

    txtDescripcion.Text = gvProductos.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); 
} 
+0

你有沒有在你的小區的鏈路,/按鈕/ LinkBut​​ton的?你需要一個控制來觸發一個事件! – 2010-10-11 23:36:04

+0

@ user459057:只有當您單擊單元格中其他任何位置的單元格的內容部分時,纔會觸發CellContentClick。 – JPReddy 2010-10-14 05:41:18

回答

1

使用CellClick事件。它始終有效。

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
     if (dataGridView2.CurrentCell != null && dataGridView2.CurrentCell.Value != null) 
     textBox1.Text = dataGridView2.CurrentCell.Value.ToString(); 
    } 

您擁有的解決方案不起作用,因爲CellContentClick僅在您單擊單元格的內容部分時觸發。如果您單擊其中沒有內容的單元格的其他區域,則它不會被觸發。這就是爲什麼這個事件不能始終工作。嘗試點擊內容只有你纔會意識到。

0

它可以在客戶端做。雖然datagrid行數據綁定發生添加一個JavaScript方法,它傳遞特定的文本點擊。因此,它會比上述方法更快的工作,並有可能訪問的值是在服務器端的文本框中 我正在使用一個隱藏的飛行,並指定我要傳遞的內容。 服務器端代碼可能是這樣的

protected void gvProductos_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       HiddenField lbl = (HiddenField)e.Row.FindControl("hdfAstID"); 
       e.Row.Attributes.Add("onclick", "addToTextBox('" + lbl.Value + "')"); 
      } 

     } 

在客戶端

function addToTextBox(Data) 
{ 
    document.getElementByID("<% textBox1.ClientID %>").value=Data; 
} 

希望這能對你的作品

+0

堅持Hari,他談論的是winforms而不是web應用程序。 – JPReddy 2010-10-14 08:37:00

0
private void grid_order_CellContentClick(
       object sender, 
       DataGridViewCellEventArgs e) 
{ 
    Textbox1.Text = Convert.ToString(Gridview.SelectedCells[0].Value);  
} 
0

的CellContentClick適用於後續選擇,但不填第一次顯示datagridview時的文本框。但是,您可以使用單獨的方法填充文本框,傳入e.RowIndex。在顯示datagridview之後,使用初始'記錄'的行值調用該方法。

  1. dgvExceptions.DataSource = ds.Tables [0];
  2. dgvExceptions.Columns [0] .HeaderText =「ID」;
  3. dgvExceptions.Columns [0] .Width = 30;
  4. dgvExceptions.Columns [0] .DefaultCellStyle.Alignment =
  5. DataGridViewContentAlignment.MiddleRight;
  6. dgvExceptions.Columns [1] .Visible = false;
  7. dgvExceptions.Columns [2] .HeaderText =「發生在」;
  8. dgvExceptions.Columns [2] .Width = 70;
  9. dgvExceptions.Sort(dgvExceptions.Columns [2],ListSortDirection.Descending);
  10. dgvExceptions.Columns [3] .Visible = false;
  11. dgvExceptions.Columns [4] .Visible = false;
  12. dgvExceptions.Columns [5] .Visible = false;
  13. dgvExceptions.Columns [6] .Visible = false;
  14. dgvExceptions.Columns [7] .Width = 317;
  15. dgvExceptions.Columns [8] .Visible = false;
  16. dgvExceptions.Rows [0]。Selected = true;
  17. SetUpDataGridView(0);
    18. 19.
  18. 私人無效dgvExceptions_CellContentClick(對象發件人,DataGridViewCellEventArgs E)
  19. {
  20. SetUpDataGridView(e.RowIndex);
  21. } 24.
  22. 私人無效SetUpDataGridView(INT P)
  23. {
  24. 的DataGridViewRow行= dgvExceptions.Rows [P];
  25. txtExceptionId.Text = row.Cells [0] .Value.ToString();
  26. txtConnection.Text = row.Cells [1] .Value.ToString();
  27. txtExceptionType.Text = row.Cells [6] .Value.ToString();
  28. txtDateOccurred.Text = row.Cells [2] .Value.ToString();
  29. txtClass.Text = row.Cells [3] .Value.ToString();
  30. txtModule.Text = row.Cells [4] .Value.ToString();
  31. txtMethod.Text = row.Cells [5] .Value.ToString();
  32. txtMessage.Text = row.Cells [7] .Value.ToString();
  33. }
0

我想你想清除文本框前給值

這樣

private void gvProductos_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
     if (dataGridView2.CurrentCell != null && dataGridView2.CurrentCell.Value != null) 
     { 
      txtDescripcion.Text = string.Empty; 
      textBox1.Text = dataGridView2.CurrentCell.Value.ToString(); 
     } 
    }