Add Controls to a DataGrid At Runtime

xiaoxiao2021-03-06  102

Add Controls to a DataGrid At Runtime

Authordate of SubmissionUser LevelTushar Ameta03 / 19 / 2002InterMediate

Source Code: AddingControlstodataGridimg.zip 4 KB

INTRODUCTIONWhile 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 ie 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 ie 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 itSqlConnection sqlConn = new SqlConnection ( "Database = ****; server = *****; uid = **; pwd = ****"); // * - pass the required detailssqlconn (); // Create the SQL Command Object And set its command type to // execute the sque query to get the // resultss qlCommand 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 aboveSqlDataAdapter myReader = new SqlDataAdapter (sc); myReader.Fill (ds); / / Fill the rows in the gridfor (int i = 0; i

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 columncomboControl = 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 propertiesDateTimePicker dtp = new DateTimePicker (); dtp.Dock = DockStyle.Fill; dtp.Cursor = Cursors.Arrow; // Create the check box control to be added and set its propertiesCheckBox chk = new CheckBox (); chk.Dock = DockStyle.Fill; chk.Cursor = Cursors.Arrow; // Create the Radio Button Control To Be Added and Set ITS PropertiesRadiobutton RB = New Radiobutton (); Rb.dock = DockStyle.Fill; Rb.Cursor = Cursors.Arrow; // Add the controls to the respective columns in the data gridfor (int i = 0; i

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.

.

转载请注明原文地址:https://www.9cbs.com/read-95738.html

New Post(0)