Monday, July 26, 2004

manipulating drop-downs in template column of datagrid

Code snippet for demonstration of manipulating drop-downs in template column of datagrid:

Copy the following between the
tags of the aspx page in HTML view.























Copy the following in the Page_Load method in code-behind or

between the tags



private void Page_Load(object sender, System.EventArgs e)

{

DataTable dt = new DataTable();

DataRow dr;

DropDownList ddl;

int i;



//Create the datasource for the datagrid

dt.Columns.Add("col1", Type.GetType("System.String"));

for(i = 0; i < 5; i++)

{

dr = dt.NewRow();

dr[0] = i.ToString();

dt.Rows.Add(dr);

}



//bind datasource to the datagrid

dgtest.DataSource = dt;

dgtest.DataBind();



//manipulate the drop-down list of each row of the datagrid.

//this code simply displays the ID as the sole element of the drop-down.

for(i = 0; i < dgtest.Items.Count; i++)

{

ddl = (DropDownList)dgtest.Items[i].FindControl("ddltest");



if(ddl != null)

{

ddl.Items.Clear();

ddl.Items.Add(i.ToString());

}

}

}



output:

ID ¦

-------------------

0 ¦ | 0 |v|

1 ¦ | 1 |v|

2 ¦ | 2 |v|

3 ¦ | 3 |v|

4 ¦ | 4 |v|

1 comment:

Mitesh V. Mehta said...

Hi,
you need to decide whether to use Free Custome controls (3rd party) or writing your own code(creating new user control like editable drop down).

Try this Free 3rd party controls http://www.progstudios.com/

or i can give you an idea how to create a custom control like that in your own.please let me know

Regards,
Mitesh Mehta