当前位置:编程学习 > C#/ASP.NET >>

推荐: 玩转DataGrid自定义控件 --- Playing with DataGrid Control (zt)

答案:Playing with DataGrid Control Author Date of Submission User Level
Tushar Ameta 01/08/2002 Intermediate

The article 揚laying with DATAGRID? presented above gives a user the overview to show the importance and versatility of the DATAGRID control.

Introduction   

Accessing data has become a major programming task for modern software programming, both for standalone applications and for web-based applications. Microsoft's ADO.NET technology offers a solution to many of the problems associated with data access. Among the important part of ADO.NET ,DATAGRID control holds an important stature. Below is presented an article on DATAGRID and other related classes and how the user can play with them.

The versatile DataGrid control displays tabular data and supports selecting, sorting, and editing the data. Each data field is displayed in a separate column in the order it is stored in the database.

Adding Rows, columns and controls to the DataGrid dynamically   

//include all the required namespaces

using System;   
using System.Drawing;   
using System.Collections;   
using System.ComponentModel;   
using System.Windows.Forms;   
using System.Data;   
using System.Text;   
using System.Xml;  
  
namespace Test  
{
                
public class TestDataGrid : System.Windows.Forms
{
    /// <summary>
      /// Required designer variable.
      /// </summary>   
      private System.Windows.Forms.ComboBox cmbFunctionArea;
      private DataTable                     dtblFunctionalArea;
      private DataGrid                      dgdFunctionArea;
}
/// <summary>   
/// public constructor
/// </summary>  

public frmInitialShortListing()  
{  
      //automatically generated by the VS Designer  
      //creates the object of the above designer variables  
      InitializeComponent();
      PopulateGrid();  
}


private void PopulateGrid()
{
      //Declare and initialize local variables used
      DataColumn dtCol = null;//Data Column variable
      string[]   arrstrFunctionalArea = null;//string array variable
      System.Windows.Forms.ComboBox cmbFunctionArea;  //combo box var              
      DataTable dtblFunctionalArea;//Data Table var
           
      //Create the combo box object and set its properties
      cmbFunctionArea               = new ComboBox();
      cmbFunctionArea.Cursor        = System.Windows.Forms.Cursors.Arrow;
      cmbFunctionArea.DropDownStyle=System.Windows.Forms.ComboBoxStyle.DropDownList;
      cmbFunctionArea.Dock          = DockStyle.Fill;
      //Event that will be fired when selected index in the combo box is changed
      cmbFunctionArea.SelectionChangeCommitted += new   EventHandlercmbFunctionArea_SelectedIndexChanged);
     
      //Create the String array object, initialize the array with the column
      //names to be displayed
      arrstrFunctionalArea          = new string [3];
      arrstrFunctionalArea[0]       = "Functional Area";
      arrstrFunctionalArea[1]       = "Min";
      arrstrFunctionalArea[2]       = "Max";
     
      //Create the Data Table object which will then be used to hold
      //columns and rows
      dtblFunctionalArea            = new DataTable ("FunctionArea");
      //Add the string array of columns to the DataColumn object       
      for(int i=0; i< 3;i++)
      {    
            string str        = arrstrFunctionalArea[i];
            dtCol             = new DataColumn(str);
            dtCol.DataType          = System.Type.GetType("System.String");
            dtCol.DefaultValue      = "";
            dtblFunctionalArea.Columns.Add(dtCol);               
      }     

      //Add a Column with checkbox at last in the Grid     
      DataColumn dtcCheck    = new DataColumn("IsMandatory");//create the data          //column object with the name
      dtcCheck.DataType      = System.Type.GetType("System.Boolean");//Set its //data Type
      dtcCheck.DefaultValue  = false;//Set the default value
      dtblFunctionalArea.Columns.Add(dtcCheck);//Add the above column to the //Data Table
  
      //Set the Data Grid Source as the Data Table createed above
      dgdFunctionArea.DataSource    = dtblFunctionalArea;  

//set style property when first time the grid loads, next time onwards it //will maintain its property
if(!dgdFunctionArea.TableStyles.Contains("FunctionArea"))
{
         &

上一个:在狀態欄中加入進度條的實現(原創)
下一个:在 Visual Basic .NET 和 Visual C# .NET 中创建控件数组

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,