`
water84222
  • 浏览: 372799 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

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

阅读更多

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

 

分享到:
评论

相关推荐

    直接在DataGridView控件中修改数据

    直接在DataGridView控件中修改数据

    vb.net DataGridView控件用法合集

    vb.net操作DataGridView控件的用法的集合,包括: 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选中行是否为新追加的...

    vb.net DataGridView控件用法,.net DataGridView控件用法,GridView用法

    在.NET框架中,DataGridView控件是一个非常重要的组件,主要用于显示和操作数据表格。它提供了丰富的功能,包括数据的浏览、编辑、排序、过滤等。在VB.NET和C#等.NET语言中,使用DataGridView控件可以方便地构建用户...

    利用datagridview控件实现批量输入数据并存入数据库

    DataGridView控件:** 它提供了类似于电子表格的功能,可以用来显示数据源中的数据。 #### 二、项目创建与配置 **1. 创建一个新的Windows Forms应用程序。** **2. 添加DataGridView控件:** 在Form上添加一个...

    DataGridView 控件显示树结构

    在Windows Forms应用开发中,`DataGridView`控件通常用于展示二维表格数据,但有时我们可能需要展示层次化的数据,也就是树形结构。本教程将详细讲解如何在Visual Studio 2012中,使用C#语言和WinForms来实现一个在`...

    控件代码C#通过修改datagridview控件来修改access数据

    本篇文章主要介绍了如何在C#应用程序中使用DataGridView控件与Microsoft Access数据库进行交互,实现数据显示及更新的功能。通过一个简单的示例程序,我们能够了解到如何加载Access数据库到DataGridView,并通过用户...

    DataGridView控件中加入ComboBox下拉列表框

    在Windows Forms开发中,`DataGridView`控件是一个非常常用的组件,用于展示表格数据。而有时候,我们希望在某些单元格中不仅能够输入文本,还能让用户从预设的选项中选择,这时就需要用到`ComboBox`控件。在本篇...

    如何利用dataGridView控件实现不同窗体之间的数据传递

    本教程将详细介绍如何利用dataGridView控件实现在不同窗体间的数据传递,以帮助开发者更好地理解和应用这一功能。 首先,我们要理解数据传递的基本概念。在多窗体应用程序中,数据传递是将一个窗体(如Form2)中的...

    基于C#的WinForm中DataGridView控件操作汇总

    在C#的WinForm中,DataGridView控件是一种常见的控件,用于显示和编辑表格数据。下面将对DataGridView控件的操作进行汇总。 一、单元格内容的操作 在DataGridView控件中,可以通过CurrentCell属性来获取当前单元格...

    winform DataGridView 合计行控件

    首先,可以在`DataGridView`的行集合中动态添加一行,设置其样式以区别于数据行,并初始化总计值为0。例如: ```csharp DataGridViewRow summaryRow = new DataGridViewRow(); summaryRow.DefaultCellStyle....

    用vb.net实现datagridview控件的使用

    其中,DataGridView控件是用于显示和操作表格数据的强大工具,尤其适用于数据绑定和数据库操作。在这个主题中,我们将深入探讨如何使用VB.NET实现DataGridView控件的使用,包括数据的读入和读出。 首先,让我们了解...

    操作DataGridView控件详解

    #### 二、DataGridView控件的操作方式 操作`DataGridView`控件主要分为两种方式: 1. **控件绑定方式**:这种方式主要是通过将控件与数据源(如`DataSet`)进行绑定,当数据源发生变化时,`DataGridView`会自动更新...

    Winfrom中 DataGridView 控件中实现实时数据加载的虚拟模式(大数据加载,内存管控,高效方式)

    在Windows Forms开发中,`DataGridView`控件是一个非常重要的组件,用于展示表格数据。当处理大量数据时,传统的数据加载方式可能导致性能下降甚至内存溢出。为了解决这个问题,`DataGridView`提供了虚拟模式,这是...

    c#中向DataGridView控件中添加合计、平均值、最大值、最小值

    在C#编程中,`DataGridView`控件是一个非常常用的数据展示工具,它允许开发者以表格形式展示数据。在处理大量数据时,为了更好地理解和分析,我们往往需要计算这些数据的统计值,比如合计、平均值、最大值和最小值。...

    一个可折叠行的DataGridView控件

    在.NET框架中,DataGridView控件是用于显示和编辑表格数据的标准控件,广泛应用于Windows Forms应用程序。然而,标准的DataGridView控件并不直接支持行的折叠和展开功能。但通过自定义扩展,我们可以实现这一特性,...

    C# datagridView 控件使用心得

    在Windows桌面应用中,datagridview控件是一个非常重要的组件,它允许用户以表格形式展示和操作数据。本篇文章将深入探讨C#中datagridview控件的使用心得,包括其基本功能、数据绑定、自定义操作和事件处理等方面。 ...

    在DataGridView控件中加入ComboBox下拉列表框的实现

    在.NET Framework的Windows Forms开发中,`DataGridView`控件是一个常用的数据展示工具,而`ComboBox`则用于提供下拉选项供用户选择。将`ComboBox`集成到`DataGridView`的某一列,可以为用户提供更加丰富的交互体验...

    DataGridView中直接保存修改的数据

    在.NET框架中,`DataGridView`控件是用于显示和编辑表格数据的强大工具。它允许用户在界面上直接查看和修改数据,这对于数据密集型应用程序来说非常有用。标题“DataGridView中直接保存修改的数据”指的是如何在用户...

    将DataGridView控件中的数据导出到Word

    在.NET框架中,Windows Forms应用程序经常使用DataGridView控件来显示和操作数据。有时,我们需要将这些数据显示在其他格式中,例如Microsoft Word文档。本篇文章将详细介绍如何实现将DataGridView控件中的数据导出...

Global site tag (gtag.js) - Google Analytics