Adding Controls to a DataGrid at Runtime

Adding Controls to a DataGrid at Runtime
AuthorDate Of SubmissionUser Level
Tushar Ameta03/19/2002Intermediate
 

Source code: AddingControlsToDataGridImg.zip 4 KB

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 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<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 && 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();
}
...
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.


Tushar Ameta
Tushar Ameta is an engineering graduate. He is working with a Software MNC in Bangalore,India. He has been working on the .NET and C# for the past 10 months.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值