开发者

value of datagridview on textbox

I have a DataGridView and a TextBox. When I click on a cell of the DataGridView, the value must be copied on the text box. But this doesn't always work: it only works sometimes.

Here's my code:

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


Use CellClick event. It always works.

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

Solution which you have is not working because CellContentClick is fired only when you click on the content part of the cell. If you click on the other area of the cell where content is not there then it doesn't get fired. That is why that event is not working always. Try clicking on the content only then you will realize.


it is possible to do in client side.While datagrid row-databound occurs adds a javascript method which passes the particular text on the click .So it will works faster than above method and it is possible to acess the value which is in textbox in server side I am using a hidden flied and assigns what i to pass in that. server side code may be like this

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 + "')");
            }

        }

At Client Side

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

Hope this will works for you


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


The CellContentClick works for subsequent selections, but does not fill the text boxes for when the datagridview is first displayed. However, you can use a separate method to fill the textboxes, passing in the e.RowIndex. After the datagridview has been displayed, call the method with the row value of the initial 'record'.

  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 = "Occurred On";
  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 row = 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. }


I think you want to clear your text-box before give value

like this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜