通过单击C#中的列标题对GridView进行排序 [英] GridView sorting by clicking column header in C#

查看:549
本文介绍了通过单击C#中的列标题对GridView进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的代码如下所示.如何通过单击任何列标题对gridview进行排序?让我们说一下,从gridview完成搜索后将其排序.
我已经设置了allow sorting = true以及如何在下面>
中进行编码

Hi,
My code looks like below. How to sort the gridview by clicking any of the column header? Let say sort it after done the search from gridview.
I already set the allow sorting = true and how should i code in below>


SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select top 1000 machineNameDisplay, equipment_type, frequency, formStatus, NextDueDate, cal_eqid from dbo.Obs_PMChkList_1 where v_dept = 'BGA EOL'";

        //DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        DataView dv = new DataView();

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
        GridView1.DataSource = dt;
        dv = dt.DefaultView;
        //Session["values"] = dv;
        //BindData("");
        GridView1.DataBind();
        conn.Close();





protected void btnSearch_Click(object sender, EventArgs e)
    {
        string searchString = txtSearch.Text;

        foreach (GridViewRow row in GridView1.Rows)
        {
            TableCellCollection cells = row.Cells;

            foreach (TableCell cell in cells)
            {
                //if (cell.Text.ToLower().StartsWith(searchString.ToLower()))
                if (cell.Text.ToLower().StartsWith(searchString.ToLower()))
                {
                    break;

                }
                else
                {
                    //does not meet
                    cell.Visible = false;
                }
            }
        }
    }







protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//How to code here?
}

推荐答案



请参考以下链接.

带有上下图标的Gridview排序,分页 [ ^ ]

使用对象数据源进行优化的分页和排序 [ ^ ]

GridView:排序和使用C#的泛型和可空数据类型函数进行分页GetaOrDefault() [ ^ ]
Hi,

Refer the below link.

Gridview Sorting with Up and Down Icons, Paging[^]

Optimized Paging and Sorting using Object Data Source[^]

GridView:Sorting & Paging Using Generics And Nullable Datatype Function Of C# GetValueOrDefault()[^]


一无所有,应该可以正常工作
nothing at all, It should working




1.我的搜索功能似乎无法正常工作.我的预期结果是显示整行,但没有显示.例如,我搜索"abc",并且该行的第二列包含"abc",系统仅从第二列开始显示.我需要它来显示整行(包括第一个单元格).

2.在此之后,如果需要单击标题,则需要对其进行排序.它必须能够从我的搜索结果中进行排序.

请帮忙!
Hi,

1. My search function seems does not working as expected. My expected result is display the whole row but it does not. For eg, i search for "abc" and the 2nd column of the row contains "abc", system only display from second column onwards. I need it to display in whole row (include 1st cell).

2. After this, i need to sort it if click on the header. It have to able to sort it from my search result.

Please help!


这篇关于通过单击C#中的列标题对GridView进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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