DataGrid with Multiple DropdownList's - need to Populate dynamical

Discussion in 'ASP .Net Datagrid Control' started by PK9, Jul 16, 2006.

  1. PK9

    PK9 Guest

    Need help with a DataGrid DropDownList problem. I have a datagrid that
    contains several columns, including two ItemTemplate columns that have
    DropDownList controls. The DropDownList controls are in columns titled
    "Short Title" and "Editions". When a user makes a selection from one of the
    "Short Title" dropdown lists in the DataGrid, I need to populate the
    "Editions" DropDownList with the necessary editions in the row of the
    selected Short Title. This is done by querying the database with the
    selected short title.

    The problem is that I cannot find a way to identify the necessary edition
    dropdown list to populate in the datagrid! I am using an
    "OnSelectedIndexChanged" event with the Short Title dropdownlist to retrieve
    the selected short title. With this I can successfully query the database
    and retrieve the applicable editions. However, I cannot bind these editions
    to the correct "Edition" DropdownList in the datagrid because I cannot figure
    out how to identify which row had the Short Title DropDownList selected. I
    do have a column titled "RowNumber" in my datagrid that would allow me to
    find the correct DropDownList in my event handler, but I cannot figure out
    how to pass the value along in my event handler.

    My current code-set is below:

    ASP.NET CODE (datagrid):
    __________________________________________
    <asp:DataGrid id="dgKeyRequests" Runat=server>
    <Columns>
    <asp:BoundColumn DataField="RowNumber" HeaderText="Row #" >
    <asp:TemplateColumn HeaderText="Short Title">
    <ItemTemplate>
    <asp:DropDownList AutoPostBack=True
    OnSelectedIndexChanged="PopulateEditions" Runat="server"
    ID="dgKeyRequest_lstShortTitle"/>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Start Edition">
    <ItemTemplate>
    <asp:DropDownList Runat="server" ID="dgKeyRequest_lstEditions" />
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </DataGrid>

    C# CODE BEHIND (OnSelectedIndexChanged event handler):
    __________________________________________________
    public void PopulateEditions(object sender, System.EventArgs e)
    {
    //1) Get the ID of the selected short title from the dropdown list in the
    datagrid
    string strShortTitleID = ((DropDownList)sender).SelectedValue.ToString();

    //2) USE THE strShortTitleID variable to retrieve the Editions from the
    database
    ...

    //3) Bind Editions to the Edition DropdownList in the same row of the
    // selected short title
    CANNOT FIGURE OUT THIS STEP!
    }
    --
    PK9
    PK9, Jul 16, 2006
    #1
    1. Advertising

  2. PK9

    iengine Guest

    You need to remember the choice of first DropDownList
    Then query from database by that choice and fill into the selection of
    second DropDownList when handling the dropdown event of the second
    DropDownList

    "PK9" <> wrote in message
    news:...
    > Need help with a DataGrid DropDownList problem. I have a datagrid that
    > contains several columns, including two ItemTemplate columns that have
    > DropDownList controls. The DropDownList controls are in columns titled
    > "Short Title" and "Editions". When a user makes a selection from one of
    > the
    > "Short Title" dropdown lists in the DataGrid, I need to populate the
    > "Editions" DropDownList with the necessary editions in the row of the
    > selected Short Title. This is done by querying the database with the
    > selected short title.
    >
    > The problem is that I cannot find a way to identify the necessary edition
    > dropdown list to populate in the datagrid! I am using an
    > "OnSelectedIndexChanged" event with the Short Title dropdownlist to
    > retrieve
    > the selected short title. With this I can successfully query the database
    > and retrieve the applicable editions. However, I cannot bind these
    > editions
    > to the correct "Edition" DropdownList in the datagrid because I cannot
    > figure
    > out how to identify which row had the Short Title DropDownList selected.
    > I
    > do have a column titled "RowNumber" in my datagrid that would allow me to
    > find the correct DropDownList in my event handler, but I cannot figure out
    > how to pass the value along in my event handler.
    >
    > My current code-set is below:
    >
    > ASP.NET CODE (datagrid):
    > __________________________________________
    > <asp:DataGrid id="dgKeyRequests" Runat=server>
    > <Columns>
    > <asp:BoundColumn DataField="RowNumber" HeaderText="Row #" >
    > <asp:TemplateColumn HeaderText="Short Title">
    > <ItemTemplate>
    > <asp:DropDownList AutoPostBack=True
    > OnSelectedIndexChanged="PopulateEditions" Runat="server"
    > ID="dgKeyRequest_lstShortTitle"/>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="Start Edition">
    > <ItemTemplate>
    > <asp:DropDownList Runat="server" ID="dgKeyRequest_lstEditions" />
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </DataGrid>
    >
    > C# CODE BEHIND (OnSelectedIndexChanged event handler):
    > __________________________________________________
    > public void PopulateEditions(object sender, System.EventArgs e)
    > {
    > //1) Get the ID of the selected short title from the dropdown list in
    > the
    > datagrid
    > string strShortTitleID =
    > ((DropDownList)sender).SelectedValue.ToString();
    >
    > //2) USE THE strShortTitleID variable to retrieve the Editions from the
    > database
    > ...
    >
    > //3) Bind Editions to the Edition DropdownList in the same row of the
    > // selected short title
    > CANNOT FIGURE OUT THIS STEP!
    > }
    > --
    > PK9
    iengine, Jul 18, 2006
    #2
    1. Advertising

  3. PK9

    addup Guest

    PK9 wrote:
    > Need help with a DataGrid DropDownList problem. I have a datagrid that
    > contains several columns, including two ItemTemplate columns that have
    > DropDownList controls. The DropDownList controls are in columns titled
    > "Short Title" and "Editions". When a user makes a selection from one of the
    > "Short Title" dropdown lists in the DataGrid, I need to populate the
    > "Editions" DropDownList with the necessary editions in the row of the
    > selected Short Title. This is done by querying the database with the
    > selected short title.
    >
    > The problem is that I cannot find a way to identify the necessary edition
    > dropdown list to populate in the datagrid! I am using an
    > "OnSelectedIndexChanged" event with the Short Title dropdownlist to retrieve
    > the selected short title. With this I can successfully query the database
    > and retrieve the applicable editions. However, I cannot bind these editions
    > to the correct "Edition" DropdownList in the datagrid because I cannot figure
    > out how to identify which row had the Short Title DropDownList selected. I
    > do have a column titled "RowNumber" in my datagrid that would allow me to
    > find the correct DropDownList in my event handler, but I cannot figure out
    > how to pass the value along in my event handler.
    >
    > My current code-set is below:
    >
    > ASP.NET CODE (datagrid):
    > __________________________________________
    > <asp:DataGrid id="dgKeyRequests" Runat=server>
    > <Columns>
    > <asp:BoundColumn DataField="RowNumber" HeaderText="Row #" >
    > <asp:TemplateColumn HeaderText="Short Title">
    > <ItemTemplate>
    > <asp:DropDownList AutoPostBack=True
    > OnSelectedIndexChanged="PopulateEditions" Runat="server"
    > ID="dgKeyRequest_lstShortTitle"/>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn HeaderText="Start Edition">
    > <ItemTemplate>
    > <asp:DropDownList Runat="server" ID="dgKeyRequest_lstEditions" />
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </DataGrid>
    >
    > C# CODE BEHIND (OnSelectedIndexChanged event handler):
    > __________________________________________________
    > public void PopulateEditions(object sender, System.EventArgs e)
    > {
    > //1) Get the ID of the selected short title from the dropdown list in the
    > datagrid
    > string strShortTitleID = ((DropDownList)sender).SelectedValue.ToString();
    >
    > //2) USE THE strShortTitleID variable to retrieve the Editions from the
    > database
    > ...
    >
    > //3) Bind Editions to the Edition DropdownList in the same row of the
    > // selected short title
    > CANNOT FIGURE OUT THIS STEP!
    > }
    > --
    > PK9


    you've already gotten ((DropDownList)sender)
    .... so close !

    extending that,

    ((DropDownList)sender).Parent is the table cell.,
    ((DropDownList)sender).Parent.Parent is the DataGridItem housing the
    cell

    so
    ((DropDownList)((DataGridItem)((DropDownList)sender).Parent.Parent).FindControl("dgKeyRequest_lstEditions"))

    is a reference to the second drop down list, in the same datagriditem
    (i.e. datagrid row)

    from there, binding data is a trivial excercise.

    DISCLAIMER: I'm not a C# guy, so my syntax may be messed up. In VB, it
    would be

    Dim dgKeyRequest_lstEditions As DropDownList
    dgKeyRequest_lstEditions = CType(CType(CType(sender,
    DropDownList).Parent.Parent,
    DataGridItem).FindControl("dgKeyRequest_lstEditions"), DropDownList)
    dgKeyRequest_lstEditions.DataSource = whatever
    dgKeyRequest_lstEditions.DataBind()

    DISCLAIMER #2: Just because something *can* be done may be not
    sufficient reason to *do* it. Are you sure you *want* to do it this
    way?

    Hope this helps.
    -- a --
    addup, Jul 20, 2006
    #3
  4. PK9

    PK9 Guest

    Re: DataGrid with Multiple DropdownList's - need to Populate dynam

    That is PERFECT! I haven't tried it yet, but that's exactly what I was
    looking for. Thank you very much.

    As for your question as to am I sure that is how I want to do it - I don't
    know of any other way to accomplish this. The page is dynamic in that they
    can extend the number of rows in the datagrid at any point in time by
    specifiying the number of empty rows to add and clicking a button. The
    edition options in each row will always be dependent on the short title
    selection for that row. If you have any other suggestions I'm all ears.

    Thanks again!!
    --
    PK9


    "addup" wrote:

    > PK9 wrote:
    > > Need help with a DataGrid DropDownList problem. I have a datagrid that
    > > contains several columns, including two ItemTemplate columns that have
    > > DropDownList controls. The DropDownList controls are in columns titled
    > > "Short Title" and "Editions". When a user makes a selection from one of the
    > > "Short Title" dropdown lists in the DataGrid, I need to populate the
    > > "Editions" DropDownList with the necessary editions in the row of the
    > > selected Short Title. This is done by querying the database with the
    > > selected short title.
    > >
    > > The problem is that I cannot find a way to identify the necessary edition
    > > dropdown list to populate in the datagrid! I am using an
    > > "OnSelectedIndexChanged" event with the Short Title dropdownlist to retrieve
    > > the selected short title. With this I can successfully query the database
    > > and retrieve the applicable editions. However, I cannot bind these editions
    > > to the correct "Edition" DropdownList in the datagrid because I cannot figure
    > > out how to identify which row had the Short Title DropDownList selected. I
    > > do have a column titled "RowNumber" in my datagrid that would allow me to
    > > find the correct DropDownList in my event handler, but I cannot figure out
    > > how to pass the value along in my event handler.
    > >
    > > My current code-set is below:
    > >
    > > ASP.NET CODE (datagrid):
    > > __________________________________________
    > > <asp:DataGrid id="dgKeyRequests" Runat=server>
    > > <Columns>
    > > <asp:BoundColumn DataField="RowNumber" HeaderText="Row #" >
    > > <asp:TemplateColumn HeaderText="Short Title">
    > > <ItemTemplate>
    > > <asp:DropDownList AutoPostBack=True
    > > OnSelectedIndexChanged="PopulateEditions" Runat="server"
    > > ID="dgKeyRequest_lstShortTitle"/>
    > > </ItemTemplate>
    > > </asp:TemplateColumn>
    > > <asp:TemplateColumn HeaderText="Start Edition">
    > > <ItemTemplate>
    > > <asp:DropDownList Runat="server" ID="dgKeyRequest_lstEditions" />
    > > </ItemTemplate>
    > > </asp:TemplateColumn>
    > > </Columns>
    > > </DataGrid>
    > >
    > > C# CODE BEHIND (OnSelectedIndexChanged event handler):
    > > __________________________________________________
    > > public void PopulateEditions(object sender, System.EventArgs e)
    > > {
    > > //1) Get the ID of the selected short title from the dropdown list in the
    > > datagrid
    > > string strShortTitleID = ((DropDownList)sender).SelectedValue.ToString();
    > >
    > > //2) USE THE strShortTitleID variable to retrieve the Editions from the
    > > database
    > > ...
    > >
    > > //3) Bind Editions to the Edition DropdownList in the same row of the
    > > // selected short title
    > > CANNOT FIGURE OUT THIS STEP!
    > > }
    > > --
    > > PK9

    >
    > you've already gotten ((DropDownList)sender)
    > .... so close !
    >
    > extending that,
    >
    > ((DropDownList)sender).Parent is the table cell.,
    > ((DropDownList)sender).Parent.Parent is the DataGridItem housing the
    > cell
    >
    > so
    > ((DropDownList)((DataGridItem)((DropDownList)sender).Parent.Parent).FindControl("dgKeyRequest_lstEditions"))
    >
    > is a reference to the second drop down list, in the same datagriditem
    > (i.e. datagrid row)
    >
    > from there, binding data is a trivial excercise.
    >
    > DISCLAIMER: I'm not a C# guy, so my syntax may be messed up. In VB, it
    > would be
    >
    > Dim dgKeyRequest_lstEditions As DropDownList
    > dgKeyRequest_lstEditions = CType(CType(CType(sender,
    > DropDownList).Parent.Parent,
    > DataGridItem).FindControl("dgKeyRequest_lstEditions"), DropDownList)
    > dgKeyRequest_lstEditions.DataSource = whatever
    > dgKeyRequest_lstEditions.DataBind()
    >
    > DISCLAIMER #2: Just because something *can* be done may be not
    > sufficient reason to *do* it. Are you sure you *want* to do it this
    > way?
    >
    > Hope this helps.
    > -- a --
    >
    >
    PK9, Jul 20, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Liqun Xu

    Click-Event by dynamical Button

    Liqun Xu, Dec 5, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    371
    Munsifali Rashid
    Dec 5, 2003
  2. mr2_93
    Replies:
    1
    Views:
    3,582
    mr2_93
    Oct 2, 2005
  3. Replies:
    3
    Views:
    423
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
    Aug 28, 2007
  4. Vaughn

    how do I populate a dropdownlist inside a datagrid

    Vaughn, Oct 16, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    106
    Vaughn
    Oct 16, 2003
  5. Enjoy Life
    Replies:
    2
    Views:
    257
    Roland Hall
    Feb 23, 2005
Loading...

Share This Page