vadim
vadim

Reputation: 211

SelectedValue which is invalid because it does not exist in the list of items

I am using a GridView and in it I have four columns: labelID, fName, lName and Grade. The Grade is a simple Pass or Fail Radiobuttonlist. Once the data is updated I would like it to pull the data on the next reload to show the selected value if the user has passed or failed. Here is the code:

<asp:TemplateField>
           <ItemTemplate>
             <asp:RadioButtonList ID="rblChoices" runat="server" OnSelectedIndexChanged="rblChoices_SelectedIndexChanged" Text='<%# Eval("Grade") %>'>
               <asp:ListItem Value="Pass" Text="Pass"></asp:ListItem>
                 <asp:ListItem Value="Fail" Text="Fail"></asp:ListItem>
             </asp:RadioButtonList>
            </ItemTemplate>
</asp:TemplateField>

C# code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindData();
    }
}
private void BindData()
{
    string connectiongString = "Data Source=WSCJTCSQ1;Initial Catalog=LiquorStore;Integrated Security=True";
    SqlConnection myConnection = new SqlConnection(connectiongString);
    SqlDataAdapter ad = new SqlDataAdapter("SELECT id, firstname, lastname, nickname, Grade FROM Company", myConnection);
    DataSet ds = new DataSet();
    ad.Fill(ds);
    gvUsers.DataSource = ds;
    gvUsers.DataBind();
}

Thank you in advance!

Upvotes: 0

Views: 2239

Answers (1)

Tariqulazam
Tariqulazam

Reputation: 4585

You have to use the GridView RowDataBound event for this

HTML

<asp:GridView runat="server" ID="gvUsers" OnRowDataBound="gvUsers_RowDataBound"  AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%# Eval("Name") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:RadioButtonList ID="rblChoices" runat="server">
                     <asp:ListItem Value="Pass" Text="Pass"></asp:ListItem>
                     <asp:ListItem Value="Fail" Text="Fail"></asp:ListItem>
                </asp:RadioButtonList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

C# Code

A very simple company class - Company.cs

public class Company
{
    public string Name { get; set; }
    public string Grade { get; set; }
}

.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{

    List<Company> companies = new List<Company>()
        {
            new Company(){ Name = "Toyota", Grade = "Pass"},
            new Company(){ Name = "Form", Grade = "Fail"}
        };

    gvUsers.DataSource = companies;
    gvUsers.DataBind();
}


protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.DataItem != null)
            {
                string grade = DataBinder.Eval(e.Row.DataItem, "Grade") as string;
                if (!string.IsNullOrEmpty(grade))
                {
                    RadioButtonList radio = e.Row.FindControl("rblChoices") as RadioButtonList;
                    radio.Items.FindByValue(grade).Selected = true;
                    //You can use this to select as well - see comments from Andomar
                    //radio.SelectedValue = grade;
                }
            }
        }
    }

OUTPUT

enter image description here

Upvotes: 2

Related Questions