Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I have a gridview with edit button and paging enabled , after clicking on edit all the controlls in specific row of gridview becomes editable .

now my problem is if user clicks on edit and changes data in sme controlls on grid and go to next page , changes data thr and again come back to first page , the data is not maintained on first page or on 2nd page .

can anyone pls tell me how to maintain the edited data using viewstate while paging .
Heres the code :
Gridview.aspx:
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
     <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        Height="367px"   EnableViewState="true"
        onrowcancelingedit="GridView1_RowCancelingEdit"
        onrowdeleting="GridView1_RowDeleting"
        onrowediting="GridView1_RowEditing"
        onrowupdating="GridView1_RowUpdating"
        Width="535px" AllowPaging="True" AllowSorting="True"
        onpageindexchanging="GridView1_PageIndexChanging" PageSize="4"
        onsorting="GridView1_Sorting" onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:CommandField ButtonType="Button" ShowDeleteButton="True"
                ShowEditButton="True" ShowSelectButton="True" />
                 <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="SalCode" HeaderText="SalCode" />

                </Columns>
                </asp:GridView>

                 <asp:Label ID="lbl_Error" runat="server"></asp:Label>
                </form>
</body>
</html>

and gridview.aspx.cs:
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.SqlClient;
using System.Data;


public partial class _2ndGridView : System.Web.UI.Page
{
    SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet Ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        cn = new SqlConnection("Data Source=INDPI2TC12;initial catalog=CEP;Persist Security Info=True;User ID=cepuser;Password=Atos123");
        da = new SqlDataAdapter("Select * from GridTrial", cn);
        Ds = new DataSet();
        if (!IsPostBack)
        {
            Ds = GetDs();
            BindGrid(Ds);
        }
    }

    private DataSet GetDs()
    {
        
        da.Fill(Ds, "grid");
        return Ds;
    }

    private void BindGrid(DataSet ds)
    {
        GridView1.DataSource = ds.Tables["grid"];
        GridView1.DataBind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Ds = GetDs();
        BindGrid(Ds);
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id, SalCode;
        string Name;

        //retrieve the data from gridview
        id= Convert.ToInt32((GridView1.Rows[e.RowIndex].Cells[1].Text));
       Name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
        SalCode = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text);
       
        try
        {
            cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandText = "update GridTrial set Name=@NAME,SalCode=@SalCode where id=@ID";
            cmd.Parameters.Add("@ID", id);
            cmd.Parameters.Add("@Name", Name);
            cmd.Parameters.Add("@SalCode", SalCode);
            cn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            lbl_Error.Text = ex.Message;
        }
        finally
        {
            cn.Close();
        }
        GridView1.EditIndex = -1;
        Ds = GetDs();
        BindGrid(Ds);
    }


    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Ds = GetDs();
        BindGrid(Ds);
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id;
       id = Convert.ToInt32((GridView1.Rows[e.RowIndex].Cells[1].Text));
        try
        {
            cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandText = "delete from GridTrial where id=@ID";
            cmd.Parameters.Add("@ID", id);
            cn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            lbl_Error.Text = ex.Message;
        }
        finally
        {
            cn.Close();
        }
        GridView1.EditIndex = -1;
        Ds = GetDs();
        BindGrid(Ds);
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Ds = GetDs();
        BindGrid(Ds);
    }


    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        lbl_Error.Text = GridView1.SortExpression.ToString();
        //        = SortDirection.Ascending;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
    }
}
Posted
Updated 5-Sep-12 2:29am
v2
Comments
Christian Amado 5-Sep-12 8:35am    
If you go to another page, you can't get the data directly. You have some work to do. Search on google! :)

 
Share this answer
 
 
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