想在datagridview 里的一列添加顯示多個checkbox
如圖:
通過重繪 已經可以顯示多個chekbox ,現在問題是1、縮小拉伸列,checkbox跑出界了,哪位大神知道要怎修改,讓它拉伸列的時候讓chekbox變小或者藏在列里。問題2.怎樣為每個checkbox添加點擊事件
protected override void Paint(System.Drawing.Graphics graphics,
System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds,
int rowIndex, DataGridViewElementStates cellState, object value,
object formattedValue, string errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
var uu = this.ColumnIndex;
if (uu == 4)
{
var ChecName = value;
if (value !=null) {
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
cellState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, paintParts);
Rectangle textRectangleValue = new Rectangle(); if (!this.ShowOriginal)
{
// 繪出底色
if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
{
SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}
// 繪出邊框
if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
}
System.Drawing.Font myFont = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
SizeF vSizeF = graphics.MeasureString("新增", myFont);
int dStrLength = Convert.ToInt32(Math.Ceiling(vSizeF.Width));
textRectangleValue.X = 0;
for (int j=0;j<5;j++) {
Size size = CheckBoxRenderer.GetGlyphSize(graphics, state);
Point center = new Point(cellBounds.X+ +15+ dStrLength*j, cellBounds.Y);
center.X += 3;
center.Y += (cellBounds.Height - size.Height) / 2;
textRectangleValue.X = center.X+15;
textRectangleValue.Y = cellBounds.Y;
textRectangleValue.Width = cellBounds.Width - CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal).Width;
textRectangleValue.Height = cellBounds.Height
- (cellBounds.Height - CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal).Width) / 2 - 1;
state = CheckBoxState.UncheckedNormal;
CheckBoxRenderer.DrawCheckBox(graphics, center, textRectangleValue, "ggg", SystemFonts.DefaultFont, TextFormatFlags.Bottom, false, state);
}
uj5u.com熱心網友回復:
你都做到這程度了,再發散下思維就差不多了,給你個思路:自定義個用戶控制元件,用戶控制元件再放個固定大小的panel,用戶控制元件屬性帶上系結的行資料,把checkbox都丟到這個用戶控制元件內的panel上,事件也能隨便來;;
datagridview就不用一個一個checkbox添加了;;添加用戶控制元件就行,,,然后就是列寬變動時(重繪?)改動用戶控制元件的大小;
uj5u.com熱心網友回復:
這個拿wpf好實作,用這個,太難了uj5u.com熱心網友回復:
又看了下你的代碼,你這種重繪方法不合適,只是畫了出來,沒毛用;需要實體化控制元件,再將控制元件指定到相應位置
uj5u.com熱心網友回復:
之前也有用戶控制元件,但是改變列寬的時候,不知道怎改變控制元件大小uj5u.com熱心網友回復:
實作一個DataGridViewCheckboxListColumn和DataGridViewCheckBoxListCell。
uj5u.com熱心網友回復:
之前也有用戶控制元件,但是改變列寬的時候,不知道怎改變控制元件大小 你都做到這程度了,再發散下思維就差不多了,給你個思路:
自定義個用戶控制元件,用戶控制元件再放個固定大小的panel,用戶控制元件屬性帶上系結的行資料,把checkbox都丟到這個用戶控制元件內的panel上,事件也能隨便來;;
datagridview就不用一個一個checkbox添加了;;添加用戶控制元件就行,,,然后就是列寬變動時(重繪?)改動用戶控制元件的大小;
實作一個DataGridViewCheckboxListColumn和DataGridViewCheckBoxListCell。
uj5u.com熱心網友回復:
參考:https://blog.csdn.net/C_gyl/article/details/85095021轉載請註明出處,本文鏈接:https://www.uj5u.com/net/11480.html
標籤:C#
