DataGridView的类第三部分

DataGridView的类第三部分

DataGridView 使用经验

///
/// 是否由本类代理用户删除操作,默认是不代理,如果代理,则相应的事件将不发给用户处理
///
public bool AutoProcessDeleted = false;

    /// <summary>
    /// 删除数据总数
    /// </summary>
    int m_nDeletedRows = 0;

    /// <summary>
    /// 当前删除选择
    /// </summary>
    bool m_bLastSelectedAction = false;

    /// <summary>
    /// 计数
    /// </summary>
    bool m_bCanSave = false;

    protected override void OnUserDeletingRow(DataGridViewRowCancelEventArgs e)
    {
        if (AutoProcessDeleted == false)
        {
            base.OnUserDeletingRow(e);
            return;
        }

        if (m_nDeletedRows == 0)
        {
            m_bLastSelectedAction = false;
            m_nDeletedRows = SelectedRows.Count;

            if (ShowDelMsg != null)
                m_bLastSelectedAction = ShowDelMsg(); //可以在这个调用里显示是否删除信息
        }

        e.Cancel = m_bLastSelectedAction;
        m_nDeletedRows--;

        m_bCanSave = false;
        if (m_nDeletedRows == 0 && m_bLastSelectedAction == false)
            m_bCanSave = true;

        if (m_bLastSelectedAction == false && WillDeletting != null)
        {
            if (WillDeletting(e.Row.Cells[0].Value.ToString()) == false)
            {
                e.Cancel = true; //如果当前记录删除出现错误,则本条数据不再删除
            }
        }
    }

    protected override void OnUserDeletedRow(DataGridViewRowEventArgs e)
    {
        if (AutoProcessDeleted == false)
        {
            base.OnUserDeletedRow(e);
            return;
        }

        if (m_bCanSave)
        {
            if (SaveDataTable != null)
                SaveDataTable();

            m_bCanSave = false;
        }
    }

    public void SelectedRowByGuid(string p_strGuid, bool p_bIsSingle = true)
    {
        int nLing = 0;

        bool bShowAlready = true;

        for (int i = 0; i < Rows.Count; i++)
        {
            if (Rows[i].Cells[0].Value.ToString() == p_strGuid)
            {
                if (nLing == 0) //确定第一行
                    nLing = i;
                Rows[i].Selected = true;
                bShowAlready = Rows[i].Displayed;
                if (p_bIsSingle == false)
                    break;
            }
            else if (p_bIsSingle)
                Rows[i].Selected = false;
        }

        if (bShowAlready == false)
            FirstDisplayedScrollingRowIndex = nLing;
    }

    protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
    {
        if (e.RowIndex >= 0 && e.ColumnIndex == -1)
        {
            Rectangle newRect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Height, e.CellBounds.Height);

            using (Brush gridBrush = new SolidBrush(GridColor),
                backColorBrush = Rows[e.RowIndex].Selected ? new SolidBrush(e.CellStyle.SelectionBackColor) : new SolidBrush(e.CellStyle.BackColor))
            {
                using (Pen gridLinePen = new Pen(gridBrush, 2))
                {
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                    //划线
                    Point p1 = new Point(e.CellBounds.Left + e.CellBounds.Width, e.CellBounds.Top);
                    Point p2 = new Point(e.CellBounds.Left + e.CellBounds.Width, e.CellBounds.Top + e.CellBounds.Height);
                    Point p3 = new Point(e.CellBounds.Left, e.CellBounds.Top + e.CellBounds.Height);
                    Point p4 = new Point(e.CellBounds.Left, e.CellBounds.Top);
                    Point[] ps = new Point[] { p1, p2, p3, p4 };
                    e.Graphics.DrawLines(gridLinePen, ps);

                    //画字符串
                    e.Graphics.DrawString(string.Format("{0}", e.Value),
                        e.CellStyle.Font, Brushes.AntiqueWhite, e.CellBounds.Left + 5, e.CellBounds.Top + 2, StringFormat.GenericDefault);
                    e.Handled = true;
                }
            }
        }
        //else if(e.RowIndex == Rows.Count - 1 && e.ColumnIndex != -1)
        //{
        //    Rectangle newRect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Height, e.CellBounds.Height);

        //    using (Brush gridBrush = new SolidBrush(GridColor), blackBrush = new SolidBrush(BaseClass.ColorSets.BackColor),
        //        backColorBrush = Rows[e.RowIndex].Selected ? new SolidBrush(e.CellStyle.SelectionBackColor) : new SolidBrush(e.CellStyle.BackColor))
        //    {
        //        using (Pen gridLinePen = new Pen(gridBrush, 2), bottomLine = new Pen(blackBrush))
        //        {
        //            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

        //            //划线
        //            Point p1 = new Point(e.CellBounds.Left + e.CellBounds.Width, e.CellBounds.Top);
        //            Point p2 = new Point(e.CellBounds.Left + e.CellBounds.Width, e.CellBounds.Top + e.CellBounds.Height);
        //            Point p3 = new Point(e.CellBounds.Left, e.CellBounds.Top + e.CellBounds.Height);
        //            Point p4 = new Point(e.CellBounds.Left, e.CellBounds.Top);
        //            Point[] ps = new Point[] { p1, p2 };
        //            e.Graphics.DrawLines(gridLinePen, ps);

        //            ps = new Point[] { p2, p3 };
        //            e.Graphics.DrawLines(bottomLine, ps);

        //            //画字符串
        //            e.Graphics.DrawString(string.Format("{0}", e.Value),
        //                e.CellStyle.Font, Brushes.AntiqueWhite, e.CellBounds.Left + 2, e.CellBounds.Top + 2, StringFormat.GenericDefault);
        //            e.Handled = true;
        //        }
        //    }
        //}

        base.OnCellPainting(e);
    }

    /// <summary>
    /// 隐藏指定GUID的行
    /// </summary>
    /// <param name="p_strGuid"></param>
    public void HideRow(string p_strGuid)
    {
        try
        {
            for (int i = 0; i < Rows.Count; i++)
            {
                if (Rows[i].Cells[0].Value.ToString() == p_strGuid)
                {
                    CurrencyManager cm = (CurrencyManager)BindingContext[DataSource]; //隐藏前先将数据脱离,否则会报错

                    cm.SuspendBinding();
                    Rows[i].Visible = false;
                    cm.ResumeBinding();

                    return;
                }
            }
        }
        catch (Exception ex)
        {
            if (ex != null)
            {

            }
        }
    }

    /// <summary>
    /// 隐藏指定GUID的行
    /// </summary>
    /// <param name="p_strGuid"></param>
    public void HideRow(int p_nRowIndex)
    {
        try
        {
            CurrencyManager cm = (CurrencyManager)BindingContext[DataSource]; //隐藏前先将数据脱离,否则会报错

            cm.SuspendBinding();
            Rows[p_nRowIndex].Visible = false;
            cm.ResumeBinding();
        }
        catch (Exception ex)
        {
            if (ex != null)
            {

            }
        }
    }

    public void SetStyle()
    {
        colorEnter = BaseClass.ColorSets.SelectionBackColor;
        colorLeave = BaseClass.ColorSets.LeaveBackColor;

        EnableHeadersVisualStyles = false;
        AllowUserToAddRows = false;
        AllowUserToResizeRows = false;
        BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        BackgroundColor = BaseClass.ColorSets.BackColor;
        ClipboardCopyMode = DataGridViewClipboardCopyMode.Disable;
        ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
        ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
        ColumnHeadersHeight = 25;
        GridColor = BaseClass.ColorSets.BackColor;
        RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
        if (RowHeadersWidth == 41)
            RowHeadersWidth = 23;
        RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
        SelectionMode = DataGridViewSelectionMode.FullRowSelect;

        DataGridViewCellStyle cStyle = new DataGridViewCellStyle();
        cStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        cStyle.WrapMode = DataGridViewTriState.True;
        cStyle.BackColor = BaseClass.ColorSets.HeaderBackColor;
        cStyle.Font = BaseClass.ColorSets.TextFont;
        cStyle.ForeColor = BaseClass.ColorSets.HeaderForceColor;
        ColumnHeadersDefaultCellStyle = cStyle;

        DataGridViewCellStyle dStyle = new DataGridViewCellStyle();
        //dStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
        dStyle.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
        dStyle.WrapMode = DataGridViewTriState.False;
        dStyle.BackColor = BaseClass.ColorSets.NormalBackColor;
        dStyle.Font = BaseClass.ColorSets.TextFont;
        dStyle.ForeColor = BaseClass.ColorSets.NormalForceColor;
        dStyle.SelectionBackColor = colorLeave;

        RowTemplate.DefaultCellStyle = dStyle;

        RowsDefaultCellStyle = dStyle;

        DefaultCellStyle = dStyle;
        AlternatingRowsDefaultCellStyle = dStyle;

        DataGridViewCellStyle dRowHeaderStyle = new DataGridViewCellStyle();
        dRowHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
        dRowHeaderStyle.Padding = new System.Windows.Forms.Padding(1);
        dRowHeaderStyle.WrapMode = DataGridViewTriState.False;
        dRowHeaderStyle.BackColor = BaseClass.ColorSets.NormalBackColor;
        dRowHeaderStyle.Font = BaseClass.ColorSets.TextFont;
        dRowHeaderStyle.ForeColor = BaseClass.ColorSets.NormalForceColor;
        dRowHeaderStyle.SelectionBackColor = colorLeave;

        RowHeadersDefaultCellStyle = dRowHeaderStyle;
    }
}

}

​​​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值