在GridView中显示之前编辑数据

时间:2012-05-17 20:01:36

标签: asp.net gridview webforms

所以基本上我从数据库中提取数据,我希望它在gridview中显示。不幸的是,星期几被存储为整数,因此星期一等于0,星期二是1等等。

基本上,我如何在输出数据时更改数据,以便将数字转换为一周中正确的日期。

我有一个网格视图如下,目前我已经设置了onrowdatabound:

 <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
                        AutoGenerateColumns="False" DataSourceID="SqlDataSource2" Width="721px"  
                        onrowdatabound="GridView_RowDataBound" 
                        >

然后GridView_RowDataBound的代码是:

  protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

        }

    }

我认为我只需要在那里放置正确的代码。但是,出于某种原因,我无法对单个单元格执行任何操作,只能对特定列中的每个单元格执行任何操作。所以我可以将一列中的每个单元格更改为粗体,但不能单独更改单元格。我确信有办法,但无法理解如何做到这一点!

任何帮助将不胜感激!

由于

        protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int days = (int)GridView1.Rows[e.Row.RowIndex].Cell[2].Text;
            if (days == 0) GridView1.Rows[e.Row.RowIndex].Cell[2].Text = "Monday";
            if (days == 1) GridView1.Rows[e.Row.RowIndex].Cell[2].Text = "Tuesday";
            if (days == 1) GridView1.Rows[e.Row.RowIndex].Cell[2].Text = "Wednesday";
            if (days == 1) GridView1.Rows[e.Row.RowIndex].Cell[2].Text = "Thursday";
            if (days == 1) GridView1.Rows[e.Row.RowIndex].Cell[2].Text = "Friday";

        }

    }

1 个答案:

答案 0 :(得分:3)

你可以试试这个。

if (e.Row.RowType == DataControlRowType.DataRow)
{
     string days = e.Row.Cells[2].Text;
     if (days == "0") e.Row.Cells[2].Text = "Monday";
     if (days == "1") e.Row.Cells[2].Text = "Tuesday";
     if (days == "2") e.Row.Cells[2].Text = "Wednesday";
     if (days == "3") e.Row.Cells[2].Text = "Thursday";
     if (days == "4") e.Row.Cells[2].Text = "Friday";
}