dropdownlist in gridview for particular row

i have a dropdownlist in a gridview

            <asp:TemplateField HeaderText="Backup Frequency">
                <ItemTemplate>
     <asp:DropDownList ID="DropDownList1" runat="server">


    <asp:ListItem Text="Once a Day" Value="86400">Once a Day</asp:ListItem>
    <asp:ListItem Text="Once a weak" Value="604800">Once a week</asp:ListItem>
    <asp:ListItem Text="As They Change" Value="0">As They Change</asp:ListItem>

                </asp:DropDownList>
</ItemTemplate>
            </asp:TemplateField>

now it displays Once a Day in the dropdownlist in each row.... but i want that the first row should have its dropdownlist showing "As They Change"...

protected void GridView1_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow myRow in GridView1.Rows)
        {
            Label Label1 = (Label)myRow.FindControl("Label1");
            if (Label1.Text == "User Data")
            {DropDownList DropDownList1 = (DropDownList)myRow.FindControl("DropDownList1");
                DropDownList1.SelectedValue = "As They Change";
            }
        }
    }

but this is not showing As they change... Any suggestions??

13.10.2009 15:48:59
3 ОТВЕТА
РЕШЕНИЕ

You're setting SelectedValue but your DropDownList Values are "84600", "604800", "0". You need to set the SelectedValue to "0" for "As they change" to show.

2
13.10.2009 15:54:10
oh my god... really sorry for askin this question thanks tough
user175084 13.10.2009 15:57:40
No problem, I quite regularly can't see a blatant problem with my code until I've asked someone about it! ;)
DavidGouge 13.10.2009 16:02:27

The value of the drop down list you've shown is "0", so your code would need to set the selectedvalue value of the drop down list to 0. You are setting the selectedvalue to the text of the list item, but since list item values are all strings you will also need to use the string value of 0, like this:

protected void GridView1_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow myRow in GridView1.Rows)
        {
            Label Label1 = (Label)myRow.FindControl("Label1");
            if (Label1.Text == "User Data")
            {DropDownList DropDownList1 = (DropDownList)myRow.FindControl("DropDownList1");
                DropDownList1.SelectedValue = "0";
            }
        }
    }
1
13.10.2009 15:55:08
thanks... DropDownList1.SelectedValue = "0"; does the trick too
user175084 13.10.2009 15:58:37
yeah, 0.ToString() is just stupid. Typing faster than I was thinking.
kscott 13.10.2009 15:59:17

Shouldn't it be:

DropDownList1.SelectedValue = 0;

?

0
13.10.2009 15:55:34