views:

37

answers:

2

i have a datagridview and a text box, when i click on a cell of datagridview, the value must copy on the text box, but this not always work

olny works sometimes please some help

thnk.

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

    }
A: 

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.

JPReddy
A: 

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

Vishnu K B
Hold on Hari, he is talking about winforms not web application.
JPReddy