mary jean ligas

mary jean ligas

  • NA
  • 420
  • 196.3k

How to Populate Dropdownlist inside EditITemTemplate of gridview

Nov 7 2012 4:44 AM
Hello Everyone Please help me how to bind dropdownlist inside edit item template of gridview heres my code..

ASPX

<asp:GridView runat="server" ID="gridDept"
                BorderColor="#0070C0" GridLines="Both"
                OnRowDataBound="gridDept_RowDataBound"
                AutoGenerateColumns="False"
                CssClass="Content" CellPadding="3"
                onrowcancelingedit="gridDept_RowCancelingEdit"
                onrowdeleting="gridDept_RowDeleting"
                onrowediting="gridDept_RowEditing"
                onrowupdating="gridDept_RowUpdating"
                DataKeyNames="Dept_Idx">
                <Columns>
                    <asp:TemplateField>
                    <HeaderStyle BackColor="#0070C0"/>
                    <ItemTemplate>
                        <asp:ImageButton runat="server" ID="btnDelete" ImageUrl="~/images/delete.png" ToolTip="Delete this Item" CommandName="Delete" OnClientClick="showConfirm(this); return false;"/>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:ImageButton runat="server" ID="btnUndo" ImageUrl="~/images/undo.png" ToolTip="Undo Update" CommandName="Cancel" />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                     <HeaderStyle BackColor="#0070C0"/>
                    <ItemTemplate>
                        <asp:ImageButton runat="server" ID="btnEdit" ImageUrl="~/images/edit.gif" ToolTip="Edit this Item" CommandName="Edit" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:ImageButton runat="server" ID="btnSave" ImageUrl="~/images/save.png" ToolTip="Save Changes" CommandName="Update"/>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Seq No.">
                     <HeaderStyle BackColor="#0070C0" ForeColor="White"/>
                    <ItemTemplate>
                       <%# Eval("Dept_Sort")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtDept_Sort" Width="15px"></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Code">
                     <HeaderStyle BackColor="#0070C0" ForeColor="White"/>
                    <ItemTemplate>
                       <%# Eval("Dept_ShortName")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtDept_ShortName" Width="15px"></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Department Name">
                     <HeaderStyle BackColor="#0070C0" ForeColor="White"/>
                    <ItemTemplate>
                       <%# Eval("Dept_Name")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtDept_Name" Width="15px"></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Function Name">
                    <HeaderStyle BackColor="#0070C0" ForeColor="White"/>
                    <ItemTemplate>
                        <%# Eval("Func_Name") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList runat="server" ID="dropFunc" AutoPostBack="true" DataSourceID="SqlDataSource1" DataTextField="Func_Name" DataValueField="Func_Idx">
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                </Columns>
          </asp:GridView>

---------------------------
CS

protected void gridDept_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gridDept.EditIndex = -1;
            BindDept();
        }

        protected void gridDept_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int id = 0;
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                Dim_Department d = (e.Row.DataItem as Dim_Department);

                TextBox txtDept_Sort = (TextBox)e.Row.FindControl("txtDept_Sort");
                TextBox txtDept_Code = (TextBox)e.Row.FindControl("txtDept_ShortName");
                TextBox txtDept_Name = (TextBox)e.Row.FindControl("txtDept_Name");
               
               
                txtDept_Sort.Text = Convert.ToInt64(d.Dept_Sort).ToString();
                txtDept_Code.Text = d.Dept_ShortName;
                txtDept_Name.Text = d.Dept_Name;

              

            }
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {

                id = Int32.Parse(e.Row.Cells[0].ToString());

                Dim_Function d = (e.Row.DataItem as Dim_Function);
                DropDownList dropFunc = (DropDownList)e.Row.FindControl("dropFunc");
                dropFunc.DataSource = d;
                dropFunc.DataBind();

                //dropFunc.Items.Clear();
                //dropFunc.Items.Add(new ListItem("Select", "0"));
                //dropFunc.SelectedValue = Convert.ToInt64(d.Func_Idx).ToString();

            }
        }

        protected void gridDept_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Int32.Parse(gridDept.DataKeys[e.RowIndex].Value.ToString());
            CLMSDataContext context = new CLMSDataContext();
            try
            {
                Dim_Department f = context.Dim_Departments.Single(x => x.Dept_Idx == id);
                context.Dim_Departments.DeleteOnSubmit(f);

                context.SubmitChanges();
                BindDept();
                successTitle.InnerText = "Delete Result";
                pSuccessMessage1.InnerText = f.Dept_Name + " has been successfully deleted.";
                btnSuccessOk.Focus();
                modpopSuccess.Show();
            }
            catch (Exception ex)
            {
                errorTitle.InnerText = "Delete Failed";
                pErrorMessage.InnerText = ex.Message;
                OkButton.Focus();
                modpopError.Show();
            }
            finally
            {
                context.Dispose();
            }
        }

        protected void gridDept_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gridDept.EditIndex = e.NewEditIndex;
            BindDept();
        }

        protected void gridDept_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Int64 id = Int64.Parse(gridDept.DataKeys[e.RowIndex].Value.ToString());

            CLMSDataContext context = new CLMSDataContext();

            try
            {
                Dim_Department dept = context.Dim_Departments.Single(x => x.Dept_Idx == id);
               
                TextBox txtDept_Sort = gridDept.Rows[e.RowIndex].FindControl("txtDept_Sort") as TextBox;
                TextBox txtDept_Code = gridDept.Rows[e.RowIndex].FindControl("txtDept_ShortName") as TextBox;
                TextBox txtDept_Name = gridDept.Rows[e.RowIndex].FindControl("txtDept_Name") as TextBox;
                DropDownList dropFunc = (DropDownList)gridDept.Rows[e.RowIndex].FindControl("dropFunc");
               

                dept.Dept_Sort = Convert.ToInt32(txtDept_Sort.Text);
                dept.Dept_ShortName = txtDept_Code.Text;
                dept.Dept_Name = txtDept_Name.Text;
                dept.Func_PIdx = Convert.ToInt64(dropFunc.SelectedValue.ToString());
           
                context.SubmitChanges();
               
                successTitle.InnerText = "Save Results";
                pSuccessMessage1.InnerText = "Update Completed!";
                btnSuccessOk.Focus();
                modpopSuccess.Show();
            }
            catch (Exception ex)
            {
                errorTitle.InnerText = "Error Encountered";
                pErrorMessage.InnerText = ex.Message;
                OkButton.Focus();
                modpopError.Show();
            }
            finally
            {
                context.Dispose();
                gridFunc.EditIndex = -1;
                BindDept();
            }
        }
}
}



Answers (4)