• DatagridView


    DatagridView

    绑定数据: 

    绑定列表用BindingList 可以自动刷新

    使用其他列表,每次改变列表时候先把DataSource置为null  在对DataSource赋值列表,也可以实现刷新,不然只有第一次会自动刷新,以后就不会刷新

    绑定列表,如果不手动定义列名称可以直接绑定列表,列名称为list中类型的属性名(必须为属性值,字段是不能绑定的),

    要实现只绑定类型中的部分属性需要设置DatagridView的AutoGenerateColumns属性为false

    通过绑定imaglist 和imageindex可以直接在DatagridView中显示图片

    更改BinglingList部分元素 是用UPdata 手动刷新

    单元格重绘实现合并单元格功能

      加载CellPainting事件  

      事件函数中重绘单元格,根据选择的Datagridview的显示方式可能需要微调,选中单元格的效果需要重绘,绑定数据改变时,手动updata下。

     if (e.RowIndex>=0)//除去标题
                {
                    Rectangle rect = e.CellBounds;
                    RectangleF rect1 = new RectangleF(rect.X+2, rect.Y + 2, rect.Width - 4, rect.Height - 4);
                    using (Brush backcolor = new SolidBrush(dataGridView1.BackgroundColor),linebrush=new SolidBrush(dataGridView1.GridColor))
                    {
                       e.Graphics.FillRectangle(backcolor, e.CellBounds);
                        using (Pen linepen = new Pen(linebrush))
                        {
                            #region
                            //横线
                            if (e.RowIndex > 0)
                            {
                                if (e.Value.ToString() != dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString())
                                {
                                    e.Graphics.DrawLine(linepen, rect.Left, rect.Top, rect.Right, rect.Top);
                                }
                            }
                            if (e.RowIndex == dataGridView1.Rows.Count-1)//最后一行下横线
                            {
                                e.Graphics.DrawLine(linepen, rect.Left, rect.Bottom, rect.Right, rect.Bottom);
                            }
                            //竖线
                            e.Graphics.DrawLine(linepen, rect.Left, rect.Top, rect.Left, rect.Bottom);
                            e.Graphics.DrawLine(linepen, rect.Right, rect.Top, rect.Right, rect.Bottom);
                            if (e.ColumnIndex == dataGridView1.Columns.Count - 1)//最后一列右边竖线
                            {
                                e.Graphics.DrawLine(linepen, rect.Right-1, rect.Top, rect.Right-1, rect.Bottom);
                            }
                        }
                            #endregion
                            #region
                        if (e.Value != null)
                        {
                            if (e.RowIndex > 0)
                            {
                                if (e.Value.ToString() != dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString())
                                {
                                    e.Graphics.DrawString(e.Value.ToString(), dataGridView1.Font, Brushes.Black, rect1, StringFormat.GenericDefault);
                                }
                            }
                            else
                            {
                                e.Graphics.DrawString(e.Value.ToString(), dataGridView1.Font, Brushes.Black, rect1, StringFormat.GenericDefault);
                            }
                        }
                        #endregion
                    }
                    e.Handled = true;
                }

     网上看有需求定制cell这个就比较麻烦,可以自己定义一种cell类型继承自DataGridViewCell,然后实现相关内容,这个就比较复杂了,这里做了一个简单的demo,网上看到有人说要一个cell放两个按钮,且不说这个需求是否合理先实现看看,放一个按钮是可以直接用fcl自带的类型的,两个好像没办法,就自己画一个,这里只是简单思路,实际使用需要优化

    class twobuttoncell : DataGridViewCell
            {
                public twobuttoncell():base()
                {
                    
                }
                public override Type FormattedValueType
                {
                    get
                    {
                        Type valueType = base.ValueType;
                        if (valueType != null)
                        {
                            return valueType;
                        }
                        return typeof(String);
                    } 
                }
                Rectangle lb;
                Rectangle rb;
                protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
                {
                    //base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
    
                    lb = new Rectangle(new Point(cellBounds.X+2,cellBounds.Y+2), new Size(cellBounds.Width / 2-4, cellBounds.Height-4));
                    rb = new Rectangle(new Point(cellBounds.X+cellBounds.Width/2+2,cellBounds.Y+2), new Size(cellBounds.Width / 2-4, cellBounds.Height-4));
                    graphics.FillRectangle(Brushes.White, cellBounds);
                    graphics.DrawRectangle(new Pen(cellStyle.ForeColor), lb);
                    graphics.DrawString("left", cellStyle.Font, Brushes.Red, lb);
                    graphics.DrawRectangle(new Pen(cellStyle.ForeColor), rb);
                    graphics.DrawString("right", cellStyle.Font, Brushes.Red, rb);
    
                }
                protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
                {
                    //base.OnMouseClick(e);
                    var rec = this.DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
                    Point pt = new Point(rec.Location.X+e.X,rec.Y+e.Y);
    
                    if (lb.Contains(pt))
                    {
                        MessageBox.Show("left");
                    }
                    if (rb.Contains(pt))
                    {
                        MessageBox.Show("right");
                    }
                }
            }
  • 相关阅读:
    Ubuntu 14.04还能看的过去的字体
    Android 65536问题
    Ubuntu 下如何设置 Java 环境变量
    rime 必知必會
    基于ParserCombinator的正则表达式引擎
    Spring源码分析——Configuration配置类解析流程
    Spring源码分析——AnnotationConfigApplicationContext组件注册流程
    在SpringBoot中实现策略模式
    ByxOrm——模仿MyBatis的轻量级ORM框架
    ByxContainerAnnotation——基于注解的轻量级IOC容器
  • 原文地址:https://www.cnblogs.com/onegarden/p/4799309.html
Copyright © 2020-2023  润新知