http://www.dotblogs.com.tw/eason.yen/archive/2011/01/31/21164.aspx
1 | protected void GridView_RowCreated( object sender, GridViewRowEventArgs e) |
3 | if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow) |
6 | e.Row.Cells[1].Visible = false ; |
http://demo.tc/Post/88
◆第一種方式算是比較小技巧的我們再GridView的RowCreated事件中撰寫以下code
- protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
- e.Row.Cells[0].Visible = false;
- }
然後在RowDataBound事件中,一樣可以利用Row.Cells的方式找到它
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- string aa = e.Row.Cells[0].Text;
- }
- }
原因很簡單因為RowCreated事件晚於DataBinding所以它在bind以後才被隱藏當然你在RowDataBound就可以找到資料了
◆第二種方式看起來就比較有點水準@@
我們是直接使用ui介面把某個欄位隱藏起來(Visible = False),然後直接指定DataItem的方式去找到它,我們需要在RowDataBound事件撰寫以下code
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- Boolean draft = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "draft"));
- if (draft == true)
- e.Row.ForeColor = System.Drawing.Color.Green;
- }
- }
以上的code我是把draftx欄位轉換成布林,然後去判斷是真是假再去對該ROW的字色作變換
沒有留言:
張貼留言