Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I had write the edit and delete in gridview code, but it doesnt work, kindly help to amend.

thank you


XML
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="acmkweb.WM.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>



<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">



<asp:TextBox ID="TextBox1" runat="server"/>
<asp:TextBox ID="TextBox2" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True"  ></asp:GridView>


<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click"/>


 <asp:TableRow ID="TableRow1" runat="server" ForeColor="Teal">
            <asp:TableHeaderCell Font-Bold="true" ForeColor="#0C0C25" ColumnSpan="4">
                <asp:label id="StatLabel" runat="server">
                </asp:label>
                </asp:TableHeaderCell>
              </asp:TableRow>

</asp:Content>



Code Behind


C#
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.Collections;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.IO;


namespace acmkweb.WM
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                FillContainerType();
            }



        }







        private void BindGrid(int rowcount)
        {

            DataTable dt = new DataTable();

            DataRow dr;

            dt.Columns.Add(new System.Data.DataColumn("Test1", typeof(String)));

            dt.Columns.Add(new System.Data.DataColumn("Test2", typeof(String)));

            dt.Columns.Add(new System.Data.DataColumn("Test3", typeof(String)));



            if (ViewState["CurrentData"] != null)
            {

                for (int i = 0; i < rowcount + 1; i++)
                {

                    dt = (DataTable)ViewState["CurrentData"];

                    if (dt.Rows.Count > 0)
                    {

                        dr = dt.NewRow();

                        dr[0] = dt.Rows[0][0].ToString();



                    }

                }

                dr = dt.NewRow();

                dr[0] = TextBox1.Text;

                dr[1] = TextBox2.Text;

                dr[2] = Drop1.SelectedValue;

                dt.Rows.Add(dr);



            }

      else
           {

              dr = dt.NewRow();

               dr[0] = TextBox1.Text;

               dr[1] = TextBox2.Text;

               dr[2] = Drop1.SelectedValue;



               dt.Rows.Add(dr);



           }



            // If ViewState has a data then use the value as the DataSource

            if (ViewState["CurrentData"] != null)
            {

                GridView1.DataSource = (DataTable)ViewState["CurrentData"];

                GridView1.DataBind();

                CommandField cf = new CommandField { AutoGenerateDeleteButton = true, AutoGenerateDeleteButton = true };
                GridView1.Columns.Add(cf);


            }

            else
            {



                // Bind GridView with the initial data assocaited in the DataTable

                GridView1.DataSource = dt;

                GridView1.DataBind();



            }

            // Store the DataTable in ViewState to retain the values

            ViewState["CurrentData"] = dt;



        }




        protected void Button1_Click(object sender, EventArgs e)
        {
            // Check if the ViewState has a data assoiciated within it. If
            if (ViewState["CurrentData"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentData"];
                int count = dt.Rows.Count;
                BindGrid(count);
            }
            else
            {
                BindGrid(1);
            }
            TextBox1.Text = string.Empty;
            TextBox2.Text = string.Empty;

            TextBox1.Focus();
            TextBox2.Focus();

        }



     protected void Button2_Click(object sender, EventArgs e)
        {


       foreach (GridViewRow oItem in GridView1.Rows)
         {
          string str1 = oItem.Cells[0].Text;
          string str2 = oItem.Cells[1].Text;
          string str3 = oItem.Cells[2].Text;
          insertData(str1, str2, str3);
    }

    Response.Redirect("WebForm2.aspx?stat=insert");
}


     public void insertData(string str1,string str2,string str3)

     {
                SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
                 string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')";



                con.Open();

                SqlCommand cmd = new SqlCommand(sql, con);


                cmd.ExecuteNonQuery();

                con.Close();


            }

        }

    }
Posted
Updated 23-Jun-13 16:28pm
v2
Comments
Prasad_Kulkarni 24-Jun-13 3:29am    
"does't work" not gonna help us much to understand the problem. If you can debug your code the get the rid of what exactly the problem is withe line number. So we can understand it and try to let you move on.
caulsonchua 24-Jun-13 3:37am    
HI Prasad_Kulkarni, im searching from Google get the all example code is link with DB, but based on my coding is save the data in view state first, then only update into DB, the problem i facing now is how to delete the data in view state before update into DB?

any example ?

 
Share this answer
 
Dear all,

im searching from Google get the all example code is link with DB, but based on my coding is save the data in view state, May i know how to delete the data in view state ?

any example ?
 
Share this answer
 

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



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