0

我有我在 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 排序

可能是什么问题?

4

2 回答 2

1

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

这是一些不错的排序示例 [DataGridViewColumnHeaderCell.SortGlyphDirection 属性][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");
于 2013-01-21T18:48:51.520 回答
0

我想如果你设置:

dataGridView1.DataSource = null 

然后分配:

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

它应该可以工作(我前段时间遇到过类似的问题..),您可以通过任何方式使用 DataView 订单属性(无需执行 sql 语句)

于 2013-01-21T18:41:17.983 回答