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;
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")) { //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.fontstyle.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 the data grid, you have 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 i.e. 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.
//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 writing necessary steps:
}//end of the class
}//end of the namespace
fig below depicts the concept discussed above:
figure 1: adding rows,columns and controls dynamically to the data grid
combo box control added to the functional area column (for each row) dynamically.
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.dropdownstyle = 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 揝elect?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 columns 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 as the cell u have taken above i.e. //the cell where u need the focus to be 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 signifies 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: [email protected]