一般来说,每一个字段的内容会单独显示于datagridview控件的一个数据行中。问题是,某些字段拥有大量文字数据,我是不是能够让该字段的内容以跨数据行的方式来显示,以便在有限的画面空间中的呈现出更完整的内容呢?答案当然是肯定的。
以图表1所示的执行画面而言,「自传」字段的内容并未单独显示于一个数据行中,而是以横跨数据行的方式,显示在同笔数据列之各字段内容的下方。相关程序代码列示如下:
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.data.sqlclient;
…
…
…
private int oldrowindex = 0;
private const int custom_content_height = 80;
private dataset mydataset;
private void ch13_demoform009_load(object sender, eventargs e)
{
padding newpadding = new padding(0, 1, 0, custom_content_height);
this.datagridview1.rowtemplate.defaultcellstyle.padding = newpadding;
this.datagridview1.rowtemplate.defaultcellstyle.selectionbackcolor =
color.transparent;
this.datagridview1.rowtemplate.height += custom_content_height;
this.datagridview1.allowusertoaddrows = false;
this.datagridview1.editmode = datagridvieweditmode.editonkeystrokeorf2;
this.datagridview1.cellborderstyle = datagridviewcellborderstyle.none;
this.datagridview1.selectionmode = datagridviewselectionmode.fullrowselect;
mydataset = loaddatatodataset();
if(mydataset != null)
{
// 将 bindingsource 组件系结至数据集对象中的「飞狐工作室」数据表。
this.bindingsource1.datamember = "飞狐工作室";
this.bindingsource1.datasource = mydataset;
this.bindingsource1.allownew = false;
// 将 bindingnavigator 控件的数据来源也设定成 bindingsource 组件
//,如此一来,就可以使用 bindingnavigator 控件去导览
// datagridview 控件中的数据列。
this.bindingnavigator1.bindingsource = this.bindingsource1;
this.datagridview1.datasource = this.bindingsource1;
}
else
{
return;
}
this.datagridview1.columns[4].visible = false;
this.datagridview1.columns[0].sortmode =
datagridviewcolumnsortmode.notsortable;
this.datagridview1.columns[2].sortmode =
datagridviewcolumnsortmode.notsortable;
this.datagridview1.columns[3].sortmode =
datagridviewcolumnsortmode.notsortable;
this.datagridview1.autoresizerows(
datagridviewautosizerowsmode.allcellsexceptheaders);
}
private void datagridview1_columnwidthchanged(object sender,
datagridviewcolumneventargs e)
{
this.datagridview1.invalidate();
}
private void datagridview1_currentcellchanged(object sender, eventargs e)
{
if(oldrowindex != -1)
{
this.datagridview1.invalidaterow(oldrowindex);
}
oldrowindex = this.datagridview1.currentcelladdress.y;
}
private void datagridview1_rowprepaint(object sender,
datagridviewrowprepainteventargs e)
{
e.paintparts = e.paintparts & (~datagridviewpaintparts.focus);
if((e.state & datagridviewelementstates.selected) ==
datagridviewelementstates.selected)
{
rectangle rowbounds = new rectangle(
this.datagridview1.rowheaderswidth, e.rowbounds.top,
this.datagridview1.columns.getcolumnswidth(
datagridviewelementstates.visible) -
this.datagridview1.horizontalscrollingoffset + 1,
e.rowbounds.height);
system.drawing.drawing2d.lineargradientbrush backbrush =
new system.drawing.drawing2d.lineargradientbrush(rowbounds,
this.datagridview1.defaultcellstyle.selectionbackcolor,
e.inheritedrowstyle.forecolor,
system.drawing.drawing2d.lineargradientmode.horizontal);
try
{
e.graphics.fillrectangle(backbrush, rowbounds);
}
finally
{
backbrush.dispose();
}
}
}
private void datagridview1_rowpostpaint(object sender,
datagridviewrowpostpainteventargs e)
{
rectangle rowbounds = new rectangle(this.datagridview1.rowheaderswidth,
e.rowbounds.top, this.datagridview1.columns.getcolumnswidth(
datagridviewelementstates.visible) -
this.datagridview1.horizontalscrollingoffset + 1, e.rowbounds.height);
solidbrush forebrush = null;
try
{
if((e.state & datagridviewelementstates.selected) ==
datagridviewelementstates.selected)
{
forebrush = new solidbrush(e.inheritedrowstyle.selectionforecolor);
}
else
{
forebrush = new solidbrush(e.inheritedrowstyle.forecolor);
}
object recipe =
this.datagridview1.rows.sharedrow(e.rowindex).cells[4].value;
if(!(recipe == null))
{
string text = recipe.tostring();
rectangle textarea = rowbounds;
rectanglef clip = textarea;
textarea.x -= this.datagridview1.horizontalscrollingoffset;
textarea.width += this.datagridview1.horizontalscrollingoffset;
textarea.y += rowbounds.height - e.inheritedrowstyle.padding.bottom;
textarea.height -= rowbounds.height -
e.inheritedrowstyle.padding.bottom;
textarea.height =
(textarea.height / e.inheritedrowstyle.font.height) *
e.inheritedrowstyle.font.height;
clip.width -= this.datagridview1.rowheaderswidth + 1 - clip.x;
clip.x = this.datagridview1.rowheaderswidth + 1;
rectanglef oldclip = e.graphics.clipbounds;
e.graphics.setclip(clip);
e.graphics.drawstring(text, e.inheritedrowstyle.font,
forebrush, textarea);
e.graphics.setclip(oldclip);
}
}
finally
{
forebrush.dispose();
}
if (this.datagridview1.currentcelladdress.y == e.rowindex)
{
e.drawfocus(rowbounds, true);
}
}
private void datagridview1_rowheightchanged(
object sender, datagridviewroweventargs e)
{
int preferrednormalcontentheight =
e.row.getpreferredheight(e.row.index,
datagridviewautosizerowmode.allcellsexceptheader, true) -
e.row.defaultcellstyle.padding.bottom;
padding newpadding = e.row.defaultcellstyle.padding;
newpadding.bottom = e.row.height - preferrednormalcontentheight;
e.row.defaultcellstyle.padding = newpadding;
}
// 本程序会连接至数据来源并建立所需的 dataset 对象。
private dataset loaddatatodataset()
{
// 利用 sqlconnectionstringbuilder 对象来构建连接字符串。
sqlconnectionstringbuilder sqlstringbuilder =
new sqlconnectionstringbuilder();
sqlstringbuilder.datasource = @"(local)/sqlexpress";
sqlstringbuilder.initialcatalog = "北风贸易";
sqlstringbuilder.integratedsecurity = true;
// 建立一个数据集。
dataset ds = new dataset();
try
{
using (sqlconnection northwindconnection =
new sqlconnection(sqlstringbuilder.connectionstring))
{
sqlcommand cmdliming = new sqlcommand(
"select 姓名,员工性别,出生日期, 目前薪资, 自传" +
" from dbo.飞狐工作室 where 自传 is not null",
northwindconnection);
northwindconnection.open();
using (sqldatareader drliming = cmdliming.executereader())
{
ds.load(
drliming,
loadoption.overwritechanges,
new string[] { "飞狐工作室" });
}
}
}
catch (exception)
{
messagebox.show(
"要能够顺利执行本范例程序,您的计算机必须已安装 sql server " +
"express,并且必须已附加了本书所附的「北风贸易」数据库。" +
"关于如何安装 sql server express,请参阅附录或相关文件说明。");
// 无法连接至 sql server。
return null;
}
return ds;
}
新闻热点
疑难解答