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'.
- dgvExceptions.DataSource = ds.Tables[0];
- dgvExceptions.Columns[0].HeaderText = "ID";
- dgvExceptions.Columns[0].Width = 30;
- dgvExceptions.Columns[0].DefaultCellStyle.Alignment =
- DataGridViewContentAlignment.MiddleRight;
- dgvExceptions.Columns[1].Visible = false;
- dgvExceptions.Columns[2].HeaderText = "Occurred On";
- dgvExceptions.Columns[2].Width = 70;
- dgvExceptions.Sort(dgvExceptions.Columns[2], ListSortDirection.Descending);
- dgvExceptions.Columns[3].Visible = false;
- dgvExceptions.Columns[4].Visible = false;
- dgvExceptions.Columns[5].Visible = false;
- dgvExceptions.Columns[6].Visible = false;
- dgvExceptions.Columns[7].Width = 317;
- dgvExceptions.Columns[8].Visible = false;
- dgvExceptions.Rows[0].Selected = true;
- SetUpDataGridView(0);
18. 19. - private void dgvExceptions_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- SetUpDataGridView(e.RowIndex);
- } 24.
- private void SetUpDataGridView(int p)
- {
- DataGridViewRow row = dgvExceptions.Rows[p];
- txtExceptionId.Text = row.Cells[0].Value.ToString();
- txtConnection.Text = row.Cells[1].Value.ToString();
- txtExceptionType.Text = row.Cells[6].Value.ToString();
- txtDateOccurred.Text = row.Cells[2].Value.ToString();
- txtClass.Text = row.Cells[3].Value.ToString();
- txtModule.Text = row.Cells[4].Value.ToString();
- txtMethod.Text = row.Cells[5].Value.ToString();
- txtMessage.Text = row.Cells[7].Value.ToString();
- }
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();
}
}
精彩评论