GridView绑定数据后想要更换每个单元格中内容的方法

 上一个篇写的不明确,这篇进行详细的解释:

1,首先是GridView控件有一个事件就是GridView1_RowDataBound,此事件是在设置了GridView的绑定后每绑定一条数据就会执行一次的;

2,参数e是传过来的GridView1,此时可以对GridView1进行设置了,e.Row.RowType是指GridView1中每行的类型,一般在DataControlRowType中,如:DataControlRowType.DataRow(数据行),其中还有Pager(显示导航按钮的行)、Header(表头)、Footer(脚注释)、Separator(行分隔符)等。

3,例子如下该GridView控件的ID="GridTimezone"。

protected void GridTimezone_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) //如果是数据行
            {
                var obj = (BaseInfo_BusinessTimeConfig)e.Row.DataItem;
                e.Row.Cells[2].Text = obj.DayStart + ":00";
                e.Row.Cells[3].Text = obj.DayEnd + ":00";
                e.Row.Cells[4].Text = obj.MonthStart  + "日";
                e.Row.Cells[5].Text = obj.MonthEnd + "日";
                e.Row.Cells[6].Text = obj.YearStart  + "月";
                e.Row.Cells[7].Text = obj.YearEnd + "月";
                if (obj.WeekStart == 0)
                {
                    e.Row.Cells[8].Text="周日";
                }
                else
                {
                    e.Row.Cells[8].Text ="周"+obj.WeekStart.ToString();
                }
                if (obj.WeekEnd == 0)
                {
                    e.Row.Cells[9].Text = "周日";
                }
                else
                {
                    e.Row.Cells[9].Text = "周" + obj.WeekEnd.ToString();
                }
            }
        }

posted @ 2012-11-22 14:53  namehwh  阅读(2486)  评论(0编辑  收藏  举报