1

I have to bind drop down list inside a grid view. While using the given below code showing one error. Help me to find a proper solution. Thank you.

Code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType = DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
    {
        DropDownList DropdownList1 = (DropDownList)e.Row.FindControl("DropdownList1");
        ShadingAnalysisDataSetTableAdapters.tbl_AutoAssignCadTeamTableAdapter at;
        at = new ShadingAnalysisDataSetTableAdapters.tbl_AutoAssignCadTeamTableAdapter();
        DataTable dt = new DataTable();
        dt=at.GetUpdateTeam();
        DropdownList1.DataSource = dt;
        DropdownList1.DataTextField = "Assigned_Team";
        DropdownList1.DataValueField = "Assigned_Team";
        DropdownList1.DataBind();
        DropdownList1.Items.FindByValue((e.Row.FindControl("Label1") as Label).Text).Selected = true;
    }
}

ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit">
    <Columns>
        <asp:BoundField DataField="State" HeaderText="State" ReadOnly="True" SortExpression="State" />
        <asp:BoundField DataField="District" HeaderText="District" ReadOnly="True" SortExpression="District" />
        <asp:BoundField DataField="Site_ID" HeaderText="Site ID" ReadOnly="True" SortExpression="Site_ID" />
        <asp:BoundField DataField="Site_Name" HeaderText="Site Name" ReadOnly="True" SortExpression="Site_Name" />
        <asp:TemplateField HeaderText="Team" SortExpression="Assigned_Team">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" Width="88px">
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Assigned_Team") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Created_Date" HeaderText="Created Date" ReadOnly="True" SortExpression="Created_Date" />
        <asp:BoundField DataField="Created_By" HeaderText="Created By" ReadOnly="True" SortExpression="Created_By" />
    </Columns>
</asp:GridView>
1
  • You are assigning e.Row.RowType = DataControlRowType.DataRow - you need double equals.
    – StuartLC
    Nov 3, 2014 at 4:56

2 Answers 2

2

I believe you have a typo in your if statement.

if (e.Row.RowType = DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)

Should be

if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)

Notice you wrote your first equals as an assignment statement but it should be a conditional equals.

1

You can do in this way:

if (e.Row.RowType == DataControlRowType.DataRow)
     {
        if (GridView1.EditIndex == e.Row.RowIndex)
        {     
        DropDownList DropdownList1 = (DropDownList)e.Row.FindControl("DropdownList1");
        ShadingAnalysisDataSetTableAdapters.tbl_AutoAssignCadTeamTableAdapter at;
        at = new ShadingAnalysisDataSetTableAdapters.tbl_AutoAssignCadTeamTableAdapter();
        DataTable dt = new DataTable();
        dt=at.GetUpdateTeam();
        DropdownList1.DataSource = dt;
        DropdownList1.DataTextField = "Assigned_Team";
        DropdownList1.DataValueField = "Assigned_Team";
        DropdownList1.DataBind();
        DropdownList1.Items.FindByValue((e.Row.FindControl("Label1") as Label).Text).Selected = true;
        }
      }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.