DataGridVeiw的类的第二部分

DataGridVeiw的类的第二部分

DataGridView 使用经验

public System.Collections.IComparer m_ICompareAscending = null, m_ICompareDescending = null;

    //如果不是绑定表时,可以自定义排序时,用下面的类。
    private class RowCompare : System.Collections.IComparer
    {
        private static int sortOrderModifier = 1;

        public RowCompare(SortOrder sortOrder)
        {
            if (sortOrder == SortOrder.Descending)
            {
                sortOrderModifier = -1;
            }
            else if (sortOrder == SortOrder.Ascending)
            {
                sortOrderModifier = 1;
            }
        }

        public int Compare(object x, object y)
        {
            DataGridViewRow DataGridViewRow1 = (DataGridViewRow)x;
            DataGridViewRow DataGridViewRow2 = (DataGridViewRow)y;

            // Try to sort based on the Last Name column.
            int CompareResult = System.String.Compare(
                DataGridViewRow1.Cells[1].Value.ToString(),
                DataGridViewRow2.Cells[1].Value.ToString());

            // If the Last Names are equal, sort based on the First Name.
            if (CompareResult == 0)
            {
                CompareResult = System.String.Compare(
                    DataGridViewRow1.Cells[0].Value.ToString(),
                    DataGridViewRow2.Cells[0].Value.ToString());
            }
            return CompareResult * sortOrderModifier;
        }
    }

    //protected override void OnSorted(EventArgs e)
    //{
    //    if (fristRow.Count > 0)
    //    {
    //        //DataTable dt = ((DataTable)DataSource);
    //        //DataView dv = dt.DefaultView;
    //        //dv.Sort = dt.Columns[colindex].ColumnName;
    //        //dt = dv.ToTable();
    //        //DataRow dr = dt.NewRow();
    //        //dr.ItemArray = fristRow[0];
    //        //dt.Rows.InsertAt(dr, 0);
    //        //fristRow.Clear();
    //        //DataSource = dt;
    //    }

    //    base.OnSorted(e);
    //}

    //List<object[]> fristRow = new List<object[]>();

    protected override void OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
    {
        base.OnColumnHeaderMouseClick(e);

        if (m_bStopSort == false || Columns[e.ColumnIndex].DataPropertyName == null || Columns[e.ColumnIndex].DataPropertyName.Length == 0)
            return;

        if (e.RowIndex >= 0 || Rows.Count == 0)
            return;

        //记住选择的行
        SaveGridVeiwSelectedRow();

        switch (SortOrder)
        {
            case System.Windows.Forms.SortOrder.None:
                if (m_ICompareAscending == null || m_ICompareDescending == null)
                    Sort(Columns[e.ColumnIndex], ListSortDirection.Ascending);
                else
                    Sort(m_ICompareAscending);

                Columns[e.ColumnIndex].SortMode = DataGridViewColumnSortMode.Programmatic;

                Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                break;
            case System.Windows.Forms.SortOrder.Ascending:
                if (m_ICompareAscending == null || m_ICompareDescending == null)
                    Sort(Columns[e.ColumnIndex], ListSortDirection.Descending);
                else
                    Sort(m_ICompareDescending);
                Columns[e.ColumnIndex].SortMode = DataGridViewColumnSortMode.Programmatic;
                break;
            case System.Windows.Forms.SortOrder.Descending:
                if (DataSource != null)
                {
                    Type t = DataSource.GetType();

                    if (t.FullName == "System.Windows.Forms.BindingSource")
                    {
                        ((BindingSource)DataSource).Sort = "";
                    }
                    else if (t.FullName == "System.Data.DataTable")
                        ((DataTable)DataSource).DefaultView.Sort = "";
                }
                break;
        }

        RestoreGridViewSelectedRow();

        FouceGrid();

        //if (fristRow.Count > 0)
        //{
        //    DataTable dt = ((DataTable)DataSource);
        //    DataRow dr = dt.NewRow();
        //    dr.ItemArray = fristRow[0];
        //    if (SortOrder == System.Windows.Forms.SortOrder.Ascending)
        //    {
        //        dt.Rows.Add(dr);
        //        dt = dt.DefaultView.ToTable();
        //        DataSource = dt;

        //        Columns[e.ColumnIndex].SortMode = DataGridViewColumnSortMode.Programmatic;
        //        Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
        //        //SortOrder = System.Windows.Forms.SortOrder.Ascending;
        //    }
        //    else
        //        dt.Rows.InsertAt(dr, 0);
        //    fristRow.Clear();
        //}

    }


    / <summary>
    / 高亮的行的GUID
    / </summary>
    //List<string> m_listHiLine = new List<string>();

    / <summary>
    / 高亮行的单元设置
    / </summary>
    //DataGridViewCellStyle m_styleHiLine = null;

    protected override void OnEnter(EventArgs e)
    {
        for (int i = 0; i < Rows.Count; i++)
        {
            DataGridViewCellStyle cs = Rows[i].DefaultCellStyle;
            DataGridViewRowHeaderCell rs = Rows[i].HeaderCell;

            if (m_listHiLine.Contains(Rows[i].Cells[0].Value.ToString()))
            {
                cs = m_styleHiLine;
            }

            cs.SelectionBackColor = colorEnter;
            Rows[i].DefaultCellStyle = cs;

            Rows[i].HeaderCell.Style = cs;
        }

        base.OnEnter(e);

        //for (int i = 0; i < SelectedRows.Count; i++)
        //{
        //    DataGridViewCellStyle cs = SelectedRows[i].DefaultCellStyle;
        //    cs.SelectionBackColor = colorEnter;
        //    SelectedRows[i].DefaultCellStyle = cs;

        //    DataGridViewRowHeaderCell rs = SelectedRows[i].HeaderCell;
        //    rs.Style.SelectionBackColor = colorEnter;
        //    SelectedRows[i].HeaderCell = rs;
        //}

    }

    public void FouceGrid()
    {
        OnEnter(null);
    }

    protected override void OnLeave(EventArgs e)
    {
        base.OnLeave(e);

        //for (int i = 0; i < SelectedRows.Count; i++)
        //{
        //    DataGridViewCellStyle cs = SelectedRows[i].InheritedStyle;
        //    cs.SelectionBackColor = colorLeave;
        //    SelectedRows[i].DefaultCellStyle = cs;

        //    DataGridViewRowHeaderCell rs = SelectedRows[i].HeaderCell;
        //    rs.Style.SelectionBackColor = colorLeave;
        //    SelectedRows[i].HeaderCell = rs;
        //}
        for (int i = 0; i < Rows.Count; i++)
        {
            DataGridViewCellStyle cs = Rows[i].InheritedStyle;
            cs.SelectionBackColor = colorLeave;
            Rows[i].DefaultCellStyle = cs;

            DataGridViewRowHeaderCell rs = Rows[i].HeaderCell;
            rs.Style.SelectionBackColor = colorLeave;
            Rows[i].HeaderCell = rs;
        }
    }

    /// <summary>
    /// 鼠标被按下,检查哪行被选中了,调用只触发一次事件
    /// </summary>
    /// <param name="e"></param>
    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e); //基础处理中会处理选中行,但是点击空位置则不会处理,下面补上一个点空白位置清除所有选中行的逻辑

        HitTestInfo test = HitTest(e.X, e.Y);

        if (test.RowIndex == -1 && test.ColumnIndex == -1 && e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            for (int i = 0; i < Rows.Count; i++)
                Rows[i].Selected = false;
        }

        if (e.Button == System.Windows.Forms.MouseButtons.Right) //如果支持右键选择
        {
            //如果右击的位置没有选中的记录,则把当前的位置做为选中记录,否则什么也不需要做
            bool bFind = false;
            if (test.RowIndex != -1)
            {
                for (int i = 0; i < SelectedRows.Count; i++)
                {
                    if (SelectedRows[i].Index == test.RowIndex)
                    {
                        bFind = true;
                        break;
                    }
                }
            }

            if (bFind == false)
            {
                if (SelectedRows.Count > 0) //如果有被选中的记录,则先清除掉选中的数据,没有不需要,可以加快速度
                {
                    for (int i = 0; i < Rows.Count; i++)
                        Rows[i].Selected = false;
                }

                if (test.RowIndex != -1)
                {
                    Rows[test.RowIndex].Selected = true;
                }
            }
        }

        if (SelectionChangedNew != null)
            SelectionChangedNew(this, new EventArgs());
    }

    protected override void OnSelectionChanged(EventArgs e)
    {
        //如果没有鼠标按下,画面初始化自动选中时,也触发一下
        if (SelectionChangedNew != null && (SelectedRows.Count > 1)) //发生多选时,调用新的行选择事件
            SelectionChangedNew(this, new EventArgs());

        base.OnSelectionChanged(e);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值