排序DataGridView不适合我

时间:2013-01-21 18:35:17

标签: c# winforms datagridview

我有数据库,我填写DataGridView。

我尝试通过按下Heder来对我的DataGridView进行排序:

private void dataGridView1_ColumnHeaderMouseClick(object sender,DataGridViewCellMouseEventArgs e)
{
     SortOrder ss = dataGridView1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection;
     string strColumnName = dataGridView1.Columns[e.ColumnIndex].Name;
     label8.Text = "Column: " + strColumnName + " - " + "sort order: " + ss.ToString();

     if (strColumnName == "Column9")
     {
        if (ss == SortOrder.Ascending)
        {
            A_table = "Tmp_Kabat_SfiraTbl";
            SQL = "SELECT Color,Line,Makat,Des,sQty,sNewQty,CountBy_Name,UserName,TermNum,NewQty,Qty from " + A_table + " order by NewQty asc";
        }
        else if (ss == SortOrder.Descending)
        {
            A_table = "Tmp_Kabat_SfiraTbl";
            SQL = "SELECT Color,Line,Makat,Des,sQty,sNewQty,CountBy_Name,UserName,TermNum,NewQty,Qty from " + A_table + " order by NewQty desc";
        }
            dsView = new DataSet();
            adp = new OleDbDataAdapter(SQL, Main.Conn);
            adp.Fill(dsView, A_table);
            adp.Dispose();
            dataGridView1.DataSource = dsView.Tables[A_table].DefaultView;
            this.dataGridView1.ClearSelection();
      }
}

它的作品只有一次,它不会从asc变为desc排序

可能是什么问题?

2 个答案:

答案 0 :(得分:1)

您需要为HeaderCell更改SortGlyphDirection,最好的方法是在dataGridView1_ColumnHeaderMouseClick内的最后一行代码中。

这是一些不错的排序示例[DataGridViewColumnHeaderCell.SortGlyphDirection Property] [1]

现在将如何在您的样本中使用它:

    private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        SortOrder ss = dataGridView1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection;
        string strColumnName = dataGridView1.Columns[e.ColumnIndex].Name;

        PopulateDataGridView(ss, strColumnName);

        //change sorting...
        switch (ss)
        {
            case SortOrder.Ascending:
                ss = SortOrder.Descending;
                break;
            case SortOrder.Descending:
                ss = SortOrder.Ascending;
                break;
        }
        dataGridView1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = ss;
        //change column sort mode...
        dataGridView1.Columns[e.ColumnIndex].SortMode = DataGridViewColumnSortMode.Programmatic;
    }

    void PopulateDataGridView(SortOrder ss, string strColumnName)
    {
        label8.Text = "Column: " + strColumnName + " - " + "sort order: " + ss.ToString();
        if (ss == SortOrder.Ascending)
        {
            A_table = "Tmp_Kabat_SfiraTbl";
            SQL = "SELECT Color,Line,Makat,Des,sQty,sNewQty,CountBy_Name,UserName,TermNum,NewQty,Qty from " + A_table + " order by " + strColumnName + " asc";
        }
        else if (ss == SortOrder.Descending)
        {
            A_table = "Tmp_Kabat_SfiraTbl";
            SQL = "SELECT Color,Line,Makat,Des,sQty,sNewQty,CountBy_Name,UserName,TermNum,NewQty,Qty from " + A_table + " order by " + strColumnName + " desc";
        }
        dsView = new DataSet();
        adp = new OleDbDataAdapter(SQL, Conn);
        adp.Fill(dsView, A_table);
        adp.Dispose();
        dataGridView1.DataSource = dsView.Tables[A_table].DefaultView;
        this.dataGridView1.ClearSelection();
    }

对于默认人口,您可以使用以下内容:

PopulateDataGridView(SortOrder.Ascending, "Color");

答案 1 :(得分:0)

我想如果你设置:

dataGridView1.DataSource = null 

然后分配:

dataGridView1.DataSource = dsView.Tables[A_table].DefaultView;

它应该工作(我前段时间有类似问题..),任何方式都可以使用DataView命令属性(不执行sql语句)