如何通过按钮选择行DataGridview [英] how to select row DataGridview by button

查看:111
本文介绍了如何通过按钮选择行DataGridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,其中包含大量数据,我想使用按钮逐个选择数据.

I have a datagridview containing lot of data and I want to select data one by one using button.

dim i as integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       i += 1
       DataGridView1.Rows(i).Selected = True
End Sub



它可以正常工作,但是当行超出DataGridView控件时,选定的行不会自动聚焦.
(需要滚动条才能查看当前选中的内容)

如何将自动对焦设置为选定的行?



It''s working, but when row out of DataGridView control, the selected row didn''t auto focus.
(need scroll bar to see current selected)

How to set auto focus to selected row?
or
Is there any better code to make this work?

推荐答案

我不是VB专家,但是必须通过以下方式实现:
(请注意,下面的代码是C#)
I''m not an expert in VB but this must be accomplished something like this:
(Please note that the below code is C#)
DataGridView1.SelectedIndex = 1; //get the index of the row to set focus
DataGridView1.SelectedRow.Focus();
//Additionally, you can also change the background color of the row to grab the user''s attention
//DataGridView1.SelectedRow.BackColor = System.Drawing.Color.Yellow;


准确的答案取决于您如何绑定数据网格.

如果通过BindingSource组件进行绑定,则可以使用 CurrencyManager.Position [ ^ ]属性来模拟该属性,如示例中所示.
An accurate answer depends on how you are binding your datagrid.

If you are binding via a BindingSource component you can use the BindingSource.MoveNext[^] BindingSource.MovePrev methods.

Or alternatively you might be able to use the CurrencyManager.Position[^] property to simulate that as shown in the example.


感谢您的回答..
我也不是C#的专家..但是运行时出现错误.. :)

thank you for answer..
i''m also not expert in c#.. but i got error when run.. :)

DataGridView1.SelectedIndex = 1;



错误提示:
``SelectedIndex''不是``System.Windows.Forms.DataGridView''的成员:(

我已经尝试在其他控件中使用SelectedIndex,并且它仅适用于Listbox或ComboBox控件,并且它具有自动对焦功能

我认为将列表框用作表格视图会更加复杂..:sigh:



error say :
''SelectedIndex'' is not a member of ''System.Windows.Forms.DataGridView'' :(

i already try using SelectedIndex in other control and it''s only work with Listbox or ComboBox control, and it''s auto focus

i think using listbox as tabel view will be more complicate.. :sigh:


这篇关于如何通过按钮选择行DataGridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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