Click here to Skip to main content
15,905,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My Code...

Static datatable data;
protected void Page_Load(object sender, EventArgs e)
   {
       
       if (!IsPostBack)
       {
           data = new DataTable();
           CalculatePURID();
           loadgrid();
       }

   }

public DataTable Fill()
    {
        DataTable dt = new DataTable("items");
        dt.Columns.Add("sno", typeof(int));
        dt.Columns.Add("Prod_id", typeof(string));
        dt.Columns.Add("Prod_name", typeof(string));
        dt.Columns.Add("Prod_size", typeof(string));
        dt.Columns.Add("Prod_qty", typeof(string));
        dt.Columns.Add("Prod_Per_amt", typeof(string));
        dt.Columns.Add("Prod_amt", typeof(string));

        foreach (GridViewRow grdRow in Grid_Purchase.Rows)        
        {
            int i = grdRow.RowIndex;
            DataRow dr = dt.NewRow();
            System.Web.UI.WebControls.Label txt_sno = (System.Web.UI.WebControls.Label)(Grid_Purchase.Rows[i].FindControl("lblsno"));
            System.Web.UI.WebControls.TextBox txt_Prodid = (System.Web.UI.WebControls.TextBox)(Grid_Purchase.Rows[i].FindControl("txt_grd_Pid"));
            System.Web.UI.WebControls.DropDownList txt_Prodname = (System.Web.UI.WebControls.DropDownList)(Grid_Purchase.Rows[i].FindControl("txt_grd_ProdName"));
            System.Web.UI.WebControls.TextBox txt_Prodsize = (System.Web.UI.WebControls.TextBox)(Grid_Purchase.Rows[i].FindControl("txt_grd_size"));
            System.Web.UI.WebControls.TextBox txt_Prodqty = (System.Web.UI.WebControls.TextBox)(Grid_Purchase.Rows[i].FindControl("txt_grd_qty"));
            System.Web.UI.WebControls.TextBox txt_Prodperamt = (System.Web.UI.WebControls.TextBox)(Grid_Purchase.Rows[i].FindControl("txt_grd_piece_amt"));
            System.Web.UI.WebControls.TextBox txt_Prodamt = (System.Web.UI.WebControls.TextBox)(Grid_Purchase.Rows[i].FindControl("txt_grd_amt"));

            dr[0] = txt_sno.Text.ToString();
            dr[1] = txt_Prodid.Text.ToString();
            dr[2] = txt_Prodname.Text.ToString();
            dr[3] = txt_Prodsize.Text.ToString();
            dr[4] = txt_Prodqty.Text.ToString();
            dr[5] = txt_Prodperamt.Text.ToString();
            dr[6] = txt_Prodamt.Text.ToString();
            dt.Rows.Add(dr);
        }
       
        return dt;
    }


public DataTable FillNewrow()
    {
        
        int a = totalnumberofrowsin_Grid;
        data = Fill();      //To get previous values       
        
        DataRow dr1 = data.NewRow();
        dr1[0] = a+1;
        dr1[1] = "";
        dr1[2] = "";
        dr1[3] = "";
        dr1[4] = "";
        dr1[5] = "";
        dr1[6] = "";
        data.Rows.InsertAt(dr1, a + 1);        
        
        //ds.Tables.Add(data);
        
        totalnumberofrowsin_Grid++;
        return data;
    }
    


protected void Grid_Purchase_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       Grid_Purchase.PageIndex = e.NewPageIndex;
       Grid_Purchase.DataSource = data;   <b>// Even i used static datatable containing datas(I saw the data using breakpoint).</b>
       Grid_Purchase.DataBind();
   }


protected void Lnk_insert_Click(object sender, EventArgs e)   //link button inside footer of grid
    {
        DataTable dt = new DataTable();
        dt = FillNewrow();
        
        Grid_Purchase.DataSource = dt;
        Grid_Purchase.DataBind();
        DataTable myDatatable1 = new DataTable();
        myDatatable1 = SQLobj.SQL_Adapter("select Prod_name from ProductDetails");
        DropDownList drdList;
        DataRow dr2;
        dr2 = myDatatable1.NewRow();
        dr2["Prod_name"] = "-Select-";
        myDatatable1.Rows.InsertAt(dr2, 0);
        foreach (GridViewRow grdRow in Grid_Purchase.Rows)
        {
            drdList = (DropDownList)(Grid_Purchase.Rows[grdRow.RowIndex].Cells[1].FindControl("txt_grd_ProdName"));
            drdList.DataSource = myDatatable1;
            drdList.DataTextField = "Prod_name";
            drdList.DataBind();
        }
    }


protected void Grid_Purchase_RowCommand(object sender, GridViewCommandEventArgs e) //i just fired it for paging
    {

    }
Posted
Updated 11-Jul-11 2:09am
v6

It's not quite that simple!
There is a tip which shows you how to do it here: Showing a page of information at a time in a GridView[^]
 
Share this answer
 
Comments
[no name] 7-Jul-11 6:33am    
Nice Link. My 5.
It s not possible . If you have to do this first retrive data and put it on view state before bind the gridview.
and loop through the view state data.
because gridview datasource contain only the page require data. when you click next one post back occoure and it select next records.
so try to do this with using view state data.
 
Share this answer
 
Comments
J.Karthick 7-Jul-11 8:08am    
Even i remove paging, i got my datasource cleared. When i add an new row inside grid(Page refreshed).Do you have any idea?
ParthaDinda 7-Jul-11 8:18am    
can you post your code? your problem is not clear to me.
J.Karthick 7-Jul-11 9:12am    
I have posted my code...PLease check it out
J.Karthick 7-Jul-11 9:16am    
My problem is After adding the row, grid lost its previous data which it contains
J.Karthick 7-Jul-11 9:17am    
Before you started....Just check this link

http://connect.microsoft.com/VisualStudio/feedback/details/104994/templatefield-in-a-gridview-doesnt-have-its-viewstate-restored-when-boundfields-are-inserted
Got Solution for this problem,


I just maintain an STATIC datatable to update each entry of my grid into this.


Later i retrieve those grid values from that STATIC datatable. Its works fine.

Thanks for your Support guys !!
 
Share this answer
 
To achieve you have to disable paging before loop and rebind the grid and perform loop. Then enable paging and bind the grid again.
C#
Grid_Purchase.AllowPaging = false;
Grid_Purchase.Databind(); 
foreach (GridViewRow grdRow in Grid_Purchase.Rows)
{
 //code here
}
Grid_Purchase.AllowPaging = true;
Grid_Purchase.Databind(); 
 
Share this answer
 
v2
Comments
J.Karthick 7-Jul-11 7:21am    
ya....i tried it....but my gridview source becomes empty after i doing this... :(

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