Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
I have gridview class and I am trying to display student with their marks.
But when I do I am getting the error -  "ddlclass" has a selectedvalue which is invalid because it does not exist in the list of items dropdownlist.

Class.aspx
<asp:GridView ID="Gridviewclass" runat="server" >
      <Columns>
            <asp:TemplateField HeaderText="Myclass">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlclass" runat="server" DataTextField="Name" DataValueField="classID"
SelectedValue='<%# Bind("classID") %>' >
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Marks" SortExpression="Marks">
                <ItemTemplate>
                    <asp:TextBox ID="txtMarks" runat="server" Text='<%# Bind("Marks") %>' ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>



Class.aspx.cs
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 Gridviewclass.DataSource = mainclass
            Gridviewclass.DataBind()
End Sub
Posted
Updated 8-Sep-15 7:19am
v2
Comments
F-ES Sitecore 8-Sep-15 13:17pm    
That happens when you give a dropdown a SelectedValue that does not match anything it has in its Items collection. Can't say what the issue is from what you've posted though. Would need to post where you set the selected value, how your dropdowns are populated, what you mean by "dynamically" etc.
apr1234 8-Sep-15 14:00pm    
Updated code:

Class.aspx
<asp:GridView ID="Gridviewclass" runat="server" >
<columns>
<asp:TemplateField HeaderText="Myclass">
<itemtemplate>
<asp:DropDownList ID="ddlclass" runat="server" DataTextField="Name" DataValueField="classID" DataSource='<%# dtclass %>'
SelectedValue='<%# Bind("classID") %>' >



<asp:TemplateField HeaderText="Marks" SortExpression="Marks">
<itemtemplate>
<asp:TextBox ID="txtMarks" runat="server" Text='<%# Bind("Marks") %>' >







Class.aspx.cs

Public dtclass As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetclassDataTable()
Gridviewclass.DataSource = mainclass
Gridviewclass.DataBind()
End Sub


Private Function GetclassDataTable() As DataTable

If Cache.Item("class") Is Nothing Then
dtclass.Columns.Add(New DataColumn("Name"))
dtclass.Columns.Add(New DataColumn("ID"))
dtclass = classes.Getclass()
Return dtAttorneys
Else
Return DirectCast(Cache.Item("class"), DataTable)
End If

End Function

You are getting error because no listitem is not set in dropdown when a selected value is set. So in below code I added demo ListItem in dropdown control(you can change it as per your requirement).
XML
<asp:GridView ID="Gridviewclass" runat="server" >
      <Columns>
            <asp:TemplateField HeaderText="Myclass">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlclass" runat="server" DataTextField="Name" DataValueField="classID"
SelectedValue='<%# Bind("classID") %>' >
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                        <asp:ListItem>4</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Marks" SortExpression="Marks">
                <ItemTemplate>
                    <asp:TextBox ID="txtMarks" runat="server" Text='<%# Bind("Marks") %>' ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Note: If you want to bind data to dropdown from database then you need to use OnRowDataBound property of gridview. Please use below link for reference.

http://www.aspsnippets.com/Articles/How-to-populate-DropDownList-in-GridView-in-ASPNet.aspx[^]
 
Share this answer
 
Comments
apr1234 8-Sep-15 14:12pm    
It worked... Thank you Manas!
I think you must be binding the dropdownlist inside the GridView rowdatabound event.

Now you just need to check if the classid for the row is present as a value of the dropdownlist. Seems like for some row the classid is not matching in the list of drop down values.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900