开发者

check if datagrid has been sorted

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 开发者_Go百科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.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜