Can I use different data sources for each row in a given column (column type dropdownlist)

  • Thread starter Thread starter zaki.agha
  • Start date Start date
Z

zaki.agha

Hi
I have a aspx application written in c#.
I have created a datagrid control on the page.
One of the columns in the control is a dropdownlist.
I would like to set the data source of the dropdownlist to a different
recordset for each row.
How can I do this in C#?

I have used the ItemDataBound event, to set the DataSource for each row
of the dropdownlist.
Running the code in debug mode, I can see that the DataSource is being
reset, however, when my page comes up, there are two rows in the data
grid, both drop down lists have the same data source.

private void dgInvoiceList_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e )
{
DropDownList ddlCategory =
(DropDownList)e.Item.FindControl("ddlCategory");

switch ((int)(e.Item.ItemType))
{ {
case (int)ListItemType.Item :

if( (e.Item.Cells[13].Text ==
GTS.Common.GTSConsts.CostCategoryTypeCode_BookingExtentionExpense) |
(e.Item.Cells[13].Text ==
GTS.Common.GTSConsts.CostCategoryTypeCode_BookingExtensionInternalAir)
)
{
ddlCategory.DataSource = mdsCostCategories_Extensions.Tables[0];
ddlCategory.DataBind();
}
break;

case (int)ListItemType.AlternatingItem:
if( (e.Item.Cells[13].Text ==
GTS.Common.GTSConsts.CostCategoryTypeCode_BookingExtentionExpense) |
(e.Item.Cells[13].Text ==
GTS.Common.GTSConsts.CostCategoryTypeCode_BookingExtensionInternalAir)
)
{
ddlCategory.DataSource = mdsCostCategories_Extensions.Tables[0];
ddlCategory.DataBind();
}
break;
}
}
 
There is another place in the code where all ddl's get set to
mdsCostCategories.Tables[0]
But then both ddl's are displaying mdsCostCategories.Tables[0] even tho
I have the code below in ItemDataBound. I know for the second row the
code is being executed cuz I run it in debug mode.
What exactly is the problem? both dropdowns are bound to
mdsCostCategories_Extensions.Tables[0];
so obviously they will show the same data.

this one should help you understand the datagrid:
http://www.odetocode.com/Articles/231.aspx



(e-mail address removed) schreef:
Hi
I have a aspx application written in c#.
I have created a datagrid control on the page.
One of the columns in the control is a dropdownlist.
I would like to set the data source of the dropdownlist to a different
recordset for each row.
How can I do this in C#?

I have used the ItemDataBound event, to set the DataSource for each row
of the dropdownlist.
Running the code in debug mode, I can see that the DataSource is being
reset, however, when my page comes up, there are two rows in the data
grid, both drop down lists have the same data source.

private void dgInvoiceList_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e )
{
DropDownList ddlCategory =
(DropDownList)e.Item.FindControl("ddlCategory");

switch ((int)(e.Item.ItemType))
{ {
case (int)ListItemType.Item :

if( (e.Item.Cells[13].Text ==
GTS.Common.GTSConsts.CostCategoryTypeCode_BookingExtentionExpense) |
(e.Item.Cells[13].Text ==
GTS.Common.GTSConsts.CostCategoryTypeCode_BookingExtensionInternalAir)
)
{
ddlCategory.DataSource = mdsCostCategories_Extensions.Tables[0];
ddlCategory.DataBind();
}
break;

case (int)ListItemType.AlternatingItem:
if( (e.Item.Cells[13].Text ==
GTS.Common.GTSConsts.CostCategoryTypeCode_BookingExtentionExpense) |
(e.Item.Cells[13].Text ==
GTS.Common.GTSConsts.CostCategoryTypeCode_BookingExtensionInternalAir)
)
{
ddlCategory.DataSource = mdsCostCategories_Extensions.Tables[0];
ddlCategory.DataBind();
}
break;
}
}
 
Back
Top