Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dropdown list in gridview

Status
Not open for further replies.

sds814

Programmer
Feb 18, 2008
164
US
ddlExpenseSubType is a subcategory of ddlExpenseType. So the list in ddlExpenseSubType depends on the value selected in ddlExpenseType and the dataset tdsExpenseSubType. Here is the function that loads ddlExpenseSubType:

LoadExpenseSubType(ref ddlExpenseSubType, string ExpenseTypeCD_F)

tdsStaticReference.ExpenseSubType.Select("ExpenseTypeCD_F = '" + ExpenseTypeCD + "'");
ddlExpenseSubType.DataSource = tdsStaticReference.ExpenseSubType;

ddlExpenseSubType.DataValueField = tdsStaticReference.ExpenseSubType.ExpenseSubTypeCDColumn.ColumnName;

ddlExpenseSubType.DataTextField = tdsStaticReference.ExpenseSubType.ExpenseSubTypeDescriptionColumn.ColumnName;

ddlExpenseSubType.DataBind();

After the ExpenseSubType is selected from the dropdownlist and an amount is entered for the Expense SubType, the two values are entered into a gridview. What I am trying to do now is that if the user wanted to edit the data in the gridview the same Expense Subtypes appear in the EditItemTemplate of the gridview. The ExpenseType is saved in a session, Session["ExpenseTypeCD_F"]. Here is what I have so far:

<asp:TemplateField SortExpression="ExpenseSubTypeCD_F" HeaderText="SubType">

<EditItemTemplate>

<asp:DropDownList ID="ddlSubType" runat="server" DataSource='<%#Common.LoadExpenseSubType(ref ddlSubType, Session["ExpenseTypeCD_F"])%>' />

</EditItemTemplate>

<ItemTemplate>

<asp:Label runat="server" Text= '<%# Bind("ExpenseTypeCD_F") %>' ID="Label"></asp:Label>

</ItemTemplate>

But this gives me an error: The name 'ddlSubType' does not exist in the current context.

Please let me know the correct way of doing this.
 
ddlSubType is a control within the gridview's edit template. therefore to reference the control you need to Find() it and then cast it.

within the GridViewRowDataBound event
Code:
private void GridViewRowDataBound(object sender, GridViewRowDataBoundEventArgs e)
{
   if(e.Row.RowIndex > -1)
   {
      DropDownList ddl = e.Row.FindControl("ddlSubType") as DropDownList;
      if(ddl != null)
         Common.LoadExpenseSubType(ref ddl, Session["ExpenseTypeCD_F"]);
   }
}
disclaimer: i'm sure there are syntax and member errors above, but this is the general idea.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks, Jason. I'll give that a try tonight.

Another question that I have relating to this is that when I click on the Edit link button to change the SubType label in the ItemTemplate to a ddlSubType in the EditItemTemplate, the server control doesn't change.

Do I have to write any code in the RowEdit event as that is the event that is triggered when I click on the edit linkbutton?

Thanks.
 
not sure I completely understand. you never change controls as in making a label a drop down list. you control which template is rendered by changing the RowState (or RowType).

since this control only exists durning an edit then the row editing event would be a better place to put this code. the row data bound event only fires when a row is databound, not when a row is created or changes state.

you also should not need to explicitly declare the ddl paremeter as a ref this is the case by default.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason: I think you were telling me to put the code below in the RowEditing event of the GridView:

Code:
   if(e.Row.RowIndex > -1)
   {
      DropDownList ddl = e.Row.FindControl("ddlSubType") as DropDownList;
      if(ddl != null)
         Common.LoadExpenseSubType(ref ddl, Session["ExpenseTypeCD_F"]);
   }

However, Row isn't a property of the GridViewEditEventArgs e. And like you said, when I have the above code in the RowDataBound event of the GridView, ddlSubType isn't a recognized server control as it only exists during an Edit.
 
the edit event will provide the RowIndex. use this to get a reference the gridviewrow
Code:
GridViewRow row = MyGridView.Rows[e.RowIndex];

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Well, I tried something similar to this in the RowEditing event of the gridview:

Code:
(DropDownList) ddl = (DropDownList) gvExpenseSubType.Rows[e.NewEditIndex].Cells[1].FindControl("ddlSubType")

But this gave me a null back. I found out from these posts: and that in RowEditing you cannot access the server controls in the EditItemTemplate of the RowEditing event. I tried to access the server control in the RowCreated event, but when I click the edit button the RowState is still Normal. Theres a more detailed explanation in:
Thanks for the help.
 
JBenson: I've seen many sites where the developer used the RowDataBound event to bind controls that are in the EditItemTemplate. Here is an example:

Code:
if ((e.Row.RowType == DataControlRowType.DataRow) && (e.Row.RowState & DataControlRowState.Edit) > 0)
{
   Common.LoadExpenseSubType(ddl, Session["ExpenseTypeCD_F"]);
}

In the RowDataBound event, why would the RowState be in Edit mode? Doesn't the RowState change to edit when the edit button is clicked on a row? When I clicked on the edit button, the events that were triggered included RowCreated and RowEditing and the code in RowDataBound was not called.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top