Playing with datagrid control

zhaozj2021-02-16  72

THE ARTICLE? PLAYING 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 . BELOW IS Presented an Article on DataGrid and Other Related Classes and how the user can play with them.

..........................

Adding Rows, Column and Controls to the DataGrid Dynamically

// incrude all the required namespaces use 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

{

///

/// Required Designer Variable.

/// private system.windows.Forms.comBobox CMBFunctionArea; Private DataTable DTBLFunctionAlareA; Private DataGrid DGDFunctionareArea;} ///

/// public constructor

///

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 d isplayed 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 DTBLFUNCTIONAAA.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 propertyif (dgdFunctionArea.TableStyles.Contains ( "FunctionArea")!) {// Create a DataGridTableStyle object DataGridTableStyle dgdtblStyle = new DataGridTableStyle (); // Set its properties dgdtblStyle.MappingName = dtblFunctionalArea.TableName; // its table name of dataset dgdFunctionArea.TableStyles.Add (dgdtblStyle); dgdtblStyle.RowHeadersVisible = false; dgdtblStyle.HeaderBackColor = Color.LightSteelBlue; dgdtblStyle.AllowSorting = false; dgdtblStyle .HeaderBackColor = Color.FromArgb (8,36,107); dgdtblStyle.RowHeadersVisible = false; dgdtblStyle.HeaderForeColor = Color.White; dgdtblStyle.HeaderFont = new System.Drawing.Font ( "Microsoft Sans Serif", 9F, System.Drawing.FontStyl e.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte) (0))); dgdtblStyle.GridLineColor = Color.DarkGray; dgdtblStyle.PreferredRowHeight = 22; dgdFunctionArea.BackgroundColor = Color.White;

// Take the columns in a GridColumnStylesCollection object and set // the size of the // individual columns GridColumnStylesCollection colStyle; colStyle = dgdFunctionArea.TableStyles [0] .GridColumnStyles; colStyle [0] .Width = 100; colStyle [1] .Width = 50; colStyle [2] .width = 50; colStyle [3] .width = 80;} // to add the combo box dynamically to Take to Take the // text box That is present (by Default) ) in the column where u want to add // this combo box (here it is first column ie Functional Area) .From the // tablestyles of the data grid take the grid column styles of the column // where you want to add the Combo box respectively.

DataGridTextBoxColumn DGTB = (DataGridTextBoxColumn) DGDFunctionArea.tableStyles [0] .gridcolumnStyles [0];

// Add the Combo Box to the text box taken in the Above step

DGTB.TextBox.Controls.add (cMBFunctionarea); Note: - // after these add the code to fill the details in the grid by // establishing // connection to the server and write necessary steps:

} // end of the class} // end of the namespace

Fig Below Depicts The Concept Discussed Above:

Figure 1: adding rows, columns and controls Dynamical to the data

Grid

Combo Box Control Added to The Functional Area Column (for Each Row) Dynamical.

How To Add Check Boxes to a DataGrid

// Call this Below Method After Initialize Component

Private void populateshortlistgrid ()

{Datacolumn DTCShortlist;

// Combo Box Control Added As Discussed Above

CMBWorkflow

=

New comboBOX ();

CMBworkflow.cursor

= System.Windows.Forms.cursors.Arrow;

CMBWORKFLOW.DROPDOWNSTYLELELELE

= System.Windows.Forms.comBoboxStyle.dropdownList; cMbworkflow.dock

= DOCKSTYLE.FILL;

DTBShortlist

=

New DataTable ("Shortlist");

String strcolumnname

=

NULL;

String [] Arrstrsearch

= NULL;

Arrstrsearch

=

New

String [4];

Arrstrsearch [1]

= "Candidate Code";

Arrstrsearch [2]

= "Candidate Name";

Arrstrsearch [3]

= "Workflow";

// adding a check box control in The First Column of The Data Grid

// Create a Data Column Object with column name as? SELECT? span style = "MSO-TAB-COUNT: 1">

Datacolumn

DTCCHECK

=

New Datacolumn ("SELECT");

// set the data type of the checkbox i.e. to boolean

DTCCHECK.DATATYPE

= System.Type.gettype ("System.Boolean");

// set ITS default value as false

DTCCHECK.DEFAULTVALUE

=

False;

// add the Above check box column to the data table

DTBSHORTLIST.COLUMNS.ADD (DTCCHECK);

// Also Add the Other Three Column I.E. Candidate Code, Candidate // Name and Workflow Respectively

FOR

INT INTI = 1; INTI <4; INTI )

{

Strcolumnname

= arrstrsearch [inti];

DTCShortlist

=

New datacolumn (strcolumnname);

DTCShortlist.DataType

= System.Type.gettype ("System.String");

DTBSHORTLIST.COLUMNS.ADD (DTCShortlist);

}

DGDShortlist.dataSource = DTBSHORTLIST;

Figure Below Depicts The Above Discussion:

Figure: added as the first column to the data grid

Focusing a Particular Cell in the data grid

To focus a particular cell in the grid created above, you have to focus on the TextBox Control that is present in each cell of the DataGrid created above. To take the text box present in the grid cell which u want to focus, follow the steps FOLLOWED BELOW:

// bring the focus to the grid in which the cell is present (where u want // the focus) DGDLOAD.FOCUS ();

// Create a DataGrid Cell Object and take the cell by passing row and // column number respectively

DataGridcell DGC = New DataGridcell (1, 1); // Here IT IS 2NDROW, 2nd Column

// Make the current cell of the grid t Cell U Have Taken Above ie // The Cell Where u Need The focus to be dgdload.currentcell = DGDLOAD.CURRENTCELL = DGC; // to take the text box of the cell where u want the focus to be take // it from the Table Styles of the grid and in that the column style // by passing the column number where u wants the focus to be DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn) dgdLoad.TableStyles [0] .GridColumnStyles [2];

// Focus on the text box i.e. in turn on cell Where u Need The Focus

DGTB.Textbox.focus ();

Figure Below Signifier The Above Concept:

Fig 2: Focus to the particular Cell in a data grid

About The Author: Tushar Ameta is An Engineering Graduate. He is Working with a Software Mnc in Bangalore, India. He Has Been Working on The .Net and C # for the Past 10 Months.

Any Queries Welcome: Email ID:

Tush_raj@rediffmail.com

转载请注明原文地址:https://www.9cbs.com/read-15210.html

New Post(0)