asp.netgridviewwebformsrowdatabound

Webforms OnRowDataBound cell text change causes columns to appear read-only


First, I did try to manually set readonly to false. My code is company code so I can't share it here. I'll do my best to explain.

In a Webforms projects my .aspx page has a GridView with all the columns listed that I want to display. Additionally it has edit, delete, and update buttons for each row. Three columns are dates, and the data conversion from Oracle to asp ends up making the dates appear as "MM/DD/YYYY 12:00:00am". To strip off the timestamp, I put e.Row.Cells[x].Text = e.Row.Cells[x].Text.Split(' ')[0]; in the GridView's OnRowDataBound event.

It worked beautifully, I thought, until I started testing the edit buttons again. All three rows that have dates and the event above are now a blank white space when I put the row into edit mode. If I comment those three lines in OnRowDataBound out, the timestamps reappear and the rows are editable.

First and foremost, I need to make it work. Secondly, I'd love to know what is actually happening in the background. I'm pretty new to web dev as a whole and new to asp.net for sure.


Solution

  • If you create your columns in markup (AutoCreateColumns being turned off), you can use DataFormatString to format the date columns instead of modifying the cell text in code:

    <asp:BoundField DataField="Date1" DataFormatString="{0:d}" ApplyFormatInEditMode="true" ... />
    

    The format string (d in my example) can be modified to better match your preferences. And if the date columns are editable, you can also set ApplyFormatInEditMode as shown above.