|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
计算机发展到这个时候,很多技术日益成熟,想学好一种技术都是不容易的,当你学会用的时候你对它的很多原理可能很不了解)datagrid|控件|数据|显现一样平常来讲,每个字段的内容会独自显现于DataGridView控件的一个数据行中。成绩是,某些字段具有大批笔墨数据,我是否是可以让该字段的内容以跨数据行的体例来显现,以便在无限的画面空间中的出现出更完全的内容呢?谜底固然是一定的。
以图表1所示的实行画面而言,「自传」字段的内容并未独自显现于一个数据行中,而是以高出数据行的体例,显现在同笔数据列之各字段内容的下方。相干程序代码列示以下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
…
…
…
privateintoldRowIndex=0;
privateconstintCUSTOM_CONTENT_HEIGHT=80;
privateDataSetmyDataSet;
privatevoidCH13_DemoForm009_Load(objectsender,EventArgse)
{
PaddingnewPadding=newPadding(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);
}
privatevoidDataGridView1_ColumnWidthChanged(objectsender,
DataGridViewColumnEventArgse)
{
this.DataGridView1.Invalidate();
}
privatevoidDataGridView1_CurrentCellChanged(objectsender,EventArgse)
{
if(oldRowIndex!=-1)
{
this.DataGridView1.InvalidateRow(oldRowIndex);
}
oldRowIndex=this.DataGridView1.CurrentCellAddress.Y;
}
privatevoidDataGridView1_RowPrePaint(objectsender,
DataGridViewRowPrePaintEventArgse)
{
e.PaintParts=e.PaintParts&(~DataGridViewPaintParts.Focus);
if((e.State&DataGridViewElementStates.Selected)==
DataGridViewElementStates.Selected)
{
RectanglerowBounds=newRectangle(
this.DataGridView1.RowHeadersWidth,e.RowBounds.Top,
this.DataGridView1.Columns.GetColumnsWidth(
DataGridViewElementStates.Visible)-
this.DataGridView1.HorizontalScrollingOffset+1,
e.RowBounds.Height);
System.Drawing.Drawing2D.LinearGradientBrushbackbrush=
newSystem.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();
}
}
}
privatevoidDataGridView1_RowPostPaint(objectsender,
DataGridViewRowPostPaintEventArgse)
{
RectanglerowBounds=newRectangle(this.DataGridView1.RowHeadersWidth,
e.RowBounds.Top,this.DataGridView1.Columns.GetColumnsWidth(
DataGridViewElementStates.Visible)-
this.DataGridView1.HorizontalScrollingOffset+1,e.RowBounds.Height);
SolidBrushforebrush=null;
try
{
if((e.State&DataGridViewElementStates.Selected)==
DataGridViewElementStates.Selected)
{
forebrush=newSolidBrush(e.InheritedRowStyle.SelectionForeColor);
}
else
{
forebrush=newSolidBrush(e.InheritedRowStyle.ForeColor);
}
Objectrecipe=
this.DataGridView1.Rows.SharedRow(e.RowIndex).Cells[4].Value;
if(!(recipe==null))
{
stringtext=recipe.ToString();
RectangletextArea=rowBounds;
RectangleFclip=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;
RectangleFoldClip=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);
}
}
privatevoidDataGridView1_RowHeightChanged(
objectsender,DataGridViewRowEventArgse)
{
intpreferredNormalContentHeight=
e.Row.GetPreferredHeight(e.Row.Index,
DataGridViewAutoSizeRowMode.AllCellsExceptHeader,true)-
e.Row.DefaultCellStyle.Padding.Bottom;
PaddingnewPadding=e.Row.DefaultCellStyle.Padding;
newPadding.Bottom=e.Row.Height-preferredNormalContentHeight;
e.Row.DefaultCellStyle.Padding=newPadding;
}
//本程序会毗连至数据来历并创建所需的DataSet工具。
privateDataSetLoadDataToDataSet()
{
//使用SqlConnectionStringBuilder工具来构建毗连字符串。
SqlConnectionStringBuildersqlStringBuilder=
newSqlConnectionStringBuilder();
sqlStringBuilder.DataSource=@"(local)SQLEXPRESS";
sqlStringBuilder.InitialCatalog="冬风商业";
sqlStringBuilder.IntegratedSecurity=true;
//创建一个数据集。
DataSetds=newDataSet();
try
{
using(SqlConnectionnorthwindConnection=
newSqlConnection(sqlStringBuilder.ConnectionString))
{
SqlCommandcmdLiming=newSqlCommand(
"SELECT姓名,员工性别,出身日期,今朝薪资,自传"+
"FROMdbo.飞狐事情室WHERE自传ISNOTNULL",
northwindConnection);
northwindConnection.Open();
using(SqlDataReaderdrLiming=cmdLiming.ExecuteReader())
{
ds.Load(
drLiming,
LoadOption.OverwriteChanges,
newstring[]{"飞狐事情室"});
}
}
}
catch(Exception)
{
MessageBox.Show(
"要可以顺遂实行本典范程序,您的盘算机必需已安装SQLServer"+
"Express,而且必需已附加了本书所附的「冬风商业」数据库。"+
"关于怎样安装SQLServerExpress,请参阅附录或相干文件申明。");
//没法毗连至SQLServer。
returnnull;
}
returnds;
}
你觉得学习.NET怎么样,我懂的少,问的可能很幼稚,见笑了啊:) |
|