第2页的数据显示应用了分页的gridview中第1页的数据

时间:2014-12-06 22:05:50

标签: c# gridview pagination

如果在gridview上应用分页,该怎么办?如果我试图显示每一行的数据,在第二页上,它也会显示网格第二页上第一页的数据。怎么办?

我的代码是: - protected void gvbind() { SqlCommand cmd = new SqlCommand(参考JOIN,选择Reference.ReferenceID,Reference.Name,Reference.JobTitle,Reference.Organization,Reference.Phone,Reference.Email,Reference.City,Reference.StateProvince,Reference.Country,Reference.ReferenceType) JResume ON Reference.ResumeID = JResume.ResumeID JOIN JUser ON UserID = JResume.ResumeID AND JUser.Email=@Email“,conn1);         cmd.Parameters.Add(“@ Email”,SqlDbType.NVarChar,50);         cmd.Parameters [“@ Email”]。Value = Session [“Login”]。ToString();         SqlDataAdapter da = new SqlDataAdapter(cmd);

    DataSet ds = new DataSet();

    da.Fill(ds);
    SqlDataReader dr=cmd.ExecuteReader();


    if (ds.Tables[0].Rows.Count > 0)
    {

        grid1.DataSource = ds;

        grid1.DataBind();
        int CountRowsPerPage = grid1.Rows.Count; // as paging is applied so count number of rows per page to display
        foreach (GridViewRow row in grid1.Rows)
        {
            if (dr.Read())
            {
               // int RowIndex = grid1.PageIndex * grid1.PageSize + row.RowIndex;

                Label ReferenceLabel = (Label)grid1.Rows[row.RowIndex].Cells[0].FindControl("ReferenceLabel") as Label;
                if (!(ReferenceLabel == null))
                {
                    ReferenceLabel.Text = "";
                    ReferenceLabel.Text = dr["JobTitle"].ToString() + " in " + dr["Organization"].ToString() + "<br/> " + dr["City"].ToString();

                    ReferenceLabel.Text += " , " + dr["Country"].ToString();
                    ReferenceLabel.Text += " <br/> " + dr["Phone"].ToString() + "<br/>" + dr["Email"].ToString();

                }
            }
        }
        dr.Close();
    } 

}

1 个答案:

答案 0 :(得分:0)

此网站上一直没人回答我的问题。所以经过我自己的全部努力之后,我会第一次回答自己。

protected void grid1_RowDataBound(object sender,GridViewRowEventArgs e)     {         if(e.Row.RowType == DataControlRowType.DataRow)         {             Label ReferenceLabel =(Label)e.Row.Cells [0] .FindControl(“ReferenceLabel”);

        if (ReferenceLabel != null)
        {
            if (ds.Tables[0].Rows.Count>0)
            {
                ReferenceLabel.Text = "";
                ReferenceLabel.Text = ds.Tables[0].Rows[e.Row.DataItemIndex]["JobTitle"].ToString() + " in " + ds.Tables[0].Rows[e.Row.DataItemIndex]["Organization"].ToString() + "<br/> " + ds.Tables[0].Rows[e.Row.DataItemIndex]["City"].ToString();

                ReferenceLabel.Text += " , " + ds.Tables[0].Rows[e.Row.DataItemIndex]["Country"].ToString();
                ReferenceLabel.Text += " <br/> " + ds.Tables[0].Rows[e.Row.DataItemIndex]["Phone"].ToString() + "<br/>" + ds.Tables[0].Rows[e.Row.DataItemIndex]["Email"].ToString();
            }

        }

    }
}