当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 如何于DataGridView控件中以跨数据行方式显示数据

ASP.NET
使用CodeDom来生成.cs文件
在.NET中定义结构设计标准
c#v2.0 扩展特性 翻译2
.NET框架下的自动内存管理
在设计期跟踪代码 .NET
VB新發現
原来Smart Client 是这样的
flash内嵌于C#程序中的应用
解读.NET Framework中的COM+与MTS
获取Sql服务器列表 C#
.Net Framework Beta 2 初步介绍
DLL的应用。
编译自己的资源文件编辑器reseditor.exe
.NET 2.0 基础类库中的范型:其他范型类
.NET 2.0 基础类库中的范型:Functional Programming
Lion.Web.UpLoadModule 1.1.2004.0720 大文件上传带进度显示组件DOTNET
.NET编程规范
web.config配置文件示例
编程实现QQ表情文件CFC格式
关于线程的参数、“返回值”、及线程的中止

ASP.NET 中的 如何于DataGridView控件中以跨数据行方式显示数据


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 90 ::
收藏到网摘: n/a

 

一般来说,每一个字段的内容会单独显示于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(oldRowInd