gridview中的逐行修改问题
databound如下:
protected void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (GridView1.DataKeys[0][0].ToString() == "1 ")
row.Cells[2].Text = "a ";
else if (GridView1.DataKeys[0][1].ToString() == "1 ")
row.Cells[2].Text = "b ";
else if (GridView1.DataKeys[0][2].ToString() == "1 ")
row.Cells[2].Text = "c ";
else if (GridView1.DataKeys[0][3].ToString() == "1 ")
row.Cells[2].Text = "d ";
else row.Cells[2].Text = "e ";
}
}
目的为判断几个隐藏列的内容,是1的话第3列则显示相应内容。
遇到的问题为:说有两行分别满足显示不同内容的的条件时,都显示rowindex靠前的那种,比如我想要的输出为
01 id1 a
02 id2 b
但实际输出为:
01 id1 a
02 id2 a
我在csdn搜索到过逐行修改的方法为:
foreach (GridViewRow myrow in GridView1.Rows)
{
string i = myrow.Cells[1].Text;
if (i.Contains( "bb ")==true)
{
myrow.Cells[1].Text = i.Replace( "bb ", "cc ");
}
}
写在GridView1_RowDataBound事件里
感觉和我用的没什么不同。。
------解决方案--------------------抱歉啊
应该
if (e.Row.Cell【5】.Text.Trim() == "1 ")
row.Cells[2].Text = "a ";
else if (e.Row.Cells[6].Text.Trim()== "1 ")
row.Cells[2].Text = "b ";
....