AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 data not transfering from one page gridview to another page gridview
Posted by charan on  4/19/2010 4:59:51 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
iam taking data from one page gridview to another page in session but it is not displaying in another page gridview
please check my code and give suggestion which helps me



string constr = "Data Source=MAINSERVER;Initial Catalog=Inventory;User ID=sa;Password=nsg_ss_0103";
protected void Page_Load(object sender, EventArgs e)
{


if (Page.IsPostBack == false)
{
GridView1.PageIndex = 0;

bindata();

}






}
private void bindata()
{

SqlConnection con12 = new SqlConnection(constr);
SqlDataAdapter da12 = new SqlDataAdapter("SELECT [CategoryNameE], [ItemKey], [ItemKeyNameE], [CurrentQTY], [SalesPrice] FROM [CurrentInWH]", con12);
DataSet ds = new DataSet();
da12.Fill(ds, "t");
GridView1.DataSource = ds.Tables["t"];
GridView1.DataBind();
}

private void GetGridViewData()
{
DataTable dt;
if(Session["CheckedRecords"]!=null)
//if (ViewState["CheckedRecords"] != null)
dt = (DataTable)Session["CheckedRecords"];
else dt = CreateNewTable();

for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chk");
if (chk.Checked)
{
dt = AddNewRow(GridView1.Rows[i], dt);
}
else {
dt = RemoveRow(GridView1.Rows[i], dt);
}

}
Session["CheckedRecords"] = dt;

}

private DataTable CreateNewTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("CategoryNameE");
dt.Columns.Add("ItemKey");
dt.Columns.Add("ItemKeyNameE");
dt.Columns.Add("CurrentQTY");
dt.Columns.Add("SalesPrice");
dt.Columns.Add("Quantity");
dt.Columns.Add("Total");

dt.AcceptChanges();
return dt;
}

private DataTable AddNewRow(GridViewRow gvrow, DataTable dt)
{
DataRow[] dr = dt.Select("ItemKey = '" + gvrow.Cells[2].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["CategoryNameE"] = gvrow.Cells[1].Text;
dt.Rows[dt.Rows.Count - 1]["ItemKey"] = gvrow.Cells[2].Text;
dt.Rows[dt.Rows.Count - 1]["ItemKeyNameE"] = gvrow.Cells[3].Text;
dt.Rows[dt.Rows.Count - 1]["CurrentQTY"] = gvrow.Cells[4].Text;
dt.Rows[dt.Rows.Count - 1]["SalesPrice"] = gvrow.Cells[5].Text;
dt.Rows[dt.Rows.Count - 1]["Quantity"] = gvrow.Cells[6].Text;
dt.Rows[dt.Rows.Count - 1]["Total"] = gvrow.Cells[7].Text;



dt.AcceptChanges();
}
return dt;
}

private DataTable RemoveRow(GridViewRow gvrow, DataTable dt)
{
DataRow[] dr = dt.Select("ItemKey= '" + gvrow.Cells[2].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}

protected void chk_CheckedChanged(object sender, EventArgs e)
{
GetGridViewData();



TextBox txtqty1; TextBox txttott; CheckBox ch = null;
for (int i = 0; i < GridView1.Rows.Count; i++)
{

txtqty1 = (TextBox)GridView1.Rows[i].FindControl("txtqty");
txttott = (TextBox)GridView1.Rows[i].FindControl("txttot");
ch = (CheckBox)GridView1.Rows[i].FindControl("chk");

if (ch.Checked)
{
txtqty1.Visible = true;
txttott.Visible = true;
}
else {

txtqty1.Visible = false;
txttott.Visible = false;

}




}

}
// in second page
DataTable dt = (DataTable)Session["CheckedRecords"];
GridView1.DataSource = dt;
GridView1.DataBind();


Post Your Reply Post New Question

 Viewer's Reply
Check
Answered By Moderator1 on 4/23/2010 10:37:52 AM
Hi,

Please check whether data exists in Session["CheckedRecords"] or not.. because I could find some problem in the loop you used to create this session.
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
No Nonsense XML Web Development With PHP - Free 146 Page Preview!
The Beginner's Guide to Joomla
Take Advantage of Oracle's 2 Day DBA Course