如果数据是显示在Girdview控件的绑定列中,则转换比较简单,参考如下代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//将班级类别编号转换为班级类别名称
if (e.Row.Cells[5].Text == "1") //5是要转换的列的序号,从0开始
{
e.Row.Cells[5].Text = "新生班级";
}
else if (e.Row.Cells[5].Text == "2")
{
e.Row.Cells[5].Text = "二次分班";
}
}
}
但是如果数据是显示在GridView控件的模板列中,则不能采用以上的方式,可参考如下代码:{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//将班级类别编号转换为班级类别名称
if (e.Row.Cells[5].Text == "1") //5是要转换的列的序号,从0开始
{
e.Row.Cells[5].Text = "新生班级";
}
else if (e.Row.Cells[5].Text == "2")
{
e.Row.Cells[5].Text = "二次分班";
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//将班级类别编号转换为班级类别名称
Label lbl = (Label)e.Row.FindControl("Label7"); //使用模板列的情况
if (lbl != null)
{
if (lbl.Text.Trim() == "1")
{
lbl.Text = "新生班级";
}
else if (lbl.Text.Trim() == "2")
{
lbl.Text = "二次分班";
}
}
}
"Label7"是模板列显示数据时借助的Label控件的ID
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//将班级类别编号转换为班级类别名称
Label lbl = (Label)e.Row.FindControl("Label7"); //使用模板列的情况
if (lbl != null)
{
if (lbl.Text.Trim() == "1")
{
lbl.Text = "新生班级";
}
else if (lbl.Text.Trim() == "2")
{
lbl.Text = "二次分班";
}
}
}