Drop Down List / GridView

OK, so I have a Gridview with dropdownlist in each row.

Now I have a list of account numbers each one has different product codes how can I get the different product codes into each row of the Gridview within the dropdownlist?

For example:
Account              ProductCode (Dropdownlist)
12345                 10,20,30
45678                  50,90
75945                  80,40,60

I want the user to select the correct product code from the drop down list for that account.

So my datatable looks like this:
12345 10
12345 20
12345 30
45678 50
45678 90
75945 80
75945 40
75945 60

Thanks,
Mark
smithmrkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Use the row_databinding event.

From here:
http://technico.qnownow.com/how-to-bind-dropdownlist-in-gridview-rowdatabound/

C#
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.DataItem != null)
        {
            //check if is in edit mode . . . you may not need to do this, remove the if.
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddlSubCategories = (DropDownList)e.Row.FindControl("ddlSubCategories");
                //Bind subcategories data to dropdownlist
                ddlSubCategories.DataTextField = "SubCategoryName";
                ddlSubCategories.DataValueField = "ProductSubcategoryID";
                ddlSubCategories.DataSource = RetrieveSubCategories();
                ddlSubCategories.DataBind();
                DataRowView dr = e.Row.DataItem as DataRowView;
                ddlSubCategories.SelectedValue = dr["ProductSubCategoryID"].ToString();
            }
        }
    }
}

Open in new window


VB (converted by telerik's convertor).
Protected Sub gvProducts_RowDataBound(sender As Object, e As GridViewRowEventArgs)
	If e.Row.RowType = DataControlRowType.DataRow Then
		If e.Row.DataItem IsNot Nothing Then
			'check if is in edit mode . . . you may not need to do this, remove the if.
			If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
				Dim ddlSubCategories As DropDownList = DirectCast(e.Row.FindControl("ddlSubCategories"), DropDownList)
				'Bind subcategories data to dropdownlist
				ddlSubCategories.DataTextField = "SubCategoryName"
				ddlSubCategories.DataValueField = "ProductSubcategoryID"
				ddlSubCategories.DataSource = RetrieveSubCategories()
				ddlSubCategories.DataBind()
				Dim dr As DataRowView = TryCast(e.Row.DataItem, DataRowView)
				ddlSubCategories.SelectedValue = dr("ProductSubCategoryID").ToString()
			End If
		End If
	End If
End Sub

Open in new window

0
smithmrkAuthor Commented:
OK, Let me take a look and get back to you...I was off on Friday and trying to catch up.

Thanks,
Mark
0
smithmrkAuthor Commented:
OK, I looked at what you provided however that isn't quite what I'm looking for...

My data is formatted like this:

So my datatable looks like this:
12345 10
12345 20
12345 30
45678 50
45678 90
75945 80
75945 40
75945 60

For every repeating account number (first column) I want the second column loaded to the drop down list.

Mark
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Kyle AbrahamsSenior .Net DeveloperCommented:
can you post your gridview front end so I can see your columns?
0
smithmrkAuthor Commented:
When you say front end...do you mean my code or the results that display on the screen?

Mark
0
Kyle AbrahamsSenior .Net DeveloperCommented:
The markup (html) that is used for your gridview.  I'm thinking you want the drop down driven off of a different column correct?
0
smithmrkAuthor Commented:
<asp:GridView ID="GV_DE_AcctDifferences" runat="server" AutoGenerateColumns="False"
        BorderColor="Black" Font-Bold="False" TabIndex="1" Width="800px">
        <Columns>
            <asp:TemplateField HeaderText="Billing Month" Visible="False">
                <ItemTemplate>
                    <asp:Label ID="lbl_BillingMonth" runat="server" Text='<%# Eval("BillingMonth") %>'></asp:Label>
                </ItemTemplate>
                <ControlStyle Width="20px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Billing Year" Visible="False">
                <ItemTemplate>
                    <asp:Label ID="lbl_BillingYear" runat="server" Text='<%# Eval("BillingYear") %>'></asp:Label>
                </ItemTemplate>
                <ControlStyle Width="20px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Bank Number">
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server">
                    </asp:DropDownList>
                </ItemTemplate>
                <ControlStyle Width="200px" />
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Account Number">
                <ItemTemplate>
                    <asp:Label ID="lbl_AccountNum" runat="server" Text='<%# Eval("AccountNum") %>'></asp:Label>
                </ItemTemplate>
                <ControlStyle Width="150px" />
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ProductCodeList" Visible="False">
                <ItemTemplate>
                    <asp:Label ID="lbl_ProductCodeList" runat="server" Text='<%# Eval("ProductCode") %>'></asp:Label>
                </ItemTemplate>
                <ControlStyle Width="20px" />
            </asp:TemplateField>            
            <asp:TemplateField HeaderText="Product Code">
                <ItemTemplate>
                    <asp:DropDownList ID="ddl_ProductCode" runat="server">
                    </asp:DropDownList>
                </ItemTemplate>
                <ControlStyle Width="150px" />
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Something like this:

Essentially get the account control and the drop down list.
Set the datatext / member to be product code
Set the dataSource to be the rows of your data table where the account code matches the account label text.
Databind the ddl
Optionally if you have a selected value for the product code, set that as well.


Protected Sub gvProducts_RowDataBound(sender As Object, e As GridViewRowEventArgs)
	If e.Row.RowType = DataControlRowType.DataRow Then
		If e.Row.DataItem IsNot Nothing Then
			'check if is in edit mode . . . you may not need to do this, remove the if.
			If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
                                Dim account as Label =DirectCast(e.Row.FindControl("lbl_AccountNum"), DropDownList)
				Dim ddlProductCode As DropDownList = DirectCast(e.Row.FindControl("ddl_ProductCode"), DropDownList)
				'Bind ddlProductCode data to dropdownlist
				ddlProductCode .DataTextField = "ProductCode"
				ddlProductCode .DataValueField = "ProductCode"
'limit t
				ddlProductCode .DataSource = theDataTable.Rows.OfType(DataRow).Where(Function(row as DataRow) row["Account"].ToString() = lblAccount.Text)
				ddlProductCode .DataBind()
				Dim dr As DataRowView = TryCast(e.Row.DataItem, DataRowView)
				ddlProductCode.SelectedValue = dr("ProductCode").ToString()
			End If
		End If
	End If
End Sub

Open in new window

0
smithmrkAuthor Commented:
OK, well I'm getting errors with your code:

theDataTable.Rows.OfType = "OfType is not a member of System.Data.DataRowCollection
Function(row as DataRow) = Expression Expected

Mark
0
Kyle AbrahamsSenior .Net DeveloperCommented:
I'm missing an extra set of parentheses . . . sorry, used to c#
.OfType(DataRow)()
0
smithmrkAuthor Commented:
No, OfType is not an option?
I'm using .net 2.0...does that make a difference?

Mark
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Ah, yeah . . . you need at least 3.5 to use LINQ.

Basically you need to apply a filter to your data source, and then apply that to the dropdown list.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smithmrkAuthor Commented:
OK, well thanks for your help...I'll try a different approach!

Mark
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.