当前位置: 首页 > news >正文

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月出版)。

相关文章:

  • iPhone在设置中修改应用相关项,应用会重启
  • 10月9日培训日记
  • 编译蘑菇街的teamtalk
  • HTML框架代码全集
  • 同一bundle id的应用,不同证书在设备上覆盖安装会失败
  • Xcode_6.4.dmg 官方下载地址
  • 从历史学习的 8 堂架构课
  • iOS错误报告中关于崩溃地址的分析
  • 原来Windows下面也有硬链接
  • 程序员将是人类最后一个被人工智能代替的工作
  • JNDI配置原理详解
  • 利用layer的FillRule属性生成一个空心的layer
  • 最近做的一个手机上的联网应用程序
  • Parse开源了自己的SDK
  • 微信和百度地图都不支持iOS6了
  • 《Javascript高级程序设计 (第三版)》第五章 引用类型
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • 2017前端实习生面试总结
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • Effective Java 笔记(一)
  • ES6系统学习----从Apollo Client看解构赋值
  • JavaScript实现分页效果
  • node-glob通配符
  • passportjs 源码分析
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • 模型微调
  • 你不可错过的前端面试题(一)
  • 普通函数和构造函数的区别
  • 区块链技术特点之去中心化特性
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 一起参Ember.js讨论、问答社区。
  • - 语言经验 - 《c++的高性能内存管理库tcmalloc和jemalloc》
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • 整理一些计算机基础知识!
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #Z2294. 打印树的直径
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (42)STM32——LCD显示屏实验笔记
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (solr系列:一)使用tomcat部署solr服务
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (三维重建学习)已有位姿放入colmap和3D Gaussian Splatting训练
  • (循环依赖问题)学习spring的第九天
  • (转)Windows2003安全设置/维护
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • .net redis定时_一场由fork引发的超时,让我们重新探讨了Redis的抖动问题
  • .NET Remoting Basic(10)-创建不同宿主的客户端与服务器端
  • .NET/C# 使用反射注册事件
  • .NET中使用Protobuffer 实现序列化和反序列化
  • .NET中统一的存储过程调用方法(收藏)
  • [android] 请求码和结果码的作用