Reputation: 11
What to do if paging is applied on gridview.? If I try to show data of each row, on 2nd page , it shows me data of 1st page on the 2nd page of grid as well. What to do?
my code is:- protected void gvbind() { SqlCommand cmd = new SqlCommand("Select Reference.ReferenceID,Reference.Name,Reference.JobTitle,Reference.Organization,Reference.Phone,Reference.Email,Reference.City, Reference.StateProvince, Reference.Country, Reference.ReferenceType from Reference JOIN 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();
}
}
Upvotes: 0
Views: 158
Reputation: 11
Nobody answered my question as always on this website. So after whole hardwork on my own, i'm going to answer myself the first time.
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();
}
}
}
}
Upvotes: 1