473,480 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to add check box in windows form datagrid

271 Contributor
hi,
can any one help me in adding check box to a data grid in windows form. if possible help me with c#.
Jan 12 '07 #1
3 3546
radcaesar
759 Recognized Expert Contributor
hi,
can any one help me in adding check box to a data grid in windows form. if possible help me with c#.
Here is some thing for u,

Expand|Select|Wrap|Line Numbers
  1. //include all the required namespaces
  2.  
  3. using System;  
  4. using System.Drawing;  
  5. using System.Collections;  
  6. using System.ComponentModel;  
  7. using System.Windows.Forms;  
  8. using System.Data;  
  9. using System.Text;  
  10. using System.Xml;  
  11.  
  12. namespace Test  
  13. {
  14.  
  15. public class TestDataGrid : System.Windows.Forms 
  16. {
  17.     /// <summary> 
  18.       /// Required designer variable. 
  19.       /// </summary>   
  20.       private System.Windows.Forms.ComboBox cmbFunctionArea;
  21.       private DataTable                     dtblFunctionalArea;
  22.       private DataGrid                      dgdFunctionArea;
  23. }
  24. /// <summary>  
  25. /// public constructor 
  26. /// </summary>  
  27.  
  28. public frmInitialShortListing()  
  29. {  
  30.       //automatically generated by the VS Designer  
  31.       //creates the object of the above designer variables  
  32.       InitializeComponent();
  33.       PopulateGrid();  
  34. }
  35.  
  36. private void PopulateGrid()
  37. {
  38.       //Declare and initialize local variables used
  39.       DataColumn dtCol = null;//Data Column variable
  40.       string[]   arrstrFunctionalArea = null;//string array variable
  41.       System.Windows.Forms.ComboBox cmbFunctionArea;  //combo box var              
  42.       DataTable dtblFunctionalArea;//Data Table var
  43.  
  44.       //Create the combo box object and set its properties
  45.       cmbFunctionArea               = new ComboBox();
  46.       cmbFunctionArea.Cursor        = System.Windows.Forms.Cursors.Arrow;
  47.       cmbFunctionArea.DropDownStyle=System.Windows.Forms.ComboBoxStyle.DropDownList;
  48.       cmbFunctionArea.Dock          = DockStyle.Fill;
  49.       //Event that will be fired when selected index in the combo box is changed
  50.       cmbFunctionArea.SelectionChangeCommitted += new   EventHandlercmbFunctionArea_SelectedIndexChanged);
  51.  
  52.       //Create the String array object, initialize the array with the column
  53.       //names to be displayed
  54.       arrstrFunctionalArea          = new string [3];
  55.       arrstrFunctionalArea[0]       = "Functional Area";
  56.       arrstrFunctionalArea[1]       = "Min";
  57.       arrstrFunctionalArea[2]       = "Max";
  58.  
  59.       //Create the Data Table object which will then be used to hold
  60.       //columns and rows
  61.       dtblFunctionalArea            = new DataTable ("FunctionArea");
  62.       //Add the string array of columns to the DataColumn object       
  63.       for(int i=0; i< 3;i++)
  64.       {    
  65.             string str        = arrstrFunctionalArea[i];
  66.             dtCol             = new DataColumn(str);
  67.             dtCol.DataType          = System.Type.GetType("System.String");
  68.             dtCol.DefaultValue      = "";
  69.             dtblFunctionalArea.Columns.Add(dtCol);               
  70.       }     
  71.  
  72.       //Add a Column with checkbox at last in the Grid     
  73.       DataColumn dtcCheck    = new DataColumn("IsMandatory");//create the data          //column object with the name 
  74.       dtcCheck.DataType      = System.Type.GetType("System.Boolean");//Set its //data Type
  75.       dtcCheck.DefaultValue  = false;//Set the default value
  76.       dtblFunctionalArea.Columns.Add(dtcCheck);//Add the above column to the //Data Table
  77.  
  78.       //Set the Data Grid Source as the Data Table createed above
  79.       dgdFunctionArea.DataSource    = dtblFunctionalArea; 
  80.  
  81. //set style property when first time the grid loads, next time onwards it //will maintain its property
  82. if(!dgdFunctionArea.TableStyles.Contains("FunctionArea"))
  83. {
  84.             //Create a DataGridTableStyle object     
  85.             DataGridTableStyle dgdtblStyle      = new DataGridTableStyle();
  86.             //Set its properties
  87.             dgdtblStyle.MappingName            = dtblFunctionalArea.TableName;//its table name of dataset
  88.             dgdFunctionArea.TableStyles.Add(dgdtblStyle);
  89.             dgdtblStyle.RowHeadersVisible       = false;
  90.             dgdtblStyle.HeaderBackColor         = Color.LightSteelBlue;
  91.             dgdtblStyle.AllowSorting            = false;
  92.             dgdtblStyle.HeaderBackColor         = Color.FromArgb(8,36,107);
  93.             dgdtblStyle.RowHeadersVisible       = false;
  94.             dgdtblStyle.HeaderForeColor         = Color.White;
  95.             dgdtblStyle.HeaderFont              = new System.Drawing.Font("Microsoft Sans Serif", 9F,  
  96.                 System.Drawing.FontStyle.Bold, 
  97.                 System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  98.             dgdtblStyle.GridLineColor           = Color.DarkGray;
  99.             dgdtblStyle.PreferredRowHeight            = 22;
  100.             dgdFunctionArea.BackgroundColor           = Color.White; 
  101.  
  102. //Take the columns in a GridColumnStylesCollection object and set //the size of the
  103.             //individual columns   
  104.             GridColumnStylesCollection    colStyle;
  105. colStyle                = dgdFunctionArea.TableStyles[0].GridColumnStyles;
  106.             colStyle[0].Width             = 100;
  107.             colStyle[1].Width       = 50;
  108.             colStyle[2].Width       = 50;
  109.             colStyle[3].Width       = 80;
  110. }
  111.  
  112.   //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.
  113.  
  114.             DataGridTextBoxColumn dgtb    =     (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[0];
  115.  
  116.       //Add the combo box to the text box taken in the above step 
  117.       dgtb.TextBox.Controls.Add (cmbFunctionArea);        
  118.  
  119. Note:-//After these add the code to fill the details in the grid by //establishing
  120.       // connection to the server and writing necessary steps: 
  121.  
  122.   }//end of the class
  123.  
  124. }//end of the namespace
Jan 12 '07 #2
rajkiran
3 New Member
hi,
can any one help me in adding check box to a data grid in windows form. if possible help me with c#.
visit:
http://RustemSoft.com/DataGridColumns.htm
Jan 12 '07 #3
karthi84
271 Contributor
thanks for the coding and the link that was much help ful for me
Jan 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
7024
by: John | last post by:
Hello all, I'm trying to display a dataset into a datagrid and format the datagrid columns but I'm having a hard time with it. I've tried the MSDN examples but they don't seem to work. ...
4
6651
by: VR | last post by:
I am trying to embed a check box into a FlexGrid's cell, but having a problem when I start scrolling the grid. Here is my MyCheckBox class... class MyCheckBox : CheckBox { void Init (...
4
4475
by: Carl Fenley | last post by:
I believe I have almost successfully created a custom datagrid control. The new class builds without error. I have added it as reference to the main Windows Application project. It appears on...
2
1450
by: Wayne | last post by:
I have a datagrid on my windows form, it needs to be read only and when a user selects a row, I want the whole row to be selected. How would I go about doing this? -- Thanks Wayne Sepega...
7
1565
by: Richard | last post by:
If the grid is dragged off the screen and back on the data is visible. Tried a dg.refresh() and Inactivate(). Can I do this by overriding custom base class OnPaint that my form inherits from? I...
4
2910
by: Sileesh | last post by:
Hi I have datagrid with 8 items. Out of which 2 items are checkboxes. Data is binded dynamically to the datagrid Based on some values from the database I have to check or uncheck the checkbox...
2
6045
by: Rekha | last post by:
Hi I have datagrid with 5 items. Out of which 1 items are checkboxes. The bool column(checkbox column ) in added in datagrid. The rest of Data is binded dynamically to the datagrid Based on...
1
4209
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
1
2778
by: Rajesh Kumar Choudhary | last post by:
Hi, I want to use the system.windows.form.datagrid control present in .net 2005 or 2003. Is it possible to use this? Please let me know if it is not supported. I have seen and used the classes...
10
5177
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
0
7039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4476
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.