3

所以基本上我有从数据库中提取的数据,我希望它显示在网格视图中。不幸的是,星期几存储为整数,因此星期一等于 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";

        }

    }
4

1 回答 1

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";
}
于 2012-05-17T21:05:59.850 回答