Adding Controls to a DataGrid at Runtime


INTRODUCTION

While developing application software, representing data in the GUI in friendly and readable format is one of the most important considerations.

In C# application (or Windows/desktop applications), .NET provides Data grid control to achieve the above important and vital consideration. Sometimes while representing data in a data grid control user wants to edit the details i.e. he/she wants to edit the details through a combo control or a date time picker control etc.

This article below sheds some light in above area i.e. user can add any control based on his/her choice and can use it to edit the details in the data grid.

Load the Grid with details (when the button Load Grid is clicked)

//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 _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<ds.Tables[0].Rows.Count;i++)
{
dataTable.LoadDataRow(arrstr,
true);
datagrid1[i,0]= ds.Tables[0].Rows[i].ItemArray[0].ToString();
datagrid1[i,1]= ds.Tables[0].Rows[i].ItemArray[1].ToString();
datagrid1[i,2]= ds.Tables[0].Rows[i].ItemArray[2].ToString();
}



Add a control to the data grid

//ADD the below code in the Got focus event of the data grid text box column
// To add any control ,create its object,set its property and add it to the respective column
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 &&
itTestGrid.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();
}
.....
datagridtextBox.TextBox.BackColor = Color.White;
}

NOTE: hitTestGrid can be obtained in the mouse down event of the data grid.



For complete source code, please see the attached cs file.