//This class describes the method to add any controls of your choice to the data // grid control. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text; using System.Xml; using System.Data.SqlClient; namespace PWDgFunctionalArea { public class TestDataGrid : System.Windows.Forms.Form { /// /// Required designer variables /// private System.Windows.Forms.ComboBox comboControl; private DataTable dataTable; private System.Windows.Forms.Button button1; private DataGrid.HitTestInfo hitTestGrid; private System.Windows.Forms.DataGrid datagrid1; private DataGridTextBoxColumn datagridtextBox; private string[] arrstr ; /// /// public constructor /// public TestDataGrid() { InitializeComponent(); //Method to create the customized data grid CreateGrid(); } /// /// Method defn to create the Data Grid using data table /// private void CreateGrid() { //Declare and initialize local variables used DataColumn dtCol = null;//Data Column variable //Create the String array object, initialize the array with the column //names to be displayed arrstr = new string [3]; arrstr[0] = "Control Name"; arrstr[1] = "Control"; arrstr[2] = "Description"; //Create the Data Table object which will then be used to hold //columns and rows dataTable = new DataTable("Controls"); //Add the string array of columns to the DataColumn object for(int i=0; i< 3;i++) { string str = arrstr[i]; dtCol = new DataColumn(str); dtCol.DataType = System.Type.GetType("System.String"); dtCol.DefaultValue = ""; dataTable.Columns.Add(dtCol); } //Set the Data Grid Source as the Data Table created above datagrid1.DataSource = dataTable; //set style property when first time the grid loads, next time onwards it //will maintain its property if(!datagrid1.TableStyles.Contains("Controls")) { //Create a DataGridTableStyle object DataGridTableStyle dgdtblStyle = new DataGridTableStyle(); //Set its properties dgdtblStyle.MappingName = dataTable.TableName;//its table name of dataset datagrid1.TableStyles.Add(dgdtblStyle); dgdtblStyle.RowHeadersVisible = false; dgdtblStyle.HeaderBackColor = Color.LightSteelBlue; dgdtblStyle.AllowSorting = false; dgdtblStyle.HeaderBackColor = Color.Navy;//.FromArgb(8,36,107); 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; datagrid1.BackgroundColor = Color.White; //Take the columns in a GridColumnStylesCollection object and set //the size of the //individual columns GridColumnStylesCollection colStyle; colStyle = datagrid1.TableStyles[0].GridColumnStyles; colStyle[0].Width = 97; colStyle[1].Width = 220; colStyle[2].Width = 150; } //Take the text box from the second column of the grid where u will be adding the controls of your choice datagridtextBox = (DataGridTextBoxColumn)datagrid1.TableStyles[0].GridColumnStyles[1]; datagridtextBox.TextBox.GotFocus += new EventHandler(this.dgdFunctionArea_GotFocus); } /// /// Method defn to connect to the data base and then load the data grid /// private void LoadGrid() { try { //Establish the connection to the data base and open it SqlConnection sqlConn = new SqlConnection("Database=****; Server=*****; uid = ***; pwd=*****"); //*-Pass the required details sqlConn.Open(); //create the sql command object and set its command type to execute the sql query to get the results SqlCommand sc = new SqlCommand(); sc.Connection = sqlConn; sc.CommandType = CommandType.Text; sc.CommandText = "SELECT ControlName,Control,Description FROM t_TestControls"; //create the data set object to be used to fill the data grid with the data DataSet ds = new DataSet(); //Create the sql adapter that will be used to fill the data set created above SqlDataAdapter myReader = new SqlDataAdapter(sc); myReader.Fill(ds); //Fill the rows in the grid for(int i =0;i /// to create the controls /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.datagrid1 = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.datagrid1)).BeginInit(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(192, 152); this.button1.Name = "button1"; this.button1.TabIndex = 9; this.button1.Text = "Load Grid"; this.button1.Click += new System.EventHandler(this.button1_Click); // // datagrid1 // this.datagrid1.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText; this.datagrid1.CaptionVisible = false; this.datagrid1.DataMember = ""; this.datagrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.datagrid1.Location = new System.Drawing.Point(8, 16); this.datagrid1.Name = "datagrid1"; this.datagrid1.PreferredRowHeight = 20; this.datagrid1.ReadOnly = true; this.datagrid1.Size = new System.Drawing.Size(472, 128); this.datagrid1.TabIndex = 10; this.datagrid1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dgd_MouseDown); // // TestDataGrid // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(488, 181); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.datagrid1, this.button1}); this.Name = "TestDataGrid"; ((System.ComponentModel.ISupportInitialize)(this.datagrid1)).EndInit(); this.ResumeLayout(false); } /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new TestDataGrid()); } /// /// Method defn to add the control of your choice to the data grid /// /// /// private void dgdFunctionArea_GotFocus(object o, EventArgs e) { //Create the combo control to be added and set its properties comboControl = new ComboBox(); comboControl.Cursor = System.Windows.Forms.Cursors.Arrow; comboControl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown; comboControl.Dock = DockStyle.Fill; comboControl.Items.AddRange(new string[5]{"","Information Technology","Computer Science","Bio Technology","Electrical Engg"}); //Create the date time picker control to be added and set its properties DateTimePicker dtp = new DateTimePicker(); dtp.Dock = DockStyle.Fill; dtp.Cursor = Cursors.Arrow; //Create the check box control to be added and set its properties CheckBox chk = new CheckBox(); chk.Dock = DockStyle.Fill; chk.Cursor = Cursors.Arrow; //Create the radio button control to be added and set its properties RadioButton rb = new RadioButton(); rb.Dock = DockStyle.Fill; rb.Cursor = Cursors.Arrow; //Add the controls to the respective columns in the data grid for(int i = 0 ;i < dataTable.Rows.Count ; i++) { //if the data in the first column is date time, add a date time control to the grid if(datagrid1[i,0].ToString().Equals("DateTime") && hitTestGrid != null && hitTestGrid.Row == i) { datagridtextBox.TextBox.Controls.Add(dtp); comboControl.SendToBack(); chk.SendToBack(); rb.SendToBack(); dtp.BringToFront(); } //if the data in the first column is combo box, add a combo box control to the grid else if(datagrid1[i,0].ToString().Equals("ComboBox") && hitTestGrid != null && hitTestGrid.Row == i) { datagridtextBox.TextBox.Controls.Add(comboControl); chk.SendToBack(); dtp.SendToBack(); rb.SendToBack(); comboControl.BringToFront(); } //if the data in the first column is check box, add a check box control to the grid else if(datagrid1[i,0].ToString().Equals("CheckBox") && hitTestGrid != null && hitTestGrid.Row == i) { datagridtextBox.TextBox.Controls.Add(chk); comboControl.SendToBack(); dtp.SendToBack(); rb.SendToBack(); chk.BringToFront(); } //if the data in the first column is radio button, add a radio button control to the grid if(datagrid1[i,0].ToString().Equals("Radio Button") && hitTestGrid != null && hitTestGrid.Row == i) { datagridtextBox.TextBox.Controls.Add(rb); comboControl.SendToBack(); chk.SendToBack(); dtp.SendToBack(); rb.BringToFront(); } datagridtextBox.TextBox.BackColor = Color.White; } } /// /// MEthod defn when load grid button is clicked /// /// /// private void button1_Click(object sender, System.EventArgs e) { try { LoadGrid(); } catch{} } /// /// Mouse down event of the data grid /// /// /// private void dgd_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { //take the hit test that will be used to identify which row has been clicked hitTestGrid = datagrid1.HitTest(e.X,e.Y); } }//end of the class }//end of the namespace