views:

29

answers:

3

hi I have this gridview like this.

<asp:DropDownList ID="triggerDropDown" runat="server" AutoPostBack="true" onselectedindexchanged="triggerDropDown_SelectedIndexChanged">

<asp:GridView ID="myGridView" run="server">
     <Columns>
          <asp:TemplateField HeaderText="Column 1">
               <ItemTemplate>
                   <asp:DropDownList ID="myDropDown1" runat="server" />
               </ItemTemplate>
          </asp:TemplateField>

          <asp:TemplateField HeaderText="Column 2">
               <ItemTemplate>
                   <asp:DropDownList ID="myDropDown2" runat="server" />
               </ItemTemplate>
          </asp:TemplateField>
     </Columns>
</asp:GridView>

now, when I change my triggerDropDown I want to change also all of the DropDowns inside Column 1 how can I do that?

protected void triggerDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    // what should I do here?
}
+1  A: 

Inside your event method you should access the DropDownList that resides within each row of the GridView. Doing this you can bind each DropDownList to whatever data you want.

This link shows you how to do that:

http://www.velocityreviews.com/forums/t191319-need-help-with-accessing-a-control-within-a-template-field.html

Basically:

  • Iterate over each row of your GridView;

  • Find the DropDownList control with something like:

    DropDownList mddl = GridView.Rows[2].FindControl("myDropDown1");

  • Bind new data to mddl.

Leniel Macaferi
actually the event is not part of the gridview but outside of the gridview. In my case the gridview is already bounded and the dropdowns inside the gridview is also bounded. I just want to re-bind the dropdowns inside the gridview if the outside dropdown is changed.
rob waminal
A: 

The gridview is very likely not what you want here. The way to change the value of a control contained in a row is usually through grabbing a handle to the desired control using e.Item.FindControl() from within the ItemDataBound event of the gridview.

The problem with your approach is that you're wanting a control outside of the gridview (triggerDropDown) to interact with a single row of the gridview. Do you want the first row, first column, last row, first column or first column for each of the items in the grid? It's probably better you take the target of your trigger dropdown and place it outside of the gridview and deal with it directly.

If you really intend to change items in a row in the grid consider doing so in the ItemDataBound event of the gridview and you'll find lots of examples out there.

Tahbaza
no, not a single row but a single column of the entire row.
rob waminal
@rob w: Will you ever have more than 1 row in your gridview? That will determine the potential solutions you have to choose from.
Tahbaza
@Tahbaza, yes...
rob waminal
you can check the e.rowindex property within the ItemDataBound event of the gridview, reference the desired control withing the desired row with e.Item.FindControl and examine the currently selected value of the trigger dd directly by control name to do what you're trying to do.
Tahbaza
A: 

Actually I can use GridViewRow :) I just have to find the GridViewControl and get its Rows attribute which is a GridViewRow and now I can do a foreach of each row.

foreach (GridViewRow gridViewRow in (this.FindControl("myGridView") as GridView).Rows)
{
    // I can see all elements of my row here as if I am traversing on GridViewEvents
}
rob waminal