3

So basically I have data being pulled from a database for which I want it to display in a gridview. Unfortunately, the days of the week are stored as integers so that monday is equal to 0 and tuesday is 1 etc etc.

Basically, how do I go about changing that data as it is being outputted so that it converts the number to the correct day of the week.

I have got a grid view as follows and at the moment I have it set up with onrowdatabound :

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

Then the code behind for GridView_RowDataBound is :

  protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
    {

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

        }

    }

I assume that I just need to put the right code in there. However, for some reason I can't do anything to a single cell only to every single cell in a particular column. So I could change every cell in one column to bold but can't individually change a cell. I'm sure there is a way but can't fathom how to do it!

Any help would be much appreciated!

Thanks

        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";

        }

    }
Shyju
  • 214,206
  • 104
  • 411
  • 497
user1096685
  • 123
  • 9

1 Answers1

3

You can try this.

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";
}
Amit
  • 21,570
  • 27
  • 74
  • 94
  • Will this loop through all of them though, or just do the top one? This was a problem I was having earlier. I will try this in 30 mins though and will update you (and mark as answer if correct) Thank you for your help tho! – user1096685 May 17 '12 at 21:15
  • `GridView_RowDataBound` this event occurs for every row in gridview so it will set every cell in that column. – Amit May 17 '12 at 21:17
  • I understand. Awesome. Just tried it out and got the following error about the word "Cell" : 'System.Web.UI.WebControls.GridViewRow' does not contain a definition for 'Cell' and no extension method 'Cell' accepting a first argument of type 'System.Web.UI.WebControls.GridViewRow' could be found – user1096685 May 17 '12 at 21:20