如何在数据网格视图中使用selectionchanged事件?
我在Windows应用程序中使用了一个数据网格视图和两个文本框以及两个按钮,一个按钮用于保存,另一个按钮用于更新.保存数据后,它将显示在数据网格视图中.然后,当我单击datagridview的单元格时,将使用selectionchanged事件在文本框中显示数据.
Hi,
I have used a one data gridview and two text box and two button in a form of windows application, one button for Save and other one for Update.After Save the datas, it will be display in data gridview. and then when i click the cell of datagridview,the data will display in the text box by using selectionchanged event.
private void dgvService_SelectionChanged(object sender, EventArgs e)
{
txtService.Text = dgvService.CurrentCell.Value.ToString();
label2.Text = dgvService.CurrentCell.Value.ToString();
}
通过上面的示例,我只能显示一列,但是当我单击一行时,数据gridview的第一列应显示在第一个文本框中,数据gridview的第二列应显示在第二个文本框中.
对于这个问题,请给我答案.
谢谢,
Viswanathan.M
By using the above example, i can display only one column.But when i click one row, the first column of data gridview should be display in first text box and second column of data gridview should display in second text box.
For This question,kindly send me the answer.
Thanks,
Viswanathan.M
处理DataGridView.SelectionChanged事件:
Handle the DataGridView.SelectionChanged event:
private void myDataGridView_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv != null && dgv.SelectedRows.Count > 0)
{
DataGridViewRow row = dgv.SelectedRows[0];
if (row != null)
{
myFirstTextBox.Text = row.Cells[0].Value.ToString();
mySecondTextBox.Text = row.Cells[1].Value.ToString();
}
}
}
您可能希望将DataGridView.SelectionMode设置为DataGridViewSelectionMode.FullRowSelect
You may want to set the DataGridView.SelectionMode to DataGridViewSelectionMode.FullRowSelect
尝试
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
textbox1.Text = dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value.ToString();
textbox2.Text = dataGridView1.Rows[e.RowIndex].Cells["Column2"].Value.ToString();
}