charan sekhar

charan sekhar

  • NA
  • 108
  • 0

transfering data from one gridview to another page gridview

Apr 14 2010 6:25 AM

hi iam using asp.net with c#, my grid contains 1st column is check box ,and paging is true ,iam displaying data from view that does not contain primary key, so based on this when user selects the no of checkboxes in 1st page and
 in another page some records then i want to transfer to another page gridview but it is not displaying. can you correct my code where iam going wrong
 

string constr = "Data Source=MAINSERVER;Initial Catalog=Inventory;User ID=sa;Password=nsg";
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 (ViewState["CheckedRecords"] != null)
dt = (DataTable)ViewState["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);
}
}
ViewState["CheckedRecords"] = dt;
}
//@CategoryNameE,@ItemKey,@ItemKeyNameE,@CurrentQTY,@SalesPrice,@Quantity,@Total
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("CustomerID = '" + gvRow.Cells[1].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("CategoryNameE= '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GetGridViewData();
GridView1.PageIndex = e.NewPageIndex;
bindata();
foreach (GridViewRow row in GridView1.Rows)
{

CheckBox c1 = (CheckBox)row.FindControl("chk");
TextBox qt = (TextBox)row.FindControl("txtqty");
TextBox tot1 = (TextBox)row.FindControl("txttot");
Label categoryname = (Label)row.FindControl("Label5");
Label ItemKey = (Label)row.FindControl("Label2");
Label ItemKeyNameE = (Label)row.FindControl("Label1");
Label CurrentQTY = (Label)row.FindControl("Label3");
Label SalesPrice = (Label)row.FindControl("Label4");
 

if (c1.Checked == true)
{
clsdataset.insert(categoryname.Text, ItemKey.Text, ItemKeyNameE.Text, CurrentQTY.Text, SalesPrice.Text, qt.Text, tot1.Text);
 

}
 

}
}
 
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
GetGridViewData();
 
}
 
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;
}
 

}
this is insertbutton code
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{

CheckBox c1 = (CheckBox)row.FindControl("chk");
TextBox qt = (TextBox)row.FindControl("txtqty");
TextBox tot1 = (TextBox)row.FindControl("txttot");
Label categoryname = (Label)row.FindControl("Label5");
Label ItemKey = (Label)row.FindControl("Label2");
Label ItemKeyNameE = (Label)row.FindControl("Label1");
Label CurrentQTY = (Label)row.FindControl("Label3");
Label SalesPrice = (Label)row.FindControl("Label4");
if (c1.Checked == true)
{
clsdataset.insert(categoryname.Text, ItemKey.Text, ItemKeyNameE.Text, CurrentQTY.Text, SalesPrice.Text, qt.Text, tot1.Text);
 

}

}

iam performing this operation inbutton1 for saving all records but it is not storing previous records