检查数据网格是否已排序

发布于 2024-12-01 16:59:38 字数 459 浏览 0 评论 0原文

在datagrid Item Databound事件中,我想知道网格是否已排序或第一次加载。我知道有一个 OnSortCommand 事件,我可以在此处设置一些变量来检查网格上是否发生排序。但我想知道是否有更好的方法来检查网格是否已排序。感谢副词。为了你的帮助。

PS:我看了一下这个post 并建议检查 Request.Form["__EVENTTARGET"]Request.Form["__EVENTARGUMENT"]。假设我在网格和表单上的其他服务器控件中有“x”个列,我觉得使用“x”个 If 条件并检查请求是否来自这些控件之一不是正确的方法。

In the datagrid Item Databound event, I want to know if the grid has been sorted or loading for the first time. I know there is an event OnSortCommand and I can set some variable here to check if there is a sort happening on the grid. But I want to know if there is a better way to check if the grid has been sorted. Thanks in adv. for your help.

PS: I took a look at this post and it suggests to check for Request.Form["__EVENTTARGET"] and Request.Form["__EVENTARGUMENT"]. Let's say I have 'x' number of columns in the grid and other server controls on the form, I feel it's not a correct way to have 'x' If conditions and check if the request is from one of those many controls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌上芳菲 2024-12-08 16:59:38

如果 DataGridView 已排序,则将设置其 SortedColumn 属性。

If a DataGridView is sorted, its SortedColumn property will be set.

夕色琉璃 2024-12-08 16:59:38

下面是验证 DataGridView 控件列的排序顺序或检查属性 sortorder 的状态的示例。

 private void button1_Click(object sender, EventArgs e)
    {
        this.dataGridView1.Sorted +=new EventHandler(dataGridView1_Sorted);
    }

    void dataGridView1_Sorted(object sender, EventArgs e)
    {
        if (this.dataGridView1.SortOrder.Equals(SortOrder.Ascending))
        {
            // your code here
        }

        if (this.dataGridView1.SortOrder.Equals(SortOrder.Descending))
        {
            // your code here
        }

        if (this.dataGridView1.SortOrder.Equals(SortOrder.None))
        {
            // your code here
        }
    }

问候

Here is an example to verify the sort order of the columns of the DataGridView control or checking the status of the property sortorder.

 private void button1_Click(object sender, EventArgs e)
    {
        this.dataGridView1.Sorted +=new EventHandler(dataGridView1_Sorted);
    }

    void dataGridView1_Sorted(object sender, EventArgs e)
    {
        if (this.dataGridView1.SortOrder.Equals(SortOrder.Ascending))
        {
            // your code here
        }

        if (this.dataGridView1.SortOrder.Equals(SortOrder.Descending))
        {
            // your code here
        }

        if (this.dataGridView1.SortOrder.Equals(SortOrder.None))
        {
            // your code here
        }
    }

Regards

柒七 2024-12-08 16:59:38

您链接的帖子中的建议相当不雅;)

我建议您阅读此内容,这是非常直接的示例。它使用 DataView 对数据进行排序。如果您出于某种原因想要“记住”用于对网格进行排序的最后一个排序表达式,您可以简单地将其存储在 ViewState 中,如下所示:

ViewState["LastSortExpression"]=e.SortExpression;  

并根据需要在 PostBack 上检索它。

The suggestion from the post you linked is rather inelegant ;)

I suggest you read this, which as very straight forward example. It uses a DataView to sort the data. If you want to "remember" for whatever reason, the last sort expression that was used to sort the grid, you can simply store it in ViewState as so:

ViewState["LastSortExpression"]=e.SortExpression;  

And retrieve it on PostBack as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文