Hi Friends,
I'm having some issue of passing data from one GridView to another Gridview. Can anyone post some sample links please. Simply i want to pass some selected data from one gridview to another gridview which is in same page. I'm using asp.net 4.0 ,C#. Any code segments are also welcome.
I tried datatable, but it only pick one record always. see the code below
DataTable dt = new DataTable();
void createDT()
{
dt.Columns.Add("Product Code", typeof(string));
dt.Columns.Add("Product Name", typeof(string));
dt.Columns.Add("Qty", typeof(string));
dt.Columns.Add("Unit Price", typeof(string));
isTableCraeted = true;
}
bool isTableCraeted= false;
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (isTableCraeted == false)
createDT();
string a = GridView1.Rows[0].Cells[1].Text.ToString();
string b = GridView1.Rows[0].Cells[2].Text.ToString();
string c = GridView1.Rows[0].Cells[3].Text.ToString();
string d = GridView1.Rows[0].Cells[4].Text.ToString();
dt.Rows.Add(a, b, c, d);
GridView2.DataSource = dt;
GridView2.DataBind();
}
Thanks
Max
Loading
Jignesh TrivediPosted May 31, 2012, 11:41 PM
I have try following code it working for me.
int index = Convert.ToInt32(e.CommandArgument);
dt = Session["test"] as DataTable;
if (dt == null)
{
dt = new DataTable();
createDT();
}
string a = GridView1.Rows[index].Cells[1].Text.ToString();
string b = GridView1.Rows[index].Cells[2].Text.ToString();
string c = GridView1.Rows[index].Cells[3].Text.ToString();
string d = GridView1.Rows[index].Cells[4].Text.ToString();
dt.Rows.Add(a, b, c, d);
Session["test"] = dt;
GridView2.DataSource = dt;
GridView2.DataBind();
hope this will help u.
Charith LiyanagamagePosted May 31, 2012, 8:49 AM
Jignesh TrivediPosted May 31, 2012, 8:09 AM
no, session is able to store dataset with in it, you can store dataset in session.
like..
dt = Session["storedDataTable"] as DataTable;
if(dt ==null)
{
if (isTableCraeted == false)
createDT();
}
string a = GridView1.Rows[0].Cells[1].Text.ToString();
string b = GridView1.Rows[0].Cells[2].Text.ToString();
string c = GridView1.Rows[0].Cells[3].Text.ToString();
string d = GridView1.Rows[0].Cells[4].Text.ToString();
dt.Rows.Add(a, b, c, d);
Session["storedDataTable"] = dt;
GridView2.DataSource = dt;
GridView2.DataBind();
try this..
Charith LiyanagamagePosted May 31, 2012, 7:49 AM
Jignesh TrivediPosted May 31, 2012, 12:28 AM
this is due to, you are not Preserve datasource of gridview2.
so preserve dt in session and when add new row @ this time first retreive from session and add new row put in session.
now assign as a data source.
hope you understand.