定制DataGrid的风格

转载 2004年09月21日 14:06:00

Customizing .NET Windows Form DataGrid's

More Information on installing the .Net Framework click here.
Download
examples from this Article.


DataGrid Table Styles

The Windows Form DataGrid control displays data in the form of a grid. The DataGridTableStyle is a class that represents the drawn grid only. This grid should not be confused with the DataTable class, which is a possible source of data for the grid. Instead, the DataGridTableStyle strictly represents the grid as it is painted in the control. Therefore, through the DataGridTableStyle you can control the appearance of the grid for each DataTable. To specify which DataGridTableStyle is used when displaying data from a particular DataTable, set the MappingName to the TableName of a DataTable.

The GridTableStylesCollection contains all the DataGridTableStyle objects used by a System.Windows.Forms.DataGrid control. The collection can contain as many DataGridTableStyle objects as you need, however the MappingName of each must be unique. At run time, this allows you to substitute a different DataGridTableStyle for the same data, depending on the user's preference.

DataGrid Column Styles

The DataGrid control automatically creates a collection of DataGridColumnStyle objects for you when you set the DataSource property to an appropriate data source. The objects created actually are instances of one of the following classes that inherit from DataGridColumnStyle: DataGridBoolColumn or DataGridTextBoxColumn class.

You can also create your own set of DataGridColumnStyle objects and add them to the GridColumnStylesCollection. When you do so, you must set the MappingName of each column style to the ColumnName of a DataColumn to synchronize the display of columns with the actual data.

Example

The following example creates two Table Styles. one for the customer table, the other for the orders table. Then the own Column Styles are used to format the columns.

dotnet_table_style.gif

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;

public class CustomGridStyles : System.Windows.Forms.Form
{
   private Button btnShowOrders;
   private DataGrid myDataGrid;
   private DataSet myDataSet;
   private Button btnShowCustomers;
   private bool TablesAlreadyAdded;

   public CustomGridStyles()
   {
      InitializeComponent();

      // Get Customers and Orders from the Database
      MakeDataSet();

      // Show the Customers Table withot custom Table Style
      myDataGrid.DataSource = myDataSet.Tables["Customers"];
   }

....
....


   public static void Main()
   {
      Application.Run(new CustomGridStyles());
   }

   // Show Orders Table with custom Table/Column Style
   protected void btnShowOrders_Click(object sender, System.EventArgs e)
   {
      if (!TablesAlreadyAdded)
      {
         AddCustomDataTableStyle();
      }
      myDataGrid.SetDataBinding(myDataSet, "Orders");
      myDataGrid.CaptionText = "Formatted Orders Table";
   }


   // Show Customers Table with custom Table/Column Style
   private void btnShowCustomers_Click(object sender, System.EventArgs e)
   {
      if (!TablesAlreadyAdded)
      {
         AddCustomDataTableStyle();
      }
      myDataGrid.SetDataBinding(myDataSet, "Customers");
      myDataGrid.CaptionText = "Formatted Customers Table";
   }


   // Create own Table/Column Styles
   private void AddCustomDataTableStyle()
   {
      // Create the first DataGridTableStyle which is used
      // for the "Customers" Table

      DataGridTableStyle ts1 = new DataGridTableStyle();
      ts1.MappingName = "Customers";

      // Set other properties.
      ts1.AlternatingBackColor = Color.LightBlue;

      // Create a first column style and set its MappingName
      // to the "CustomerID" column of the "Customers" Table.
      // Set the HeaderText and Width properties. Well I know
      // for the "CustomerID" Column it makes not much sense to
      // set a DataGridBoolColumn ... it's just a Demo.

      DataGridColumnStyle boolCol = new DataGridBoolColumn();
      boolCol.MappingName = "CustomerID";
      boolCol.HeaderText = "ID";
      boolCol.Width = 50;
      ts1.GridColumnStyles.Add(boolCol);

      // Add a second column style and set its MappingName
      // to the "CompanyName" column of the "Customers" Table.

      DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
      TextCol.MappingName = "CompanyName";
      TextCol.HeaderText = "Company";
      TextCol.Width = 250;
      ts1.GridColumnStyles.Add(TextCol);

      // Create the second DataGridTableStyle which is used
      // for the "Orders" Table.

      DataGridTableStyle ts2 = new DataGridTableStyle();
      ts2.MappingName = "Orders";

      // Set other properties.
      ts2.AlternatingBackColor = Color.LightGray;

      // Use a PropertyDescriptor to create a formatted
      // column. First get the PropertyDescriptorCollection
      // for the data source and data member

      PropertyDescriptorCollection pcol = this.BindingContext
         [myDataSet,"Orders"].GetItemProperties();

      // Create a first formatted column style and set its MappingName
      // to the OrderDate" column of the "Orders" Table.
      // Format the DateTime as Long Format "F"

      DataGridColumnStyle cOrderDate =
            new DataGridTextBoxColumn(pcol["OrderDate"], "F", true);
      cOrderDate.MappingName = "OrderDate";
      cOrderDate.HeaderText = "Formatted Order Date";
      cOrderDate.Width = 200;
      ts2.GridColumnStyles.Add(cOrderDate);

      // Create a second formatted column using a PropertyDescriptor.
      // The formatting character "c" specifies a currency format.

      DataGridColumnStyle csOrderFreight =
         new DataGridTextBoxColumn(pcol["Freight"], "c", true);
      csOrderFreight.MappingName = "Freight";
      csOrderFreight.HeaderText = "Formatted Freight";
      csOrderFreight.Width = 100;
      ts2.GridColumnStyles.Add(csOrderFreight);

      // Add the DataGridTableStyle instances to
      // the GridTableStylesCollection

      myDataGrid.TableStyles.Add(ts1);
      myDataGrid.TableStyles.Add(ts2);

      // Sets the TablesAlreadyAdded to true so this doesn't happen again.
      TablesAlreadyAdded = true;
   }


   // Create a DataSet with two tables and populate it.
   private void MakeDataSet()
   {
      // Setup DB-Connection
      string ConnectionString = "data source=xeon;uid=sa;password=manager;database=northwind";
      SqlConnection cn = new SqlConnection(ConnectionString);

      // Create the DataSet
      myDataSet = new DataSet("CustOrders");

      // Fill the Dataset with Customers, map Default Tablename
      // "Table" to "Customers".

      SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM Customers",cn);
      da1.TableMappings.Add("Table","Customers");
      da1.Fill(myDataSet);

      // Fill the Dataset with Orders, map Default Tablename
      // "Table" to "Orders".

      SqlDataAdapter da2 = new SqlDataAdapter("SELECT * FROM Orders",cn);
      da2.TableMappings.Add("Table","Orders");
      da2.Fill(myDataSet);
   }
}

ubuntu linux定制过程

定制的组成部分 linux安装文件加载程序isolinu及其他相关信息、根文件系统镜像(Ramdisk)、内核镜像、根文件系统四部分组成。安装过程由安装加载程序控制,直至完成后转入到挂载的根文件系统...
  • aniuge008
  • aniuge008
  • 2017年06月13日 15:53
  • 534

MATLAB编辑器风格定制

此文借鉴了:matlab编辑器颜色风格设置一文 因为经常用到MATLAB,之前也是想把MATLAB编辑器弄得好看一点,发现从preferences修改确实是有点麻烦,而且经常自己都不知道修改的是啥,今...
  • lafengxiaoyu
  • lafengxiaoyu
  • 2017年06月05日 14:42
  • 1069

优雅的自定义TabLayout

为啥要自己定义TabLayout? 1.design包中的TabLayout很多时候不能满足UI的需求 2.我们需要自定义tab的位置和tab内容的字体和style 3.我们自定义的控件比较容易...
  • qq_22271479
  • qq_22271479
  • 2017年04月03日 19:37
  • 491

datagrid的初始化以及动态加载

//datagrid初始化  $(function() {     $('#tt').datagrid({     //title : '信息显示',     //iconCls : 'icon-sa...
  • hulufeng1203
  • hulufeng1203
  • 2014年02月17日 09:15
  • 1562

js动态创建DataGrid

和上一篇一样,使用JS动态的创建datagrid也是一件非常简单的事情,以项目中的实例来做说明,当然easyui的官网文档也有,例子很简单: ①从后台获取数据 ②根据需要显示...
  • wang13667539325
  • wang13667539325
  • 2015年04月30日 21:19
  • 6105

弹出datagrid内容的dialog框

js: $(function () { $('#risk_suit_list').datagrid({ queryParams: '', url: 'ri...
  • qq_36092584
  • qq_36092584
  • 2016年09月26日 21:36
  • 1801

easyui的datagrid使用以及分页

easyui对于开发桌面级的WEB应用界面确实十分方便,而且美观。这里着重记录一下datagrid的使用,以及通过前后端配合实现的超简单的数据分页查询方式。...
  • tzdwsy
  • tzdwsy
  • 2015年08月09日 13:57
  • 3440

datagrid加载数据,列内容显示,改变字体颜色

//1.加载数据  列内容显示蓝色字体 #region 加载数据  列内容显示蓝色字体 //代码出自:三项工作--项目管理系统--项目管理--项目变更--采购方式变更申请 //页面路径:Ope...
  • qq_25409579
  • qq_25409579
  • 2016年01月11日 11:01
  • 2546

EasyUI效果--DataGrid的编辑效果

DataGrid的编辑效果是我目前使用的easyUI的第三个效果,相对于前两个,这个算是比较复杂点了。     运行起来的效果,大概就是这样,任意点击某行,然后该行变为可以编辑的,失去焦点之后,该行...
  • liuyanlinglanq
  • liuyanlinglanq
  • 2015年02月28日 18:21
  • 11954

datagrid实现单行的选择、取消

jQuery EasyUI Demo function getSelected(){ var row = $('#tt').datagrid('getSelected')...
  • zhao1949
  • zhao1949
  • 2016年11月28日 15:40
  • 2497
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:定制DataGrid的风格
举报原因:
原因补充:

(最多只允许输入30个字)