DataGrid with Multiple DropdownList's - need to Populate dynamical

P

PK9

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!
}
 
I

iengine

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
 
A

addup

PK9 said:
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!
}

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 --
 
P

PK9

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!!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,708
Messages
2,569,347
Members
44,653
Latest member
Angus30T19

Latest Threads

Top