473,545 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
+ 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 3554
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
7028
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. Basically, what I want to do is that, on a button click, the dataset fills up dynamically, binds to the datagrid, and displays the data. This is my code: ...
4
6664
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 ( AxMSFlexGridLib.AxMSFlexGrid oGrid,
4
4484
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 the Toolbox as expected, but... When I drag-drop it on a Form, it appears like a component (e.g. SqlConnection) down in it's own area not drawn on...
2
1458
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 Jacksonville, Fl
7
1578
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 don't thin it is the backcolor or something like that because the error does not occur on everyone's workstation. Could be it be the video card or tied...
4
2915
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 in the datatgrid. Does any one know how to do this?
2
6050
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 some values from the database I have to check or uncheck the checkbox in the datatgrid. Does any one know how to do this in windows form?
1
4219
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 the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn...
1
2784
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 from FCL using system.tlb or mscorelib.tlb from dot net framework. I could use ArrayList successfully without any issue.
10
5188
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. I set the AutoPostBack property of the CheckBox in the Header to True & am invoking a sub named 'CheckAllRows' on the CheckedChanged event of this...
0
7487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7446
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1908
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 we have to send another system

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.