[Solved] How to edit drop down list in Gridview in asp.net c#

In this tutorial, I am going to show with you "how to edit drop-down list in gridview in asp.net". Some times whenever we develop an application during development these type of requirement needed. So, in this tutorial, I am trying to give you the best solution to solve this problem "edit drop-down list in gridview in asp.net". We will also discuss about how to Bind dropdownlist in gridview EditTemplate.


how to bind dropdownlist in gridview edititemtemplate



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo.aspx.cs" Inherits="Demo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="grdview" runat="server"
                OnRowEditing="grdview_RowEditing" DataKeyNames="eid" ShowFooter="true" OnRowUpdating="grdview_RowUpdating"
                OnRowDeleting="grdview_RowDeleting" OnRowCancelingEdit="grdview_RowCancelingEdit"
                AutoGenerateColumns="false">                 
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>Status</HeaderTemplate>
                        <ItemTemplate>
                            <%# Eval("IsActive").ToString()=="1"? "Active":"No Active" %>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:DropDownList ID="ddlactive" runat="server" CssClass="DropDown"
                                AutoPostBack="True">
                                <asp:ListItem Value="1">Active</asp:ListItem>
                                <asp:ListItem Value="0">No active</asp:ListItem>
                            </asp:DropDownList>
                        </FooterTemplate>
                        <EditItemTemplate>
                            
                            <asp:DropDownList ID="ddlAttnd" runat="server" CssClass="DropDown"
                                AutoPostBack="True">
                                <asp:ListItem Value="1">Active</asp:ListItem>
                                <asp:ListItem Value="0">No active</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>operation</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Button runat="server" ID="btnedit" Text="edit" CommandName="edit" />
                            <asp:Button runat="server" ID="btndelete" Text="delete" CommandName="delete" />
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Button ID="btninsert" runat="server" Text="insert" OnClick="btninsert_Click" />
                        </FooterTemplate>
                        <EditItemTemplate>
                            <asp:Button runat="server" ID="btnupdate" Text="update" CommandName="update" />
                            <asp:Button runat="server" ID="btncancel" Text="cancel" CommandName="cancel" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>No Record Available Insert Record</EmptyDataTemplate>
            </asp:GridView>
        </div>
    </form>
</body>
</html>


 CS Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Demo : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString);
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    public void BindData()
    {
        con.Open();
        cmd = new SqlCommand("getday1", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grdview.DataSource = ds;
        grdview.DataBind();
        con.Close();
    }
    public void EditData()
    {
        con.Open();
        cmd = new SqlCommand("getday1", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grdview.DataSource = ds;
        grdview.DataBind();
        con.Close();
    }
    protected void grdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from emp where eid='" + Convert.ToInt32(grdview.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {

            Response.Write("<script> alert('Deleted'); </script>");
        }
        con.Close();
        BindData();
    }
    protected void btninsert_Click(object sender, EventArgs e)
    {
        int p;
        DropDownList txt_isactive = grdview.FooterRow.FindControl("ddlactive") as DropDownList;

        if (txt_isactive.SelectedValue == "1")
        {
            p = 1;
        }
        else
        {
            p = 0;
        }
        con.Open();
        SqlCommand cmd = new SqlCommand("day1emp", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("IsActive", p);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Response.Write("<script> alert('Inserted'); </script>");
        }
        con.Close();
        BindData();

    }
    protected void grdview_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdview.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void grdview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdview.EditIndex = -1;
        BindData();
    }

    protected void grdview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        DropDownList txt_fisactive = grdview.Rows[e.RowIndex].FindControl("ddlAttnd") as DropDownList;
        int isactive;

        if (txt_fisactive.SelectedValue == "1")
        {
            isactive = 1;
        }
        else
        {
            isactive = 0;
        }

        con.Open();
        SqlCommand cmd = new SqlCommand("day1emp_update", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@IsActive", isactive);
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {

            Response.Write("<script> alert('Updated'); </script>");
        }
        con.Close();
        grdview.EditIndex = -1;
        BindData();
    }

}

Post a Comment

Post a Comment (0)

Previous Post Next Post