如何在第一行位置显示选中的Datagridviews行 [英] How to show Datagridviews selected row at first row position

查看:111
本文介绍了如何在第一行位置显示选中的Datagridviews行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我的Datagridview有来自sql server 2000的记录,其中一个datagridview行与表单上显示的当前数据相关联。我正在突出显示关联的datagridview行。但我想在第一个位置显示该行。那么我该怎么做呢?这可能吗?如果是的话,任何帮助都会非常明显。



谢谢和问候,

Rizwan Gazi。

解决方案

看看 datagridview.firstdisplayedscrollingrowindex [ ^ ]

例如

  private   void  dataGridView1_SelectionChanged( object  sender,EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 0 return ;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows [ 0 ]。索引;
}

包含第一行是因为在填充dataGridView时,SelectionChanged事件似乎被触发



- VB.NET中的解决方案 - mea culpa

 Private Sub DataGridView1_SelectionChanged(ByVal sender  As  System.Object,ByVal e  As  System.EventArgs)处理DataGridView1.SelectionChanged 
如果 DataGridView1.SelectedRows.Count = < span class =code-digit> 0 然后 返回
DataGridView1。 FirstDisplayedScrollingRowIndex = DataGridView1.SelectedRows( 0 )。索引
结束 Sub


这是另一个解决方案,需要更多努力,但实际上会将所选行移动到所有行的顶部,代替移动视图以使该行看起来位于顶部但实际上并未改变其在其他行中的位置。 (即使没有足够的记录来移动视图,它也会显示在顶部,这样可以显示在顶部。)由您来决定您想要的解决方案。) />


1)将记录填入DataTable后,将另一列添加到DataTable,其名称为 selected 和布尔类型。



2)如果您当前将DataTable直接分配给DataGridView的.DataSource,而是将DataTable的.DefaultView分配给.DataSource您的DataGridView。



3)将DataTable的.DefaultView的.Sort-Property设置为 selected desc。



4)设置 selected 的.Visible-Property - 列DataGridView为false。



5)如果您目前有代码突出显示与表格相关的行,请改为:

5.1)设置选择 - 第一行的单元格为false。

5.2)设置 selected 的.Value-Property - 与form相关的row to true。

5.3在DataGridView上调用ClearSelection()。

5.4)将第一行的.Selected-Property设置为true。 / BLOCKQUOTE>

Dear all,

My Datagridview has records from sql server 2000, in which one datagridview row is associated with the current data displayed on form. I am highlighting the associated datagridview row. But I want to show that row on the first position. So how can I perform this? Is this possible or not? If yes, any help would be highly appreciable.

Thanks and regards,
Rizwan Gazi.

解决方案

Have a look at datagridview.firstdisplayedscrollingrowindex[^]
E.g.

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (dataGridView1.SelectedRows.Count == 0) return;
    dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
}

The first line is included because the SelectionChanged event appears to be fired when populating the dataGridView

[Edit] - The solution in VB.NET - mea culpa

Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
    If DataGridView1.SelectedRows.Count = 0 Then Return
    DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.SelectedRows(0).Index
End Sub


Here's another solution, which requires a bit more effort but will actually move the selected row to the top of all rows, instead of moving the "view" so that the row appears to be at the top but not actually changes its position among the other rows. (Which has the benefit that it will show at the top even if there aren't enough records present to move the "view" so it could appear at the top.) It's up to you to decide which solution you want :)

1) After filling the records into a DataTable, add another column to the DataTable with a name like "selected" and type of boolean.

2) If you currently assign the DataTable directly to the .DataSource of your DataGridView, instead assign the .DefaultView of the DataTable to the .DataSource of your DataGridView.

3) Set the .Sort-Property of the .DefaultView of the DataTable to "selected desc".

4) Set the .Visible-Property of the "selected"-column of the DataGridView to false.

5) Where you currently have your code to highlight the "form-associated" row, do this instead:
5.1) Set the .Value-Property of the "selected"-cell of the first row to false.
5.2) Set the .Value-Property of the "selected"-cell of the "form-associated" row to true.
5.3) Call ClearSelection() on the DataGridView.
5.4) Set the .Selected-Property of the first row to true.


这篇关于如何在第一行位置显示选中的Datagridviews行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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