65.9K
CodeProject 正在变化。 阅读更多。
Home

DataGrid格式化

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.57/5 (29投票s)

2004 年 8 月 16 日

CPOL

5分钟阅读

viewsIcon

242914

downloadIcon

3930

在 WinForm DataGrid 控件中应用 DataGridTableStyle 类。

写作意图

从 .NET Beta 到 .NET Ver 1.1,微软并没有为 DataGrid 及其数据内容提供非常用户友好的格式化功能。但在下一个版本中,他们将引入一个新的网格控件,称为 GridView 控件。

在我经常访问的主要讨论区,例如微软讨论区和 Code Project 讨论区,我看到很多关于 DataGrid 格式化的问题。回复所有或多或少都指向相同疑问的帖子,只是有细微差别,这是一项耗时的工作。因此,这就是我写这篇文章的实际灵感。我们可以将其作为适用于 DataGrid 控件的各个格式化阶段的参考。

在开始之前,我非常乐意邀请您就本文未涵盖的 DataGrid 格式化提出您的想法和建议。这将有助于我在后续阶段更新文章内容,并帮助该讨论区的其他成员。

“如果您觉得它不错,请不要忘记投票并提出您的建议。”

引言

在进入实际应用之前,我需要稍微解释一下这篇文章。如果您正在寻找可以直接应用到项目中的 3D 渲染或其他内容,那么这篇文章将无法帮助您。它将帮助您处理您正在进行或将要进行的不同 Windows 项目。

在这里,我使用了一个名为 Card 的表,该表是在 Sybase 数据库的帮助下设计的。您可以自己创建 Card 表。我的目的是展示如何在 DataGrid 中使用不同的数据类型及其格式化。仅此而已。

这是表的结构。

字段名 数据类型 描述
card_no 数值 卡号。
card_type char 卡类型
card_id varchar 卡标识
card_balance decimal 卡余额
card_valid_sd 时间戳 卡有效期开始日期
card_valid_ed date 卡有效期结束日期
card_valid_time 时间 卡有效期时间

在这里,您可能会提出一个疑问:时间戳、日期和时间数据类型中有什么?

请看下面给出的图片,并注意区别。图片比文字更有用。

因此,我的表中包含不同类型的数据。接下来,我需要以不同的方式显示它们。(有关数据绑定技术,请参阅附加的代码。)

默认情况下,如果您不对 DataGrid 应用任何样式,它将像这样显示网格和内容

告诉我,在这种情况下您会怎么做

  1. 这里所有记录的对齐方式都是左对齐。但将数字记录左对齐不是一个好习惯。它们应该始终右对齐。
  2. 如果您想将某些字段值居中对齐,您会怎么做?
  3. 现在,在现有场景下,我们可以轻松地更改选中行的颜色。但它不会改变选中单元格的颜色(至少在我看来是这样)。假设您想让选中单元格显示不同的颜色方案。
  4. 如果您查看上面网格的标题(例如 card_id、card_type 等),它与我们在表中定义的相同。我的问题是:“这个标题对用户有意义吗?”根据我的经验,“没有”。因为我们需要为这个标题提供有意义的标题,如卡号、卡类型等。这样用户才会觉得它很友好。
  5. 如果您查看上表,字段标题和内容之间没有任何样式差异。在当前场景下,我们可以更改 DataGrid 的标题样式和字体。假设您想更改字段名称的标题样式和字体样式。您会怎么做?

以上提及的是显示数据的基本标准。它可能会根据您的喜好而变化。看看这个

这张图片为上述五个问题提供了最终解决方案。

我将解释它是如何从先前的状态变为上述新状态的。如果您将 DataSet 的表与 DataGrid 绑定,它会以类似网格的形式显示数据。如果您更深入地查看,您会发现 DataGrid 的样式呈现有点像表格结构。在 .NET 中,我们有一个名为 DataGridTableStyle 的类文件,它仅表示绘制的网格。DataGridTableStyle 严格表示在控件中绘制的网格。因此,通过这个类(DataGridTableStyle)的对象,您可以控制 DataSet 中每个数据表的网格外观。(您将在附加的 zip 文件中找到完整的源代码。)

我们的目标是

  • 创建一个 DataGridTableStyle 类实例。
  • 应用我们所需的格式样式。
  • 使用 DataGridTableStyle 类中的 MappingName 属性指定需要应用样式的表。简单来说,您需要在此处绑定表名。
  • 最后,使用 Add() 方法将此 DataGridTableStyle 对象添加到您的 DataGridTableStyle 属性中。

这是我们实际代码块中的一小部分。这只是简单的格式化。像这样,您可以做很多事情。所以,抓住概念并根据您的需求应用。

private void radioButton1_Click(object sender, System.EventArgs e)
{
    /*For fast capturing , i put this code block 
    little bit simple. So it look like lengthy.

    This will create an instance of DataGridTableStyle class*/
    DcStyle=new DataGridTableStyle();

    /*this will help you to assign the forecolor,backcolor and 
    the font style of your feild header.*/
    DcStyle.HeaderForeColor=Color.WhiteSmoke;
    DcStyle.HeaderBackColor=Color.Green;
    DcStyle.HeaderFont= new Font("Verdana",10,FontStyle.Bold);

    /*this foreach loop will iterate through the entire
    collection of your datatable's columns.*/
    foreach(DataColumn dc in Dset.Tables["Card"].Columns)
    {
      /* like above this will help you to define formatting to 
      your feild values. which is bounding to DataGridTextBoxColumn
      using the Mappingname and HeaderText.*/
      DataGridTextBoxColumn textColumn=new DataGridTextBoxColumn();
      textColumn.MappingName= dc.ColumnName;
      textColumn.HeaderText = dc.Caption;

      if(dc.ColumnName.ToString()=="card_no")
      {
        /*this will change the caption of your 
        datagrid Column's default value*/

        /*to the below mwntioned value.*/
        textColumn.HeaderText="Card No";
        /*this will help you to align the content for you.*/
        textColumn.Alignment=System.Windows.Forms.HorizontalAlignment.Right;
        /*this will help you set the back color of selected cell*/
        textColumn.TextBox.BackColor=Color.Red;
        /*this will help you to adjest the width of Column's*/
        textColumn.Width=75;
      }

      //continued ........................

      /*this will help you from updating data content.*/
      textColumn.ReadOnly = true;
      /*finally we will add our formated column 
      to DataGridTableStyle instance*/
      DcStyle.GridColumnStyles.Add(textColumn);
    }
    /*Here you need to map the name of the table
    to mappingName property of datadridtablestyle instance */
    DcStyle.MappingName=Dset.Tables["Card"].TableName;
    /*then add your datadridtablestyle instance to
    datagrid TableStyles collection using Add() method.*/
    dataGrid1.TableStyles.Add(DcStyle);
    /*our normal databinding*/
    dataGrid1.DataSource=Dset.Tables["Card"].DefaultView;
}

您可以将不同的样式添加到 DataGridTableStyle 集合中。同样,您可以删除或清除 DataGrid 的表样式。这将帮助您为 DataGrid 保留默认样式。这段代码本身就具有一定的解释性。所以我不打算详细解释。

privatevoid radioButton2_CheckedChanged_1(object sender, System.EventArgs e)
{
    //this block will help you to simply clear the existing 
    //style of datagrid and will help you to keep 
    //its default style.
    DataGridTableStyle DefaultStyle =new DataGridTableStyle();
    dataGrid1.TableStyles.Clear ();
    DefaultStyle.HeaderBackColor = Color.Red;
    DefaultStyle.AlternatingBackColor = Color.LightGray;
    dataGrid1.TableStyles.Add(DefaultStyle);
}

我的最后讨论是关于数据格式化。假设我们需要更改来自数据库表的数据的格式。您可以在此处解决此问题。

(您将在 zip 文件中找到完整的代码。)

//this will help you to show datarow in alternate color
DcStyle1.AlternatingBackColor=Color.Gold;

//this will help you to show data in currency form
textColumn.Format=System.String.Format("c",textColumn.TextBox);
//this will format and show the date in yyyy-MMM-dd format
textColumn.Format=System.String.Format("yyyy-MMM-dd",textColumn.TextBox);

//this will format and show the date in dd-MMM-yyyy format
textColumn.Format=System.String.Format("dd-MMM-yyyy",textColumn.TextBox);

//this will help you to show the time information excluding the date.
textColumn.Format=System.String.Format("hh:mm:ss",textColumn.TextBox);

下载并执行源文件。请提出您的想法。

© . All rights reserved.