在DataGridView中将最后一行设置为Frozen [英] Setting last row as Frozen in DataGridView
问题描述
我的网格当前有40行。一次只能显示20行,因此网格上有一个滚动条。
My grid currently has 40 rows. Only 20 rows can be displayed at a time, so the grid has a scrollbar.
我想冻结DataGridView的最后一行,但是滚动条消失了。
I want to freeze the last row of the DataGridView, but then the scrollbar disappears. How to solve this?
推荐答案
您正在使用 DataGridviewRow.Frozen
属性:请参见文档这里。
You are using the DataGridviewRow.Frozen
property: see documentation here.
此属性可让您在用户使用一或几行重要
信息时保留它们在DataGridView中滚动。 冻结行上方的所有
行也都被冻结。
从本质上讲,这意味着,如果您冻结最后一行,冻结行上方的所有行也将冻结;
Which essentially means, if you "freeze" the last row, all rows above the frozen row get frozen too; meaning that the scrollbar gets, because you freeze the last row, removed.
回答问题;这意味着滚动条被删除了,因为冻结了最后一行。您不能单独冻结 仅最后一行,这不是冻结
属性的性质。
To answer your question; you can not individually "freeze" only the last row, this is not in the nature of Frozen
property.
在此文档。但是,它在VB中,因此您必须自己将其翻译为C#。
There is a workaround documented in this document. However, it is in VB, so you will have to translate this to C# yourself.
实际上,我进一步发现此文档,在C#中有一个小例子。
Actually looking further I found this document, which has a small example in C#. It appears to have bugs but might get you going towards your goal.
public partial class MyDataGridView : DataGridView
{
public StatusStrip Footer
{
get { return (StatusStrip)this.Controls["Footer"]; }
}
private bool _footerVisible;
[Browsable(false)]
///
/// Sets or Gets the value specifying if a footer bar is shown or not
///
public bool FooterVisible
{
get { return _footerVisible; }
set
{
_footerVisible = value;
this.Controls["Footer"].Visible = _footerVisible;
}
}
public MyDataGridView()
{
InitializeComponent();
StatusStrip footer = new StatusStrip();
footer.Name = "Footer";
footer.ForeColor = Color.Black;
this.Controls.Add(footer);
((StatusStrip)this.Controls["Footer"]).Visible = _footerVisible;
((StatusStrip)this.Controls["Footer"]).VisibleChanged += new EventHandler(RDataGridView_VisibleChanged);
this.Scroll += new ScrollEventHandler(RDataGridView_Scroll);
_footerItems = ((StatusStrip)this.Controls["Footer"]).Items;
}
}
上述代码可用作用户控件和继承自 DataGridView
。然后添加一个页脚,您可以在其中填充所选的最后一行。如果将所有行的 Frozen
属性设置为False,则滚动条仍然可见。
The above stated code can be used as a usercontrol and inherits from the DataGridView
. It then adds a footer which you can fill with the last row of your choice. The scrollbar still will be visible if you set the Frozen
property of all rows to False.
这篇关于在DataGridView中将最后一行设置为Frozen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!