网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.asp.net 2.0中用GRIDVIEW插入新记.
.在 ASP.NET 中使用计时器(Timer).
.ASP.NET中用healthMonitor属性用.
.ASP.NET2.0下含有DropDownList的.
.P/Invoke能够在asp.net中使用.
.在.net中使用GDI+来提高gif图片的.
.ASP.NET 2.0 AJAX中Webservice调.
.关于EXCEL数据上传到SQL SERVER中.
.ASP.NET中水晶报表的使用.
.在Asp.net中为图像加入版权信息.
.如何于DataGridView控件中以跨数.
.101个微软提供的Visual Studio 2.
.ASP.NET应用程序中调用EJB.
.在.NET中,将竖表变横表(支持固定.
."ASP.NET AJAX RC1(疑似缺陷)"及.
.asp.net 2.0 权限树的控制.
.ASP.NET中数据校验部分的封装与应.
.asp.net 2.0中一次性更新所有GRI.
..net回收机制的使用.
.ASP.NET创建Web服务之使用事务.

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

发表日期:2006-10-11


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

上一篇:asp.net 2.0中不同web控件之间的相互调用 人气:4853
下一篇:图片地址防盗链,通过IHttpHandler实现 人气:7179
浏览全部DataGridView的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐