一般来说,每一个字段的内容会单独显示于DataGridView控件的一个数据行中。问题是,某些字段拥有大量文字数据,我是不是能够让该字段的内容以跨数据行的方式来显示,以便在有限的画面空间中的呈现出更完整的内容呢?答案当然是肯定的。 private void CH13_DemoForm009_Load(object sender, EventArgs e) this.DataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor = this.DataGridView1.RowTemplate.Height += CUSTOM_CONTENT_HEIGHT; this.DataGridView1.AllowUserToAddRows = false; myDataSet = LoadDataToDataSet(); if(myDataSet != null) this.BindingSource1.AllowNew = false; // 将 BindingNavigator 控件的数据来源也设定成 BindingSource 组件 this.DataGridView1.DataSource = this.BindingSource1; this.DataGridView1.Columns[4].Visible = false; this.DataGridView1.Columns[0].SortMode = private void DataGridView1_ColumnWidthChanged(object sender, private void DataGridView1_CurrentCellChanged(object sender, EventArgs e) oldRowIndex = this.DataGridView1.CurrentCellAddress.Y; private void DataGridView1_RowPrePaint(object sender, if((e.State & DataGridViewElementStates.Selected) == System.Drawing.Drawing2D.LinearGradientBrush backbrush = try private void DataGridView1_RowPostPaint(object sender, SolidBrush forebrush = null; try Object recipe = if(!(recipe == null)) textArea.X -= this.DataGridView1.HorizontalScrollingOffset; e.Graphics.SetClip(clip); e.Graphics.DrawString(text, e.InheritedRowStyle.Font, e.Graphics.SetClip(oldClip); if (this.DataGridView1.CurrentCellAddress.Y == e.RowIndex) private void DataGridView1_RowHeightChanged( Padding newPadding = e.Row.DefaultCellStyle.Padding; // 本程序会连接至数据来源并建立所需的 DataSet 对象。 sqlStringBuilder.DataSource = @”(local)\SQLEXPRESS”; // 建立一个数据集。 try northwindConnection.Open(); using (SqlDataReader drLiming = cmdLiming.ExecuteReader()) // 无法连接至 SQL Server。 return ds;
以图表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;
{
Padding newPadding = new Padding(0, 1, 0, CUSTOM_CONTENT_HEIGHT);
this.DataGridView1.RowTemplate.DefaultCellStyle.Padding = newPadding;
Color.Transparent;
this.DataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
this.DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
this.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
{
// 将 BindingSource 组件系结至数据集对象中的「飞狐工作室」数据表。
this.BindingSource1.DataMember = “飞狐工作室”;
this.BindingSource1.DataSource = myDataSet;
//,如此一来,就可以使用 BindingNavigator 控件去导览
// DataGridView 控件中的数据列。
this.BindingNavigator1.BindingSource = this.BindingSource1;
}
else
{
return;
}
DataGridViewColumnSortMode.NotSortable;
this.DataGridView1.Columns[2].SortMode =
DataGridViewColumnSortMode.NotSortable;
this.DataGridView1.Columns[3].SortMode =
DataGridViewColumnSortMode.NotSortable;
this.DataGridView1.AutoResizeRows(
DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders);
}
DataGridViewColumnEventArgs e)
{
this.DataGridView1.Invalidate();
}
{
if(oldRowIndex != -1)
{
this.DataGridView1.InvalidateRow(oldRowIndex);
}
}
DataGridViewRowPrePaintEventArgs e)
{
e.PaintParts = e.PaintParts & (~DataGridViewPaintParts.Focus);
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);
new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
this.DataGridView1.DefaultCellStyle.SelectionBackColor,
e.InheritedRowStyle.ForeColor,
System.Drawing.Drawing2D.LinearGradientMode.Horizontal);
{
e.Graphics.FillRectangle(backbrush, rowBounds);
}
finally
{
backbrush.Dispose();
}
}
}
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);
{
if((e.State & DataGridViewElementStates.Selected) ==
DataGridViewElementStates.Selected)
{
forebrush = new SolidBrush(e.InheritedRowStyle.SelectionForeColor);
}
else
{
forebrush = new SolidBrush(e.InheritedRowStyle.ForeColor);
}
this.DataGridView1.Rows.SharedRow(e.RowIndex).Cells[4].Value;
{
string text = recipe.ToString();
Rectangle textArea = rowBounds;
RectangleF clip = textArea;
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;
forebrush, textArea);
}
}
finally
{
forebrush.Dispose();
}
{
e.DrawFocus(rowBounds, true);
}
}
object sender, DataGridViewRowEventArgs e)
{
int preferredNormalContentHeight =
e.Row.GetPreferredHeight(e.Row.Index,
DataGridViewAutoSizeRowMode.AllCellsExceptHeader, true) –
e.Row.DefaultCellStyle.Padding.Bottom;
newPadding.Bottom = e.Row.Height – preferredNormalContentHeight;
e.Row.DefaultCellStyle.Padding = newPadding;
}
private DataSet LoadDataToDataSet()
{
// 利用 SqlConnectionStringBuilder 对象来构建连接字符串。
SqlConnectionStringBuilder sqlStringBuilder =
new SqlConnectionStringBuilder();
sqlStringBuilder.InitialCatalog = “北风贸易”;
sqlStringBuilder.IntegratedSecurity = true;
DataSet ds = new DataSet();
{
using (SqlConnection northwindConnection =
new SqlConnection(sqlStringBuilder.ConnectionString))
{
SqlCommand cmdLiming = new SqlCommand(
“SELECT 姓名,员工性别,出生日期, 目前薪资, 自传” +
” FROM dbo.飞狐工作室 WHERE 自传 IS NOT NULL”,
northwindConnection);
{
ds.Load(
drLiming,
LoadOption.OverwriteChanges,
new string[] { “飞狐工作室” });
}
}
}
catch (Exception)
{
MessageBox.Show(
“要能够顺利执行本范例程序,您的计算机必须已安装 SQL Server ” +
“Express,并且必须已附加了本书所附的「北风贸易」数据库。” +
“关于如何安装 SQL Server Express,请参阅附录或相关文件说明。”);
return null;
}
}
如何于datagridview控件中以跨数据行方式显示数据_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何于datagridview控件中以跨数据行方式显示数据_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧