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 - 蚂蚁 - 安宁](http://img0.ph.126.net/_Ywa4oQUWwDetpauRQ16fQ==/1285777693631564520.jpg)