关闭

DataGridViewCheckColumn 添加 Text

650人阅读 评论(0) 收藏 举报
分类:

 public class DataGridViewCheckAndTextColumn : DataGridViewCheckBoxColumn
    {
       public DataGridViewCheckAndTextColumn()
       {
           this.CellTemplate = new DataGridViewCheckAndTextCell();
       }

    }

------------------------------------------------------------------------------------------------------------------------------------

 public  class DataGridViewCheckAndTextCell : DataGridViewCheckBoxCell

    {
        public DataGridViewCheckAndTextCell()
        {
            this.Enabled = true;
        }

        private bool enabled;
        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
                this.ReadOnly = !enabled;
            }
        }

        private string text;
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        private System.Drawing.Color color;
        public System.Drawing.Color Color
        {
            get
            {
                return color;
            }
            set { color = value; }
        }

        private System.Drawing.Font font;
        public System.Drawing.Font Font
        {
            get { return font; }
            set { font = value; }
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,
                      int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue,
                      string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
                      DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value,
                formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            if (this.Font == null)
                this.Font = cellStyle.Font;

            if (!this.Enabled)
                this.Color = Color.Gray;
            else if (this.Color.IsEmpty)
                this.Color = cellStyle.ForeColor;
 
            Rectangle contentBounds = this.GetContentBounds(rowIndex);

            SizeF ForeSize = graphics.MeasureString(this.Text, this.Font);

            Point stringLocation = new Point();
            stringLocation.Y = cellBounds.Y + 4;

            if (contentBounds.Right + 2 + ForeSize.Width > cellBounds.Width)
            {
                stringLocation.X = cellBounds.X + contentBounds.Right + 2;
            }
            else
            {
                stringLocation.X = cellBounds.X + cellBounds.Width - (int)ForeSize.Width  - 4;
            }
      
            graphics.DrawString(this.Text, this.Font, new SolidBrush(this.Color), stringLocation);
        }

    }

----------------------------------------------------------------------------------------------------------------------

    DataGridViewCheckAndTextColumn NewJuryoColumn = new DataGridViewCheckAndTextColumn();
            NewJuryoColumn.Name = "zhongliang" ;
            NewJuryoColumn.HeaderText = 新重量;
            NewJuryoColumn.Width = 100;
            NewJuryoColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
            NewJuryoColumn.DefaultCellStyle.Font = new System.Drawing.Font("MS 明朝", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));          

           NewJuryoColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            DGVform.Columns.Add(NewJuryoColumn);

---------------------------------------------------------------------------------------------------------------------------------

 

            dataGridView1.Rows.Add();
            index = dataGridView1.Rows.Count - 1;
              dr = dataGridView1.Rows[index];           

            DataGridViewCheckAndTextCell ctCell = dr.Cells["zhongliang"] as DataGridViewCheckAndTextCell;
            ctCell.Enabled = true;
            ctCell.Text = "500";

  ctCell.Value = false;

DataGridViewCheckColumn  添加 Text - 蚂蚁 - 安宁
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5968次
    • 积分:82
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:9篇
    • 译文:0篇
    • 评论:0条