DataGridView选择行的列值到文本框 [英] DataGridView selected row's column values to textbox

查看:48
本文介绍了DataGridView选择行的列值到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助从datagridview获取值到文本框,我有一个datagridview从SQL获取数据从4个列和从sql检索的行数,我有4个文本框,我想要datagridview选择行的列文本框的值。



请提供同样的建议。

解决方案

您可能想要回复到 DataGridView.SelectionChanged事件 [ ^ ]。br />
在其处理程序中检查是否已选择单行,然后从该行获取值并将它们放在文本框中。

  private   void  DataGridView1_SelectionChanged( object  sender,EventArgs e)
{
if (DataGridView1.SelectedRows.Count = 0
{
TextBox1.Value = DataGridView1.SelectedRows( 0 )。单元格( 0 )。值;
TextBox2.Value = DataGridView1.SelectedRows( 0 )。单元格( 1 )。
TextBox3.Value = DataGridView1.SelectedRows( 0 )。单元格( 2 )。值;
TextBox4.Value = DataGridView1.SelectedRows( 0 )。单元格( 3 )。
}
}



该代码尚未经过测试,可能包含拼写错误。



确保 DataGridView.SelectionMode属性 [ ^ 设置为 FullRowSelect 以使其正常工作。



您可以使用 DataGridView.MultiSelect属性 [ ^ ]以确保只选择了一行a时间。


它现在好了

<前lang =c#> 私人 void dgviewSearch_CellcontentClick( object sender,DataGridViewCellEventArgs e)
{
int i;
i = dgViewSearch.SelectedCells [ 0 ]。RowIndex;
textBox3.Text = dgViewSearch.Rows [i] .Cells [ 1 ]。Value.ToString();
textBox4.Text = dgViewSearch.Rows [i] .Cells [ 3 ]。Value.ToString();
}





非常感谢您的帮助,我期待在datagridview上选择多个文本框,(逗号)喜欢(2001,2002,2003)


注册网格的MouseClick事件并使用以下代码。

  private   void  dataGridView1_MouseClick( object  sender,MouseEventArgs e)
{
DataGridViewRow dr = dataGridView1.SelectedRows [ 0 ];
textBox1.Text = dr.Cells [ 0 ]。Value.ToString();
// 或只是使用列名而不是索引
// dr.Cells [id]。Value.ToString();
textBox2.Text = dr .Cells [ 1 ]。Value.ToString();
textBox3.Text = dr.Cells [ 2 ]。Value.ToString();
textBox4.Text = dr.Cells [ 3 ]。Value.ToString();
}



并在加载事件中添加以下行

 dataGridView1.SelectionMode = DataGridViewSelectionMode .FullRowSelect; 


Hi, need help on getting the values from datagridview to textboxes, i am having a datagridview getting data's from SQL which counts 4 column and number of rows retrieved from sql, i am having 4 textboxes, i want the datagridview selected row's column values to the textboxes.

Kindly advice on the same.

解决方案

You will probably want to respond the to the DataGridView.SelectionChanged Event[^].
In its handler check if a single row has been selected then get the values from that row and place them in the text boxes.

private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
  if( DataGridView1.SelectedRows.Count = 0 )
  {
    TextBox1.Value = DataGridView1.SelectedRows(0).Cells(0).Value;
    TextBox2.Value = DataGridView1.SelectedRows(0).Cells(1).Value;
    TextBox3.Value = DataGridView1.SelectedRows(0).Cells(2).Value;
    TextBox4.Value = DataGridView1.SelectedRows(0).Cells(3).Value;
  }
}


The code has not been tested and may contain typos.

Make sure the DataGridView.SelectionMode Property[^] is set to FullRowSelect for this to work.

You can use DataGridView.MultiSelect Property[^] to make sure only one row is selected a time.


Hi, its taking fine now

private void dgviewSearch_CellcontentClick(object sender, DataGridViewCellEventArgs e)
{
int i;
i = dgViewSearch.SelectedCells[0].RowIndex;
textBox3.Text = dgViewSearch.Rows[i].Cells[1].Value.ToString();
textBox4.Text = dgViewSearch.Rows[i].Cells[3].Value.ToString();
}



Thank you very much for your help, i am looking forward for mulitple selection on datagridview to textbox with ,(comma) like (2001,2002,2003)


Register MouseClick event of grid and use following code.

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    DataGridViewRow dr = dataGridView1.SelectedRows[0];
    textBox1.Text = dr.Cells[0].Value.ToString();
     // or simply use column name instead of index
    //dr.Cells["id"].Value.ToString();
    textBox2.Text = dr.Cells[1].Value.ToString();
    textBox3.Text = dr.Cells[2].Value.ToString();
    textBox4.Text = dr.Cells[3].Value.ToString();
}


And add the following line in your load event

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;


这篇关于DataGridView选择行的列值到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆