【问题标题】:value of datagridview on textbox文本框上 datagridview 的值
【发布时间】:2014-09-12 17:13:39
【问题描述】:

我有一个 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();
}

【问题讨论】:

  • 您的单元格中有链接/按钮/链接按钮吗?你需要一个控件来触发一个事件!
  • @user459057:只有当您单击单元格的内容部分而不是单元格中的任何其他位置时,才会触发 CellContentClick。

标签: c# winforms datagridview


【解决方案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。如果您单击内容不存在的单元格的其他区域,则它不会被触发。这就是为什么该事件并不总是有效的原因。尝试点击内容,然后你就会意识到。

【讨论】:

    【解决方案2】:

    可以在客户端进行。当数据网格行数据绑定发生时,添加了一个 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;
    }
    

    希望这对你有用

    【讨论】:

    • 等一下 Hari,他说的是 winforms 而不是 web 应用程序。
    【解决方案3】:
    private void grid_order_CellContentClick(
                     object sender, 
                     DataGridViewCellEventArgs e)
    {
        Textbox1.Text = Convert.ToString( Gridview.SelectedCells[0].Value);      
    }
    

    【讨论】:

      【解决方案4】:

      CellContentClick 适用于后续选择,但不会填充数据网格视图首次显示时的文本框。但是,您可以使用单独的方法来填充文本框,传入 e.RowIndex。显示数据网格视图后,使用初始“​​记录”的行值调用该方法。

      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. private void dgvExceptions_CellContentClick(object sender, DataGridViewCellEventArgs e)
      19. {
      20. SetUpDataGridView(e.RowIndex);
      21. } 24.
      22. private void 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. }

      【讨论】:

        【解决方案5】:

        我认为你想在赋予价值之前清除你的文本框

        喜欢这个

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-07
          • 1970-01-01
          相关资源
          最近更新 更多