C#自定义网格组件(DataGridView)实现数据分组、排序

2012-05-01 23:59:39|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:C#, 界面设计|来源:唯设编程网

完成了上述准备工作以后,我们就可以对设计的网格控件进行测试了,下面是一个使用IList实例的显示方法:

 private void Form1_Load(object sender, EventArgs e) {

    // invoke the outlook style
    menuSkinOutlook_Click(sender, e);

    // setup our example list of business objects
    // in this case a list of contacts
    ContactList = new ArrayList();
    ContactList.Add(new ContactInfo(1,
        "Mark",
        DateTime.Now.Subtract(TimeSpan.FromDays(2)),
        "as the world turns",
        0.54));
    ContactList.Add(new ContactInfo(2,
        "Mark",
        DateTime.Now.Subtract(TimeSpan.FromDays(8)),
        "GTST",
        0.54));
    ContactList.Add(new ContactInfo(3,
        "Piet",
        DateTime.Now.Subtract(TimeSpan.FromDays(1)),
        "Day after",
        0.35));
    ContactList.Add(new ContactInfo(4,
        "Herre",
        DateTime.Now.Subtract(TimeSpan.FromDays(17)),
        "Wodka lime",
        0.9567));
    ContactList.Add(new ContactInfo(5,
        "Ronald",
        DateTime.Now.Subtract(TimeSpan.FromDays(42)),
        "I need some coffee",
        0.54));
    ContactList.Add(new ContactInfo(6,
        "Piet",
        DateTime.Now.Subtract(TimeSpan.FromDays(167)),
        "Mr Bean",
        0.653));

    // invoke inital filling, in this case unbound data
    menuUnboundContactList_Click(sender, e);

}

下面这个函数完成了鼠标单击事件的响应,实现数据的分组和排序功能:

// this event is called when the user clicks on a cell
// in this particular case we check to see if one of the column headers
// was clicked. If so, the grid will be sorted based on the clicked column.
// Note: this handler is not implemented optimally. It is merely used for
// demonstration purposes
private void outlookGrid1_CellClick(object sender, DataGridViewCellEventArgs e) {
    if (e.RowIndex < 0 && e.ColumnIndex >= 0) {
        ListSortDirection direction = ListSortDirection.Ascending;
        if (e.ColumnIndex == prevColIndex) // reverse sort order
            direction = prevSortDirection == ListSortDirection.Descending ?
                ListSortDirection.Ascending : ListSortDirection.Descending;

        // remember the column that was clicked and in which direction is ordered
        prevColIndex = e.ColumnIndex;
        prevSortDirection = direction;

        // set the column to be grouped
        outlookGrid1.GroupTemplate.Column = outlookGrid1.Columns[e.ColumnIndex];

        //sort the grid (based on the selected view)
        switch (View) {
            case "BoundContactInfo":
                outlookGrid1.Sort(new ContactInfoComparer(e.ColumnIndex, direction));
                break;
            case "BoundCategory":
                outlookGrid1.Sort(new DataRowComparer(e.ColumnIndex, direction));
                break;
            case "BoundInvoices":
                outlookGrid1.Sort(new DataRowComparer(e.ColumnIndex, direction));
                break;
            case "BoundQuarterly":
                // this is an example of overriding the default behaviour of the
                // Group object. Instead of using the DefaultGroup behavious, we
                // use the AlphabeticGroup, so items are grouped together based on
                // their first character:
                // all items starting with A or a will be put in the same group.
                IOutlookGridGroup prevGroup = outlookGrid1.GroupTemplate;
                // execption when user pressed the customer name column
                if (e.ColumnIndex == 0) 
                {
                    // simply override the GroupTemplate to use before sorting
                    outlookGrid1.GroupTemplate = new OutlookGridAlphabeticGroup();
                    outlookGrid1.GroupTemplate.Collapsed = prevGroup.Collapsed;
                }

                // set the column to be grouped
                // this must always be done before sorting
                outlookGrid1.GroupTemplate.Column = outlookGrid1.Columns[e.ColumnIndex];

                // execute the sort, arrange and group function
                outlookGrid1.Sort(new DataRowComparer(e.ColumnIndex, direction));

                //after sorting, reset the GroupTemplate back to its default (if it was changed)
                // this is needed just for this demo. We do not want the other
                // columns to be grouped alphabetically.
                outlookGrid1.GroupTemplate = prevGroup;
                break;
            default: //UnboundContactInfo
                outlookGrid1.Sort(outlookGrid1.Columns[e.ColumnIndex], direction);
                break;
        }
    }
}

这个函数非常重要,通过不同的数据类型想排序函数Sort传入不同的比较器进行排序,其业务逻辑比较简单,在此不进行详细讨论,到此,一个完整的自定义网格组件项目便完成了,有任何补充和建议欢迎在评论区域进行讨论。

发表评论0条 】
网友评论(共?条评论)..
C#自定义网格组件(DataGridView)实现数据分组、排序