補充程式碼實作區(配合GRIDVIEW控制項文章一起看)
因替代文字時是「資料產生時」就要取代,所以要使用 「onRowDataBound」
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{ //e代表傳入的事件參數
if (e.Row.RowIndex > -1) //因為欄位名稱在索引值是-1 所以只要大於-1就可以排除欄位名稱
{
//if (e.Row.Cells[3].Text == "False")
//{
// e.Row.Cells[3].Text = "女";
//}
//else
//{
// e.Row.Cells[3].Text = "男";
//}
//簡易寫法:Variable = (Statment) ? TrueValue : FalseValue;
e.Row.Cells[3].Text = e.Row.Cells[3].Text == "False" ? "女" : "男";
string stred = "";
switch (e.Row.Cells[4].Text)
{
case "1":
stred = "國小";
break;
case "2":
stred = "國中";
break;
case "3":
stred = "高中";
break;
case "4":
stred = "大學";
break;
default:
stred = "研究所以上";
break;
}
e.Row.Cells[4].Text = stred;
}
}
補充CELL計算畫面: