DataGridView自定义排序

.net自带DataGridView排序貌似最终都是用DataGridView.Sort(....),发现当是用排序后,DataGridView将把排序前的行样式等重置,仔细测试后觉得貌似是排序时DataGridView重新绑定了数据源,具体不得而知......

 

实在没有办法破解,我也不想重新写比较来排序,用了最土的办法,希望有人能给出正确的排序方法,使行样式排序后还在

 

protected override void OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
        {
            base.OnColumnHeaderMouseClick(e);
            #region//自定义编程排序方式
            if (e.Button == MouseButtons.Left && this.Columns[e.ColumnIndex].SortMode == DataGridViewColumnSortMode.Programmatic)
            {
                // 检查已经排序的列
                DataGridViewColumn newColumn = this.Columns[e.ColumnIndex];
                DataGridViewColumn oldColumn = this.SortedColumn;
                ListSortDirection direction;

                // 判断排序方式
                if (oldColumn != null)
                {
                    if (oldColumn == newColumn &&
                        this.SortOrder == SortOrder.Ascending)
                    {
                        direction = ListSortDirection.Descending;
                    }
                    else
                    {
                        direction = ListSortDirection.Ascending;
                        oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
                    }
                }
                else
                {
                    direction = ListSortDirection.Ascending;
                }

                // 排序
                if (newColumn != null)
                {
                    SortedList<string, DataGridViewCellStyle> st1 = new SortedList<string, DataGridViewCellStyle>();
                    SortedList<string, int> rowheight = new SortedList<string, int>();
                    SortedList<string, bool> rowSelected = new SortedList<string, bool>();
                    foreach (DataGridViewRow row in this.Rows)
                    {
                        string guid = null;
                        foreach (DataGridViewColumn col in this.Columns)
                        {
                            guid += row.Cells[col.Name].Value.ToString();
                        }
                        st1.Add(guid, row.DefaultCellStyle.Clone());
                        rowheight.Add(guid, row.Height);
                        rowSelected.Add(guid, row.Selected);
                    }

                    this.Sort(newColumn, direction);
                    //图标
                    newColumn.HeaderCell.SortGlyphDirection =
                        direction == ListSortDirection.Ascending ?
                        SortOrder.Ascending : SortOrder.Descending;
                    //重新绑定外观
                    //this.DefaultCellStyle = st0;
                    foreach (DataGridViewRow row in this.Rows)
                    {
                        string guid = null;
                        foreach (DataGridViewColumn col in this.Columns)
                        {
                            guid += row.Cells[col.Name].Value.ToString();
                        }

                        row.DefaultCellStyle = st1[guid];
                        row.Height = rowheight[guid];
                        row.Selected = rowSelected[guid];
                    }
                }
            }
            #endregion
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值