hi John, seems like working code. I have tried to test your code and it
seems to works well, with your selectedIndexChanged event triggering as
expected :
public class MyCustomDataGrid : DataGrid
{
protected override void CreateControlHierarchy(bool useDataSource)
{
base.CreateControlHierarchy(useDataSource);
Table dataGridTable = Controls[0] as Table;
//Get the table header row.
DataGridItem dataGridItem = dataGridTable.Controls[0] as
DataGridItem;
//Add a ddl to each column in the header.
//Use style sheet "td.DataGridHeaderCell select" to make
//sure each ddl is on a new line with spacing between the
column
//header text or sort link.
foreach (Control c in dataGridItem.Controls)
{
System.Web.UI.WebControls.DropDownList DropDownList1 = new
DropDownList();
c.Controls.AddAt(c.Controls.Count, DropDownList1);
DropDownList1.AutoPostBack = true;
DropDownList1.SelectedIndexChanged += new
System.EventHandler(this.DropDownList1_SelectedIndexChanged);
if (useDataSource)
{
DropDownList1.Items.Add(new ListItem("1", "a"));
DropDownList1.Items.Add(new ListItem("2", "b"));
DropDownList1.Items.Add(new ListItem("3", "c"));
}
}
}
private void DropDownList1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Context.Response.Write("Raised dropdownlist
selectedindexchanged");
}
}
Page to test the custom DataGrid Control :
----------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.MyCustomDataGrid1.DataSource = CreateDataSource();
this.MyCustomDataGrid1.DataBind();
}
}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
John Blair said:
Hi Alessandro ,
Thanks for replaying ...it is a custom contol derived from datagrid ....
here's the relevant snippet!
protected override void CreateControlHierarchy(bool useDataSource)
.
.
.
CreateFilterControlHierarchy(useDataSource));
.
.
.
protected void CreateFilterControlHierarchy(bool useDataSource){
//Data Grid table is the first control in the collection.
Table dataGridTable = Controls[0] as Table;
Page.Trace.Write("DataGrid:Begin CreateFilterControlHierarchy");
//Get the table header row.
DataGridItem dataGridItem = dataGridTable.Controls[0] as
DataGridItem;
//Add a ddl to each column in the header.
//Use style sheet "td.DataGridHeaderCell select" to make
//sure each ddl is on a new line with spacing between the column
//header text or sort link.
int i =0;
foreach (Control c in dataGridItem.Controls){
DropDownList ddl = new DropDownList();
//Begin tracking viewstate.
c.Controls.AddAt(c.Controls.Count,ddl);
//Populate the ddls.
//Note: If not using the datasource rely on viewstate to restore
//ddl filter content.
string columnName =
DataSourceDataSet.Tables[0].Columns.ColumnName;
ddl.ID=columnName;
ddl.AutoPostBack=true;
//THIS IS IGNORED!!!!!
ddl.SelectedIndexChanged += new EventHandler(
this.DataGridDropDownList_SelectedIndexChanged);
if (useDataSource){
PopulateDDL(
ddl,
DataSourceDataSet.Tables[0],
columnName);
//Initialise view state.
ViewState[columnName] = ddl.Items[0].Text;
}
else{
//Use existing viewstate.
}
}
Page.Trace.Write("DataGrid:End CreateFilterControlHierarchy");
}
.
.