日期:2012-01-30  浏览次数:20567 次

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;