insert update delete in gridview in asp.net
  • Hi today i am sharing how can we perform add update delete in asp.net gridview using C# code.
    Source code is Also Attached with data script.

    image

    Below is code for several actions on gridview



    protected void AddNewProduct(object sender, EventArgs e)
    {
    string ProductID = ((TextBox)GridView1.FooterRow.FindControl("txtProductID")).Text;
    string Name = ((TextBox)GridView1.FooterRow.FindControl("txtProductName")).Text;
    string Company = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "insert into Products(ProductID, ProductName, CompanyName) " +
    "values(@ProductID, @ProductName, @CompanyName);" +
    "select ProductID,ProductName,CompanyName from Products";
    cmd.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = ProductID;
    cmd.Parameters.Add("@ProductName", SqlDbType.VarChar).Value = Name;
    cmd.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = Company;
    GridView1.DataSource = GetData(cmd);
    GridView1.DataBind();
    }

    protected void DeleteProduct(object sender, EventArgs e)
    {
    LinkButton lnkRemove = (LinkButton)sender;
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "delete from Products where " +
    "ProductID=@ProductID;" +
    "select ProductID,ProductName,CompanyName from Products";
    cmd.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = lnkRemove.CommandArgument;
    GridView1.DataSource = GetData(cmd);
    GridView1.DataBind();
    }
    protected void EditProduct(object sender, GridViewEditEventArgs e)
    {
    GridView1.EditIndex = e.NewEditIndex;
    BindData();
    }
    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
    GridView1.EditIndex = -1;
    BindData();
    }
    protected void UpdateProduct(object sender, GridViewUpdateEventArgs e)
    {
    string ProductID = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblProductID")).Text;
    string Name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtProductName")).Text;
    string Company = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCompany")).Text;
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "update Products set ProductName=@ProductName,CompanyName=@CompanyName " +
    "where ProductID=@ProductID;" +
    "select ProductID,ProductName,CompanyName from Products";
    cmd.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = ProductID;
    cmd.Parameters.Add("@ProductName", SqlDbType.VarChar).Value = Name;
    cmd.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = Company;
    GridView1.EditIndex = -1;
    GridView1.DataSource = GetData(cmd);
    GridView1.DataBind();
    }


    and below is ASPX page body tag for idea



    <body style ="margin:0;padding:0">
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
        <div id = "dvGrid" style ="padding:10px;width:550px">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
           <asp:GridView ID="GridView1" runat="server"  Width = "550px"
            AutoGenerateColumns = "False" Font-Names = "Arial" 
            Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"  
            HeaderStyle-BackColor = "green" AllowPaging ="True"  ShowFooter = "True"  
            OnPageIndexChanging = "OnPaging" onrowediting="EditProduct"
            onrowupdating="UpdateProduct"  onrowcancelingedit="CancelEdit" BackColor="White" 
                    BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
                    ForeColor="Black" GridLines="Vertical" >
           <Columns>
            <asp:TemplateField ItemStyle-Width = "30px"  HeaderText = "ProductID">
                <ItemTemplate>
                    <asp:Label ID="lblProductID" runat="server" Text='<%# Eval("ProductID")%>'></asp:Label>
                </ItemTemplate> 
                <FooterTemplate>
                    <asp:TextBox ID="txtProductID" Width = "40px" MaxLength = "5" runat="server"></asp:TextBox>
                </FooterTemplate> 
                <ItemStyle Width="30px" />
            </asp:TemplateField> 
            <asp:TemplateField ItemStyle-Width = "100px"  HeaderText = "Name">
                <ItemTemplate>
                    <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName")%>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtProductName" runat="server" Text='<%# Eval("ProductName")%>'></asp:TextBox>
                </EditItemTemplate>  
                <FooterTemplate>
                    <asp:TextBox ID="txtProductName" runat="server"></asp:TextBox>
                </FooterTemplate> 
                <ItemStyle Width="100px" />
            </asp:TemplateField>
            <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Company">
                <ItemTemplate>
                    <asp:Label ID="lblCompany" runat="server" Text='<%# Eval("CompanyName")%>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtCompany" runat="server" Text='<%# Eval("CompanyName")%>'></asp:TextBox>
                </EditItemTemplate>  
                <FooterTemplate>
                    <asp:TextBox ID="txtCompany" runat="server"></asp:TextBox>
                </FooterTemplate> 
                <ItemStyle Width="150px" />
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument = '<%# Eval("ProductID")%>' 
                     OnClientClick = "return confirm('Do you want to delete?')"
                    Text = "Delete" OnClick = "DeleteProduct"></asp:LinkButton>
                </ItemTemplate>
                 <FooterTemplate>
                    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick = "AddNewProduct" />
                </FooterTemplate> 
            </asp:TemplateField> 
            <asp:CommandField  ShowEditButton="True" /> 
           </Columns> 
           <AlternatingRowStyle BackColor="#CCCCCC"  />
               <FooterStyle BackColor="#CCCCCC" />
               <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
               <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
               <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
               <SortedAscendingCellStyle BackColor="#F1F1F1" />
               <SortedAscendingHeaderStyle BackColor="#808080" />
               <SortedDescendingCellStyle BackColor="#CAC9C9" />
               <SortedDescendingHeaderStyle BackColor="#383838" />
        </asp:GridView> 
        </ContentTemplate> 
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID = "GridView1" /> 
        </Triggers> 
        </asp:UpdatePanel> 
        </div>
        </form>
    </body>
    AddUpdateDeleteGridViewAsp.net.jpg
    553 x 223 - 45K
    InsertUpdateDeleteGridvIew.zip
    35K

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

Optimum Creative