有问题请教,在gridview中我实现对会员信息进行加红显示:
加红代码:
protected void Page_Load(object sender, EventArgs e)
{
//绑定查询数据
DataTable dt = new DataTable();
Connection con = new Connection(string.Format(" select *from Personal_info order by Published Desc"), dt); GridView1.DataSource = dt;
GridView1.DataBind(); for (int i = 0; i < GridView1.Rows.Count; i++)
{
//获取REDDay的值
int RedDay = Convert.ToInt32( dt.Rows [i][29]); //获取发布时间
DateTime t1 = Convert.ToDateTime(dt.Rows[i][26]); //获取当前时间
DateTime t2 = DateTime.Now; //设置一个时间间隔
System.TimeSpan ts = t2 - t1; //将间隔转为int类型用来比较
int days = ts.Days;
//实现会员变红需要两条件 1.首先通过验证 2.申请的天数大于当前日期减去发布日期
if (days <= RedDay && ((CheckBox)GridView1.Rows[i].Cells[9].Controls[0]).Checked)
{
//实现加红显示
GridView1.Rows[i].ForeColor = System.Drawing.Color.Red; }
}
//隐藏不需要的列
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[8].Visible = false;
GridView1.Rows[i].Cells[9].Visible = false;
}
}当我把gridview控件的分页功能启用后,我的这个加红功能就失效了。请问如何解决?
加红代码:
protected void Page_Load(object sender, EventArgs e)
{
//绑定查询数据
DataTable dt = new DataTable();
Connection con = new Connection(string.Format(" select *from Personal_info order by Published Desc"), dt); GridView1.DataSource = dt;
GridView1.DataBind(); for (int i = 0; i < GridView1.Rows.Count; i++)
{
//获取REDDay的值
int RedDay = Convert.ToInt32( dt.Rows [i][29]); //获取发布时间
DateTime t1 = Convert.ToDateTime(dt.Rows[i][26]); //获取当前时间
DateTime t2 = DateTime.Now; //设置一个时间间隔
System.TimeSpan ts = t2 - t1; //将间隔转为int类型用来比较
int days = ts.Days;
//实现会员变红需要两条件 1.首先通过验证 2.申请的天数大于当前日期减去发布日期
if (days <= RedDay && ((CheckBox)GridView1.Rows[i].Cells[9].Controls[0]).Checked)
{
//实现加红显示
GridView1.Rows[i].ForeColor = System.Drawing.Color.Red; }
}
//隐藏不需要的列
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[8].Visible = false;
GridView1.Rows[i].Cells[9].Visible = false;
}
}当我把gridview控件的分页功能启用后,我的这个加红功能就失效了。请问如何解决?
可以将你的处理写在gridview的RowDataBound事件里去就可以解决了,不过你的写法要改改,或者将你page_load
里写的代码放到一个方法里,你的PageIndexChanging事件也去调用这个方法就行了
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//获取REDDay的值
int RedDay = Convert.ToInt32( dt.Rows [i][29]); //获取发布时间
DateTime t1 = Convert.ToDateTime(dt.Rows[i][26]); //获取当前时间
DateTime t2 = DateTime.Now; //设置一个时间间隔
System.TimeSpan ts = t2 - t1; //将间隔转为int类型用来比较
int days = ts.Days;
//实现会员变红需要两条件 1.首先通过验证 2.申请的天数大于当前日期减去发布日期
if (days <= RedDay && ((CheckBox)GridView1.Rows[i].Cells[9].Controls[0]).Checked)
{
//实现加红显示
GridView1.Rows[i].ForeColor = System.Drawing.Color.Red; }
}
}
}
up
你的方式写在page_load中,分页时自然没有加载颜色处理程序。