`
izuoyan
  • 浏览: 8915614 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Visual C# 2005 - 如何于DataGridView控件中以跨数据行方式显示数据

阅读更多
图表1
一般来说,每一个字段的内容会单独显示于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;
}
建议阅读书籍:《Visual C# 2005文件IO与数据存取秘诀》(机械工业出版社,预计2006年12月出版)。
分享到:
评论

相关推荐

    DataGridView 控件显示树结构

    开发工具:Vs2012 语言c# winform 源码可以调试,运行。在DataGridView中显示树形结构。

    C#在DataGridView及TreeView中显示数据

    C#在DataGridView及TreeView中显示数据,定义一个二维数组,数组中的每一行代表DataGridView中的一条记录,当按下鼠标左键时,首先获取选定行,记录每一行对应的信息,当鼠标进入TreeView控件时,触发的操作,判断...

    C#编程资源,datagridview编程控件详细使用

    VS2005的各种控件编程代码下载。datagridview编程控件详细使用

    C#通过DataSet将数据绑定到DataGridView显示

    C#在Winform窗体程序中通过DataSet对象将数据绑定到DataGridView中来显示,DataGridView是用来显示数据库中内容较方便的一个数据显示控件,对C#初学者来说,了解其用法显得很重要,也很实用,通过本程序的代码演示,...

    C#操作数据库和DataGridview源码 CoperationdatabaseandData.rar

    分别提供ado.net和ef的写法和DatagridView控件的使用。 二、菜单功能 1、查询、用户信息、列表信息、新增数据、更新选中行、删除选中行等。 三、注意事项 1、开发环境为Visual Studio 2012,数据库为Sqlsever2008...

    Visual C# 2005编程技巧大全 源码

    全书内容涵盖了大量的Visual C# 2005 编程新技术和新理念,包括ToolStrip控件应用、泛型应用、注册表管理、WMI管理规范使用、XML文件处理、多线程处理、程序异常处理、文件压缩及解压缩、文件加密解密、文件访问权限...

    Visual+C#+2008程序设计经典案例设计与实现.rar

    案例1 利用DataGridView控件显示数据库信息 案例2 数据库数据记录单 案例3 利用下拉列表框动态查询数据库信息 案例4 利用ListView控件导航数据库信息 案例5 带有数据库的会员登录系统 案例6 动态添加数据库数据信息...

    Visual C# 2008程序设计经典案例设计与实现

    案例1 利用DataGridView控件显示数据库信息 案例2 数据库数据记录单 案例3 利用下拉列表框动态查询数据库信息 案例4 利用ListView控件导航数据库信息 案例5 带有数据库的会员登录系统 案例6 动态添加数据库数据...

    Visual C# 2005程序设计自学手册 随书源码第一部分(共三部)

    Visual C# 2005 程序设计自学手册 *****是随书源码光盘***** *****人民邮电出版社***** **长春明日科技组织编写** 本书从初学者角度出发,通过通俗易懂的语言和大量生动典型的实例,由浅入深、循序渐进地介绍使用...

    C#从DataGridView中追加内容到Treeview控件中

    摘要:C#源码,控件类库,TreeView C#从DataGridView中追加内容到Treeview控件中,同是这个实例源码也告诉大家,如何在Visual C#环境中使用TreeView控件和DataGridView控件,这两个控件都是十分常用的控件,所以还是有...

    visual C# 2005 实例

    DataGridViewEdit Windows中的数据显示控件实现编辑的案例。 DataGridViewRowCol 读取数据控件中的行和列的案例。 DataGridViewSample 数据视图使用案例。 DataGridViewWebService 数据视图与...

    Visual C# 2008程序设计经典案例设计与实现第四章源码

    案例1 利用DataGridView控件显示数据库信息 案例2 数据库数据记录单 案例3 利用下拉列表框动态查询数据库信息 案例4 利用ListView控件导航数据库信息 案例5 带有数据库的会员登录系统 案例6 动态添加数据库数据...

    C#禁用DataGridView控件列表头自动排序功能

    摘要:C#源码,数据库应用,DataGridView Visual C#禁用DataGridView控件列表头自动排序功能,需要连接数据库以演示数据,为DataGridView控件填充数据源,不让表头自动排序,只需禁止就行了,本源码实例教你如何禁止这...

    VS.NET 2005 C#webprintpreview打印预览控件

    对Visual Studio环境下各种表格控件信息进行快速的打印预览,主要支持Visual Studio环境下的C/S结构应用程序表格控件有DataGridView控件、DataGrid控件、支持B/S结构应用程序表格控件有GridView控件、DataGrid控件。...

    C#中改变DataGridView控件边框颜色的方法

    DataGridView是Visual Studio中一个最重要的数据控件。它可以应用在大多数场合,功能强大,使用灵活。本文要重点介绍一下,如果设置DataGridView的边框颜色。 比尔盖次说“Apple机上没有哪一个软件我是觉得应该是...

    C#开发实例大全(基础卷).软件开发技术联盟(带详细书签) PDF 下载

    主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和集合的...DataGridView数据控件、自定义用户控件、文件基本操作、文件夹基本操作、文件流操作、加密、解密及解压缩文件、C#与Word互操作、高效应用...

    明日科技《C#示例源代码》(9-12)

    实例021 从DataGridView控件中拖放数据到TreeView控件 78 第3章 图形图像及多媒体应用 实例022 生成中文验证码 86 实例023 生成图片缩略图 88 实例024 不失真压缩图片 90 实例025 批量图像格式转换 ...

Global site tag (gtag.js) - Google Analytics