views:

98

answers:

1

I'm defining a GridView as follows:

        <asp:GridView ID="MediaViewsGrid" DataSourceID="MediaViewsDataSource"
    runat="server" 
    AutoGenerateColumns="False">
        <Columns>
            <asp:CommandField ShowEditButton="True" ShowDeleteButton="true" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblLanguage" runat="server" Text='<%# Eval("Language.Name") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                <asp:DropDownList ID="ddlLanguages"
                                  DataSourceID="LanguageDataSource" 
                                  DataTextField="Name" 
                                  DataValueField="Code"
                                  OnDataBound="ddlLanguages_OnDataBound"                                      
                                  runat="server" />
                </EditItemTemplate>
            </asp:TemplateField>

I'm trying to set a default value for my ddlLanguages drop down list when user clicks on "edit". Is the correct solution to define an OnDataBound function and set the selected index there?

If so, how do i do it? Or is there a better solution?

A: 

Your subject says datagrid, but your code says gridview. I'll assume gv.

Here's a sample:

protected void gvOrderSummary_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow &&
        (e.Row.RowState == DataControlRowState.Edit ||
            (e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))))
    {
        ((DropDownList)e.Row.Cells[4].Controls[1]).SelectedValue = DataBinder.Eval(e.Row.DataItem, "orderStatusId").ToString();
    }
}
Steve
Thanks, works like a charm!
EdanB