Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

I have data in page1 in gridview,now i want to transfer grid data to another page and also how to parse this grid data so that i can fill those data to lables and textboxes.

Can any one assist me?

Thanks,
Posted
Comments
preet88 18-May-12 1:29am    
hi afzal you can do this in other way also... you can have a class and store the data of the grid accordingly in the data members.
Once you have the data in class object you can pass that object to other page by get methods(if there is direct navigation from 1page to other) or you can also pass this using session variables

put grid inside session in your first page
C#
 protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnStringDb1"].ConnectionString;
            SqlCommand cmd = new SqlCommand("select EVENT_CODE from EVENT_MASTER", con);
            DataTable dt = new DataTable();
            SqlDataAdapter ad = new SqlDataAdapter();
            ad.SelectCommand = cmd;
            ad.Fill(dt);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
            Session["grd"] = GridView1;
            ad.Dispose();
            cmd.Dispose();
            con.Dispose();
}

in second page acess this grid and bind to individual control like

C#
protected void Page_Load(object sender, EventArgs e)
       {

           GridView dr = new GridView();

           if(dr!=null)
               dr = (GridView)Session["grd"];
            Label1.Text = dr.Rows[0].Cells[0].ToString();

       }
 
Share this answer
 
v2
Comments
Afzal Shaikh 17-May-12 8:13am    
I tried this one , it is giving me below error in another page

Unable to cast object of type 'System.Web.UI.WebControls.GridViewRow' to type 'System.Web.UI.WebControls.GridView'.

if u know please ?
Nilesh Rokade 17-May-12 8:28am    
can u post code you are trying.since after running i have updated above code.May be there is something typographical error is occuring
Afzal Shaikh 17-May-12 8:41am    
Page1 code:

protected void GrdvwSuccessful_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Btn"))
{
GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
//Afzal
object obj = objAgentHandler.GetALLordersSuccssful(Convert.ToInt32(StateHelper.currentLogin);
Session["grd"] = GrdvwSuccessful;

Response.Redirect("InfoPage_Remarks.aspx?ID=" + RowIndex);

}

}

Page2 Code:


public void GetVisaInfo_Remarks()
{
k = Convert.ToInt32(Request.QueryString["ID"]);
List<visaordertinfo> info = (List<visaordertinfo>)new VisaOrder_Handler().GetUserInfo_Neworder(k);

if (info.Count >= 0)
{
//GridViewRow dr = new GridViewRow();
GridView dr = new GridView();
dr = (GridView)Session["grd"];
LbName.Text = dr.Rows[0].Cells[0].ToString();

}

}
VB
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
       If e.CommandName = "select" Then
 

 
           GridView1.FooterRow.Visible = True
 
           '' for navigate other page or....
            Dim i As Integer
          i = e.CommandArgument()
             Dim row As GridViewRow
           row = GridView1.Rows(i)
 
           Session("name") = row.Cells(1).Text
           Session("mob") = row.Cells(2).Text
           Session("add") = row.Cells(3).Text
           Response.Redirect("Default4.aspx")
 

 

       End If

'' In default4.aspx
use above sessions in default4. page and store this values in datatble and assign datatable to gridview present in default4.aspx
VB
Dim dt As DataTable = New DataTable()
            
dt.Columns.Add("Name")
            dt.Columns.Add("MobileNumber")
            dt.Columns.Add("Address")
            Dim ro As DataRow
            ro = dt.NewRow()
            ro(0) =   Session("name").tostring()
            ro(1) =  Session("mob").tostring()
            ro(2) =  Session("add").tostring()
            dt.Rows.Add(ro)
 GridView1.DataSource = dt
            GridView1.DataBind()
 
Share this answer
 
v3
are you casting System.Web.UI.WebControls.GridViewRow' to grid ???? copy paste code given in above comments
 
Share this answer
 
do not put grid in session at following function .


C#
protected void GrdvwSuccessful_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("Btn")) { GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer); int RowIndex = gvr.RowIndex; //Afzal object obj = objAgentHandler.GetALLordersSuccssful(Convert.ToInt32(StateHelper.currentLogin); Session["grd"] = GrdvwSuccessful; Response.Redirect("InfoPage_Remarks.aspx?ID=" + RowIndex); } } 


You will have to put grid in session at place where you are retrieving data.
 
Share this answer
 
v2
Comments
Afzal Shaikh 20-May-12 2:30am    
Thansk , it works!!!!!!!
there are multiple way,
1)you van use session,
put your griddata into session, and access session data on another page,

2)You can also use HTTPcookies,
you can put you griddata into cookie,
C#
Response.Cookies["myNewCookie"].Values["SubCookie1"] = "value1";

string myResponse = Request.Cookies["myNewCookie"].Values["SubCookie1"];



hope this will help you....
 
Share this answer
 
v2
put this code in function where you are retrieving data from databse.

You will have to move your code
C#
Session["grd"] = GrdvwSuccessful;  from 

protected void GrdvwSuccessful_RowCommand(object sender, GridViewCommandEventArgs e)
 
Share this answer
 
v2
int pageSize = 0; //每页显示行数
int nMax = 0; //总记录数
int pageCount = 0; //页数=总记录数/每页显示行数
int pageCurrent = 0; //当前页号
int nCurrent = 0; //当前记录行
DataSet ds = new DataSet();
DataTable dtInfo = new DataTable();

string strConn = "SERVER=127.0.0.1;DATABASE=NORTHWIND;UID=SA;PWD=ULTRATEL"; //数据库连接字符串
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
string strSql = "SELECT * FROM CUSTOMERS";
SqlDataAdapter sda = new SqlDataAdapter(strSql,conn);
sda.Fill(ds,"ds");
conn.Close();
dtInfo = ds.Tables[0];
InitDataSet();

C#
private void InitDataSet()
        {
            pageSize = 20;      //设置页面行数
            nMax = dtInfo.Rows.Count;

            pageCount=(nMax/pageSize);    //计算出总页数

            if ((nMax % pageSize) > 0) pageCount++;

            pageCurrent = 1;    //当前页数从1开始
            nCurrent = 0;       //当前记录数从0开始

            LoadData();
        }


C#
private void LoadData()
        {
            int nStartPos = 0;   //当前页面开始记录行
            int nEndPos = 0;     //当前页面结束记录行

            DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架

            if (pageCurrent == pageCount)
                nEndPos = nMax;
            else
                nEndPos = pageSize * pageCurrent;

            nStartPos = nCurrent;

            lblPageCount.Text = pageCount.ToString();
            txtCurrentPage.Text = Convert.ToString(pageCurrent);

            //从元数据源复制记录行
            for (int i = nStartPos; i < nEndPos; i++)
            {
                dtTemp.ImportRow(dtInfo.Rows[i]);
                nCurrent++;
            }
            bdsInfo.DataSource = dtTemp;
            bdnInfo.BindingSource = bdsInfo;
            dgvInfo.DataSource = bdsInfo;
        }
        4、菜单响应事件:


C#
private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "关闭")
            {
                this.Close();
            }
            if (e.ClickedItem.Text == "上一页")
            {
                pageCurrent--;
                if (pageCurrent <= 0)
                {
                    MessageBox.Show("已经是第一页,请点击"下一页"查看!");
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }

                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                pageCurrent++;
                if (pageCurrent > pageCount)
                {
                    MessageBox.Show("已经是最后一页,请点击"上一页"查看!");
                    return;
                }
                else
                {
                   nCurrent=pageSize*(pageCurrent-1);
                }
                LoadData();
            }
        }
 
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