datagrid搞的头大,发现一篇文章,但看着就晕..

本文详细介绍了DataGrid控件在WinForms和WebForms中的使用,包括数据源分配、格式设置、导航、复制到剪贴板、导出到文本文件等操作。重点讨论了如何通过DataGridTableStyle自定义表格样式,以及在WebForms中使用HTML元素进行格式化。此外,还对比了两者在编辑、添加和更新行时的不同实现方式。
摘要由CSDN通过智能技术生成

希望有人翻译下...

5 Data Grid

A DataGrid is used to display an independent Table or a collection of Tables contained in a DataSet. It also provides a UI for editing, deleting and inserting records along with a set of event notifications for programmatic response and for data change tracking. In addition it supports a collection of DataGrid Table Styles that provides custom presentation for each table in its DataSource. WebForms and WinForms can both display a DataGrid bound and unbound to a database; however, there are significant differences in usage.

This section will present how to work with a DataGrid and its many features and different use models.

5.1 Methods and Properties

Methods

Description

Collapse

Collapse a specified row or all rows.

Expand

Expand a specified row or all rows

IsExpanded

Returns true if a specified row is expanded otherwise false.

NavigateBack

Navigates back to the previously displayed table in the grid.

NavigateTo

Navigate to a table in the grid.

SetDataBinding

Binds a dataset to the DataSource and selects a table.

Select

This method selects row and highlights it. It is not necessarily the same as the current row.

UnSelect

This method unselects a row and turns of highlighting. It does not change the current row.

Properties

Description

AllowSorting

Set to True allows column sorting using the column headers.

AllowNavigation

Allows navigation within the data grid using for example the arrow or tab keys.

AlternatingBackColor

This sets a background color for alternating rows in the DataGrid making it easier to read across a row.

DataSource

This returns or sets the source of data for the DataGrid. It is either a DataSet or a DataTable.

DataMember

This sets the table to be displayed. When the DataSource is a DataSet the DataGrid display is in a hierarchical mode, setting this to one of the Tables in the DataSet automatically forces it to be displayed.

CurrentCell

Gets or Sets the currently active cell including the CurrentRowIndex

CurrentRowIndex

Gets or Sets the current row

5.2 Assigning Data Sources

The DataSource for a DataGrid is either a Table or a DataSet. For example, a previously created DataTable dt object is assigned to the DataGrid as using the DataGrid’s DataSource member follows:

dg.DataSource = dt; // DataSource contains only a
// DataTable object

A DataSet that contains multiple tables in its table’s collection can be assigned using the DataGrid’s DataSource member as follows::

DataSet ds = new DataSet();
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
...
ds.Tables.Add(dtn);

dg.DataSource = ds; // DataSource contains a DataSet object

By default the first table in the collection is displayed in the DataGrid, but a particular table can be selected using the DataGrid’s DataMember property as follows:

dg.DataMember = dt2.TableName;

or equivalently:

dg.DataMember = ds.Tables[1].TableName; // zero-based index

or a DataSet can be assigned and a Table selected at the same time using the SetDataBinding() method as follows:

dg.SetDataBinding(ds, dt2.TableName);

Note : The only restriction for DataSets is that tables must have a primary key, if not then an error will occur at the time of assigning the DataSet to the DataSource.

5.3 Formatting

5.3.1 DataGridTableStyle - WinForms

A DataTable is used to hold a collection of Column definitions and a collection of rows containing data for each column. A DataSet contains a collection of Tables and a DataGrid provides the interactive UI for the presentation of a table from a DataTable or from one contained in a DataSet.

The rendering of each table being managed by the DataGrid is carried out through the collections of individual table styles (DataGridTableStyle) where each table style contains a collection of column styles. Refer to Figure 1 for a pictorial view of these relationships.

Microsoft’s .NET IDE provides DataGridTextBoxColumn or DataGridBoolColumn objects that are as there name implies a TextBox and Boolean or CheckBox column respectively. The TextBox column is the default column type used when declaring a DataGrid.

Unique rendering for each column is provided through the column style properties and methods. In addition it is possible to modify the .NET provided column styles or define new column styles such as ComboBoxes and ImageControls that are inherited from the base classes.

Note: DataGridTableStyle is not available for WebForms instead one must use itemstyles – see WebForm example.

In the following example assume the following:

A DataTable dt is defined

  • A DataSet ds is defined and contains dt at index equal 0 in its Tables collection
  • A DataGrid dg is defined with DataSource containing ds

Initially the DataGrid dg does not have any TableStyles contained in the TableStyles collection, which can be seen through the Count property value:

dg.TableStyles.Count;

Also, the dg.Controls collection only contains Vertical and Horizontal Scrollbar controls.

// First, a DataGridTableStyle object is declared
// that will hold a collection of
// GridColumnStyles.
System.Windows.Forms.DataGridTableStyle DGStyle =
   new DataGridTableStyle();
// In this example the .NET DataGridTextBoxColumn class is used.
DataGridTextBoxColumn textColumn;
// Loop through each Column in table dt to get a DataColumn
// object that will be used
// to define properties for its TextBoxColumn style.
foreach (DataColumn dc in dt.Columns)
{
  textColumn = new DataGridTextBoxColumn();
  // the MappingName must correspond to the Table Column Name
  // in order to establish the relationship between them
  textColumn.MappingName = dc.ColumnName;
  // the HeaderText value is displayed in Header for the column, here
  // the Caption value is used.
  textColumn.HeaderText = dc.Caption;
  // specify some other property values
  textColumn.Width = 200;
  textColumn.Alignment = System.Windows.Forms.HorizontalAlignment.Left ;
  textColumn.ReadOnly = true;
  // Add this column object with its specifications to the
  // GridColumnStyles collection
  DGStyle.GridColumnStyles.Add(textColumn);
}
// The name of the DataGridTableStyle must match that of the table
// Since the DataGrid can contain multiple tables,
// similar the TableStyles collection
// can contain multiple DataGridTableStyles, one for each table.
DGStyle.MappingName = ds.Tables[0].TableName;
DGStyle.AlternatingBackColor = Color.Gainsboro;
DGStyle.AllowSorting = false;
DGStyle.ReadOnly = true;
// The Clear() method is called to ensure that
// the previous style is removed.
dg.TableStyles.Clear();
// Add the new DataGridTableStyle collection to
// the TableStyles collection
dg.TableStyles.Add(DGStyle);

5.3.2 Change only one table’s DataGridTableStyles

The following shows how to change only one Table’s DataGridTableStyles. Assume the table’s style is contained in the TableStyles collection at index equal 0

// Clear only a single table of its GridColumnStyles
dgConversionTable.TableStyles[0].GridColumnStyles.Clear();
DataGridTextBoxColumn textColumn;
// Loop through each Column in table dt to get a
// DataColumn object that will be used
// to define properties for its TextBoxColumn style.
foreach (DataColumn dc in dt.Columns)
{
  textColumn = new DataGridTextBoxColumn();
  // the MappingName must correspond to the Table Column Name
  // in order to establish the relationship between them
  textColumn.MappingName = dc.ColumnName;
  // the HeaderText value is displayed in Header for the column, here
  // the Caption value is used.
  textColumn.HeaderText = dc.Caption;
  // specify some other property values
  textColumn.Width = 200;
  textColumn.Alignment = System.Windows.Forms.HorizontalAlignment.Left ;
  textColumn.ReadOnly = true;
  // Add this column object with its specifications to
  // the GridColumnStyles collection
  // for TableStyles[0]
  dgConversionTable.TableStyles[0].GridColumnStyles.Add(textColumn);
}

5.3.3 Change a single column in a Table’s DataGridTableStyle

One or more columns in an existing Table’s DataGridTableStyles collection can be changed using code to similar to the following. In this example, the first method uses the HeaderText to find the column that is to be changed in a table that is named “Demo”. Once the desired object is obtained, the current HeaderText value for the TextBoxColumn “old header text” is changed to “new header text”. Also, the Width of the column is changed to 150. In the second example the Column MappingName must be used instead of the HeaderText if they are different.

5.3.3.1 Method 1 – for-loop through collection

In this example

GridColumnStylesCollection gcsColl =
  dgConversionTable.TableStyles[“Demo”].GridColumnStyles;
for (int i=0; i< gcsColl.Count; i++)
{
  if(gcsColl[i].GetType() == typeof(DataGridTextBoxColumn))
  {
   DataGridTextBoxColumn textColumn = (DataGridTextBoxColumn)gcsColl[i];
   If (textColumn.HeaderText == “old header text”)
   {
     textColumn.Width = 150;
     textColumn.HeaderText = “new header text”;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值