基于特定列值的gridview排序

时间:2016-05-12 15:41:51

标签: asp.net gridview gridview-sorting

我有一个包含以下列的gridview。

enter image description here

我想根据状态对gridview进行排序。红色状态应首先(上)然后是绿色状态。

请建议我该怎么办?

这是我的代码:

Using (OracleCommand cmd = new OracleCommand("SELECT Instance,ServerName,IPFQDN,Status from server_interspc")) 
{ 
    using (OracleDataAdapter sda = new OracleDataAdapter())
    {
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        using (DataTable dt = new DataTable()) 
        {
            sda.Fill(dt); 
            GridView1.DataSource = dt;
            GridView1.DataBind();
        } 
    } 
}

2 个答案:

答案 0 :(得分:0)

我建议您将代码更改为:

   sda.Fill(dt);   
   DataView dv = dt.DefaultView;
   //Sorting datatable as you like before set it as datasource of your gridView
   dv.Sort = "Status desc";  // Or      dv.Sort = "Status asc";
   DataTable sortedDT = dv.ToTable();
   GridView1.DataSource = sortedDT;

答案 1 :(得分:0)

只需将您的查询更改为

即可
SELECT Instance,ServerName,IPFQDN,Status from server_interspc GROUP BY Status ORDER BY Status desc