Click here to Skip to main content
14,882,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I have gridview as follows:

XML
<asp:GridView ID="role_update" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="role_update_RowCancelingEdit"
                                OnRowEditing="role_update_RowEditing" OnRowUpdating="role_update_RowUpdating"
                                CssClass="GridView">
                                <Columns>
                                    <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Center">
                                        <ItemTemplate>
                                            <asp:Label ID="lblname" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Code" HeaderStyle-HorizontalAlign="Center">
                                        <ItemTemplate>
                                            <asp:Label ID="lblcode" runat="server" Text='<%# Bind("code") %>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Role" HeaderStyle-HorizontalAlign="Center">
                                        <ItemTemplate>
                                            <asp:Label ID="role_label" runat="server" Text='<%# Bind("role") %>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <EditItemTemplate>
                                            <asp:DropDownList runat="server" ID="role_list" DataTextField="rolename" DataValueField="role" >
                                            </asp:DropDownList>
                                        </EditItemTemplate>
                                    </asp:TemplateField>
                                    <asp:CommandField HeaderText="Action" ShowEditButton="True" ButtonType="Button">
                                        <ControlStyle CssClass="btn" />
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:CommandField>
                                </Columns>
                            </asp:GridView>


I want such that,when i click on edit,the role column dropdown list should be binded at that time,so i tried that in row editing event as follows:


C#
protected void role_update_RowEditing(object sender, GridViewEditEventArgs e)
        {            
            role_update.EditIndex = e.NewEditIndex;
            GridViewRow row = role_update.Rows[e.NewEditIndex];
            DropDownList dl = (DropDownList)row.FindControl("role_list");
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString=      System.Configuration.ConfigurationManager.ConnectionStrings["xyz"].ConnectionString;
            conn.Open();
            SqlCommand cmd = new SqlCommand("select  DISTINCT role from table", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dl.DataSource = ds;
            dl.DataBind();
            conn.Close();
            rolebind();
        }

The problem is,i am getting object reference error at the line highlighted above,it means i am getting null dropdown as role_list is not there in itemtemplate,its in edititemtemplate.

My question is how to bind dropdown at time of editing?

I googled alot but every example shows binding in rowdatabound event,thats useless for me(as per as i think).If not,then how to accomplish that?

Any help would be really appreciated.
Posted

1 solution

You are using wrong event to bind your gridview. Use RowDataBound event to bind. For example:

C#
protected void YourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
Control dl = e.Row.FindControl("yourDropDownList");
if (dl != null)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString=      System.Configuration.ConfigurationManager.ConnectionStrings["xyz"].ConnectionString;
conn.Open();
SqlCommand cmd = new SqlCommand("select  DISTINCT role from table", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dl.DataSource = ds;
dl.DataBind();
conn.Close();
rolebind();
}
}
   
Comments
Sejal Rabari 5-Jun-13 3:21am
   
I appreciate this one..!
Zafar Sultan 5-Jun-13 3:38am
   
You're welcome.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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