gridview一个应用问题
关于Gridview的一个应用问题:
我想在Gridview中如果某一行的某一列字段的值为"Y",那么这一行的背景色为红色,代码我写出来的,页面第一次加载是按照意思显示的,但击其它页不是这样,我知道一定是分页代码问题,请问大家,这个分页代码该如果写呢?
部分代码如下:
protected void bind()
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection();
cmd.CommandType = CommandType.StoredProcedure;
conn.ConnectionString = SqlDataSource1.ConnectionString;
cmd.CommandText = "query_trans4";
SqlDataAdapter myda = new SqlDataAdapter(cmd.CommandText, conn.ConnectionString);
DataSet myds = new DataSet();
conn.Open();
myda.Fill(myds, "trans");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "托运号" };
GridView1.DataBind();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv = myds.Tables["trans"].DefaultView[i];
String mystr = Convert.ToString(mydrv[9]);
if (mystr == "N" || mystr == "")
{
GridView1.Rows[i].BackColor = System.Drawing.Color.Red;
}
else
{
GridView1.Rows[i].BackColor = System.Drawing.Color.White;
}
}
conn.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
bind();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
------解决方案--------------------
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
bind();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
这里明显是错误的...因为bind(); 方法里面已经实现了GridView1.DataBind(); ,,
这里再加上GridView1.DataBind(); 已经重复了,原先bind(); 设为红色被后面GridView1.DataBind(); 冲掉了...
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
DataRowView mydrv = myds.Tables["trans"].DefaultView[i+(GridView1.PageIndex*GridView1.PageSize];
String mystr = Convert.ToString(mydrv[9]);
注意红色的...
这样...