解决DataGridView常见问题.docx
- 文档编号:3252235
- 上传时间:2022-11-21
- 格式:DOCX
- 页数:30
- 大小:29.24KB
解决DataGridView常见问题.docx
《解决DataGridView常见问题.docx》由会员分享,可在线阅读,更多相关《解决DataGridView常见问题.docx(30页珍藏版)》请在冰豆网上搜索。
解决DataGridView常见问题
解决DataGridView常见问题<源代码>
2007-09-1622:
03:
19| 分类:
.Net| 标签:
|字号大中小 订阅
1. 如何使指定的单元格不可编辑?
ReadOnly属性决定了单元格中的数据是否可以编辑,可以设置单元格的ReadOnly属性,也可以设置DataGridViewRow.ReadOnly或DataGridViewColumn.ReadOnly使得一行或一列所包含的单元格都是只读的。
默认情况下,如果一行或一列是只读的,那么其包含的单元格也会使只读的。
不过你仍可以操作一个只读的单元格,比如选中它,将其设置为当前单元格,但用户不能修改单元格的内容。
注意,即使单元格通过ReadOnly属性设置为只读,仍然可以通过编程的方式修改它,另外ReadOnly也不会影响用户是否可以删除行。
2. 如何让一个单元格不可用(disable)?
单元格可以设置为只读而不可编辑,但DataGridView却没提供使单元格不可用的支持。
一般意义上,不可用意味着用户不能进行操作,通常会带有外观的暗示,如灰色。
没有一种简单的方法来创建那种不可操作的单元格,但提供一个暗示性的外观告诉用户某单元格不可用还是可行的。
内置的单元格类型没有进行不可用设置的属性,下面的例子扩展了DataGridViewButtonCell,参照常见控件的Enabled属性,为其添加了Enabled属性,如果该属性设置为false,那么其外观状态将类似于普通按钮的不可用状态。
publicclassDataGridViewDisableButtonColumn:
DataGridViewButtonColumn
{
publicDataGridViewDisableButtonColumn()
{
this.CellTemplate=newDataGridViewDisableButtonCell();
}
}
publicclassDataGridViewDisableButtonCell:
DataGridViewButtonCell
{
privateboolenabledValue;
publicboolEnabled
{
get{
returnenabledValue;
}
set{
enabledValue=value;
}
}
//OverridetheClonemethodsothattheEnabledpropertyiscopied.
publicoverrideobjectClone()
{
DataGridViewDisableButtonCellcell=
(DataGridViewDisableButtonCell)base.Clone();
cell.Enabled=this.Enabled;
returncell;
}
//Bydefault,enablethebuttoncell.
publicDataGridViewDisableButtonCell()
{
this.enabledValue=true;
}
protectedoverridevoidPaint(Graphicsgraphics,
RectangleclipBounds,RectanglecellBounds,introwIndex,
DataGridViewElementStateselementState,objectvalue,
objectformattedValue,stringerrorText,
DataGridViewCellStylecellStyle,
DataGridViewAdvancedBorderStyleadvancedBorderStyle,
DataGridViewPaintPartspaintParts)
{
//Thebuttoncellisdisabled,sopainttheborder,
//background,anddisabledbuttonforthecell.
if(!
this.enabledValue)
{
//Drawthecellbackground,ifspecified.
if((paintParts&DataGridViewPaintParts.Background)==
DataGridViewPaintParts.Background)
{
SolidBrushcellBackground=
newSolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground,cellBounds);
cellBackground.Dispose();
}
//Drawthecellborders,ifspecified.
if((paintParts&DataGridViewPaintParts.Border)==
DataGridViewPaintParts.Border)
{
PaintBorder(graphics,clipBounds,cellBounds,cellStyle,
advancedBorderStyle);
}
//Calculatetheareainwhichtodrawthebutton.
RectanglebuttonArea=cellBounds;
RectanglebuttonAdjustment=
this.BorderWidths(advancedBorderStyle);
buttonArea.X+=buttonAdjustment.X;
buttonArea.Y+=buttonAdjustment.Y;
buttonArea.Height-=buttonAdjustment.Height;
buttonArea.Width-=buttonAdjustment.Width;
//Drawthedisabledbutton.
ButtonRenderer.DrawButton(graphics,buttonArea,
PushButtonState.Disabled);
//Drawthedisabledbuttontext.
if(this.FormattedValueisString)
{
TextRenderer.DrawText(graphics,
(string)this.FormattedValue,
this.DataGridView.Font,
buttonArea,SystemColors.GrayText);
}
}
else
{
//Thebuttoncellisenabled,soletthebaseclass
//handlethepainting.
base.Paint(graphics,clipBounds,cellBounds,rowIndex,
elementState,value,formattedValue,errorText,
cellStyle,advancedBorderStyle,paintParts);
}
}
}
3. 如何避免用户将焦点设置到指定的单元格?
默认情况下DataGridView的操作(navigation)模型在限制用户将焦点置于指定的单元格方面没有提供任何支持。
你可以实现自己的操作逻辑,这需要重写合适的键盘、导航、鼠标方法,如DataGridView.OnKeyDown,DataGridView.ProcessDataGridViewKey,DataGridView.SetCurrentCellAddressCore,DataGridView.SetSelectedCellCore,DataGridView.OnMouseDown。
4. 如何使所有单元格总是显示控件(不论它是否处于编辑状态)?
DataGridView控件只支持在单元格处于编辑状态时显示真实的控件(如TextBox)。
DataGridView没有被设计为显示多控件或为每行重复显示控件。
DataGridView在单元格不被编辑时为其绘制对应控件的外观,该外观可能是你想要的。
例如,DataGridViewButtonCell类型的单元格,不管它是否处于编辑状态,总是表现为一个按钮。
6. 如何在单元格内同时显示图标和文本?
DataGridView控件没有对在同一单元格内同时显示图标和文本提供支持。
但通过实现自定义的绘制事件,如CellPaint事件,你可以轻松实现这个效果。
下面这段代码扩展了DataGridViewTextBoxColumn和DataGridViewTextBoxCell类,将一个图片显示在文本旁边。
这个示例使用了DataGridViewCellStyle.Padding属性来调整文本的位置,重写了Paint方法来绘制图片。
该示例可以得到简化,方法是处理CellPainting事件,在这里实现类似的功能。
publicclassTextAndImageColumn:
DataGridViewTextBoxColumn
{
privateImageimageValue;
privateSizeimageSize;
publicTextAndImageColumn()
{
this.CellTemplate=newTextAndImageCell();
}
publicoverrideobjectClone()
{
TextAndImageColumnc=base.Clone()asTextAndImageColumn;
c.imageValue=this.imageValue;
c.imageSize=this.imageSize;
returnc;
}
publicImageImage
{
get{returnthis.imageValue;}
set
{
if(this.Image!
=value){
this.imageValue=value;
this.imageSize=value.Size;
if(this.InheritedStyle!
=null){
PaddinginheritedPadding=this.InheritedStyle.Padding;
this.DefaultCellStyle.Padding=newPadding(imageSize.Width,
inheritedPadding.Top,inheritedPadding.Right,
inheritedPadding.Bottom);
}
}
}
}
privateTextAndImageCellTextAndImageCellTemplate
{
get{returnthis.CellTemplateasTextAndImageCell;}
}
internalSizeImageSize
{
get{returnimageSize;}
}
}
publicclass
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 解决 DataGridView 常见问题