星期三, 5月 10, 2017

[C#] DataGridView - 指定欄位進行排序

這篇 MSDN 文章 - 如何:自訂 Windows Form DataGridView 控制項的排序 的第一個範例:程式設計排序,基本上是把 DataGridViewColumn.SortMode = DataGridViewColumnSortMode.Automatic 自動排序,變成寫 Code 指定排序,範例是把排序功能寫在 BtnSort 內執行,故意把 Code 寫在 DataGridView.ColumnHeaderMouseClick Event 上,操作上會比較直覺

MSDN 說明
使用的資料行 SortMode 屬性設定為 DataGridViewColumnSortMode.Automatic, 、 SortedColumn 和 SortOrder 屬性會自動設定,並顯示適當的排序圖像。 使用的資料行 SortMode 屬性設定為 DataGridViewColumnSortMode.Programmatic, ,您必須顯示排序圖像自行透過 DataGridViewColumnHeaderCell.SortGlyphDirection 屬性。
Layout
  • DataGridView:這次主角
  • TextBox:用來顯示 DataGridView.SortedColumn 和 DataGridView.SortOrder 資料
  • Button:MSDN 範例排序 Code
[C#] DataGridView - 指定欄位進行排序-1

Code
namespace SimpleOrder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.MultiSelect = false;
            dataGridView1.ColumnHeaderMouseClick += DataGridView1_ColumnHeaderMouseClick;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PopulateDataGridView();
            ShowSortInfo();
        }

        private void DataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var col = dataGridView1.Columns[e.ColumnIndex];

            ListSortDirection direction;
            if (dataGridView1.SortOrder == (SortOrder.Descending | SortOrder.None))
            {
                direction = ListSortDirection.Ascending;
            }
            else
            {
                direction = ListSortDirection.Descending;
            }

            // Sort 只能指定單欄位排序
            dataGridView1.Sort(col, direction);

            // SortGlyphDirection 就是 Header 那個排序的三角符號
            col.HeaderCell.SortGlyphDirection =
                direction == ListSortDirection.Ascending ?
                SortOrder.Ascending : SortOrder.Descending;

            ShowSortInfo();
        }

        // Populates the DataGridView.
        // Replace this with your own code to populate the DataGridView.
        public void PopulateDataGridView()
        {
            // DataGridViewSelectionMode.ColumnHeaderSelect 時,DataGridViewColumn.SortMode 不能是 Auto
            dataGridView1.SelectionMode =
DataGridViewSelectionMode.ColumnHeaderSelect;

            // 設定 DataGridViewTextBoxColumn
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
            {
                HeaderText = "姓",
                Name = "ColLastName",
                SortMode = DataGridViewColumnSortMode.Programmatic
            });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
            {
                HeaderText = "都市",
                Name = "ColCity",
                SortMode = DataGridViewColumnSortMode.Programmatic
            });

            // DataGridView 資料
            dataGridView1.Rows.Add("Parker", "Seattle");
            dataGridView1.Rows.Add("Watson", "Seattle");
            dataGridView1.Rows.Add("Osborn", "New York");
            dataGridView1.Rows.Add("Jameson", "New York");
            dataGridView1.Rows.Add("Brock", "New Jersey");
        }

        private void ShowSortInfo()
        {
            var sb = new StringBuilder();
            sb.AppendLine(dataGridView1.SortedColumn == null ? "目前無排序欄位" : $"排序欄位:{dataGridView1.SortedColumn.Name}");
            sb.AppendLine($"排序模式:{dataGridView1.SortOrder}");
                
            txtSortInfo.Text = string.Empty;
            txtSortInfo.Text = sb.ToString();
        }

        // 文章內的 MSDN 範例排序 Code
        private void BtnSort_Click(object sender, EventArgs e)
        {
            // Check which column is selected, otherwise set NewColumn to null.
            DataGridViewColumn newColumn =
                dataGridView1.Columns.GetColumnCount(
                DataGridViewElementStates.Selected) == 1 ?
                dataGridView1.SelectedColumns[0] : null;

            DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
            ListSortDirection direction;

            // If oldColumn is null, then the DataGridView is not currently sorted.
            if (oldColumn != null)
            {
                // Sort the same column again, reversing the SortOrder.
                if (oldColumn == newColumn &&
                    dataGridView1.SortOrder == SortOrder.Ascending)
                {
                    direction = ListSortDirection.Descending;
                }
                else
                {
                    // Sort a new column and remove the old SortGlyph.
                    direction = ListSortDirection.Ascending;
                    oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
                }
            }
            else
            {
                direction = ListSortDirection.Ascending;
            }

            // If no column has been selected, display an error dialog  box.
            if (newColumn == null)
            {
                MessageBox.Show("Select a single column and try again.",
                    "Error: Invalid Selection", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
            else
            {
                dataGridView1.Sort(newColumn, direction);

                newColumn.HeaderCell.SortGlyphDirection =
                    direction == ListSortDirection.Ascending ?
                    SortOrder.Ascending : SortOrder.Descending;
            }

            ShowSortInfo();
        }
    }
}
DataGridView.Sort 方法 (DataGridViewColumn, ListSortDirection) 只能針對單一欄位進行排序喔

[C#] DataGridView - 指定欄位進行排序-2

沒有留言:

張貼留言