vb.net 选择DataGridView的第一行并在文本框中显示单元格内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29583797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:00:06  来源:igfitidea点击:

Select first row of DataGridView and display cell contents in text boxes

vb.netdatagrid

提问by Randy Castillo

I am working on a small VB application that retrieves customer data (ID, Name, Last Name, Purchase Detail) from an SQL table and displays it on a DataGridView.

我正在开发一个小型 VB 应用程序,该应用程序从 SQL 表中检索客户数据(ID、姓名、姓氏、购买详细信息)并将其显示在 DataGridView 上。

There are 4 text fields on the same form, and my goal is that every time a DataGridView row is clicked on, the contents of its cells will be displayed on those fields.

同一个表单上有 4 个文本字段,我的目标是每次单击 DataGridView 行时,其单元格的内容都会显示在这些字段上。

So far everything′s working fine, except for this: Once the form finishes loading and the DataGridView is displayed, the first row of the grid is selected (highlighted) but its data is not displayed on the text fields.

到目前为止一切正常,除了:一旦表单完成加载并显示 DataGridView,网格的第一行被选中(突出显示),但其数据不会显示在文本字段中。

If I click once on that row, the data IS displayed.

如果我在该行上单击一次,则会显示数据。

So my problem is that I have not been able to programatically make the data from the 1st row to be automatically displayed on the text fields once the form finished loading.

所以我的问题是,一旦表单完成加载,我无法以编程方式使第一行中的数据自动显示在文本字段上。

I have tried

我试过了

Form1.DataGrid1.Rows(0).Selected = true

Form1.DataGrid1.Rows(0).Selected = true

and also

并且

Form1.DataGrid1.Rows.GetFirstRow(DataGridViewElementStates.Visible)

Form1.DataGrid1.Rows.GetFirstRow(DataGridViewElementStates.Visible)

but neither code works. The 1st row is highlighted but the data is not displayed on the text fields unless I click on the row.

但两种代码都不起作用。第一行突出显示,但除非我单击该行,否则数据不会显示在文本字段中。

I was thinking about programatically simulate a mouse click on the 1st row, but I have no idea if that′s a feasible solution.

我正在考虑以编程方式模拟第一行上的鼠标单击,但我不知道这是否是可行的解决方案。

Any help you can provide will be much appreciated. Many thanks in advance!

您可以提供的任何帮助将不胜感激。提前谢谢了!

Randy

兰迪

采纳答案by jmcilhinney

The reason that selecting the row simply highlights it is that that is exactly what selection is. You can select multiple rows at the same time but obviously you couldn't display data from multiple rows in your other controls at the same time.

选择行只是突出显示它的原因是这正​​是选择。您可以同时选择多行,但显然您无法同时在其他控件中显示多行中的数据。

How exactly are you loading the data into the grid? If you're binding it via a BindingSource, as you should be, then what you should actually be doing is setting the Currentor Positionof the BindingSource. If the other controls are bound too then that will have the desired effect.

你究竟是如何将数据加载到网格中的?如果您是通过结合它BindingSource,你应该是,那么你实际上应该做的是设置CurrentPositionBindingSource。如果其他控件也被绑定,那么这将产生预期的效果。

Otherwise, what you need to do is to set the CurrentRowproperty, which you can only do by assigning a cell in that row to the CurrentCellproperty.

否则,您需要做的是设置CurrentRow属性,您只能通过将该行中的单元格分配给CurrentCell属性来完成。

回答by Pradnya Bolli

Try this..

尝试这个..

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

also see this link

另请参阅此链接