简体   繁体   中英

Changing GridView Cell Text OnEditing “Input string was not in a correct format.” C# ASP.NET

I am trying to display a fairly simple weekly schedule on an ASP.NET website that ties into an SQL table. The problem I am having is I the week day values are held in the SQL table as integers (1 through 7). I wanted to display the integers to the user as actual week day names, which works just find using the OnRowDataBound event. But, when I change the weekdays back to integers for editing I am getting a JScript error that the "Input string was not in the correct format".

At first I thought it may be an issue with one of the cells other then the two I am messing with, but as far as I can tell that is not the case. Unfortunately the JScript error is not giving me enough information to really debug this problem, and I know that the OnEditing event is successfully changing the text of the two boundfields back to a number

So my question is, what is the best way to fix this? Is there a better way to display week day names to the user then change those names to integers when they want to edit a row? Is there a way I can change the edit template to use something like a drop down list or something like that?

<asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns="false"
    AutoGenerateDeleteButton="true"
    AutoGenerateEditButton="true"
    CssClass="view"
    DataKeyNames="TimeID"
    DataSourceID="SqlDataSource1"
    OnRowDataBound="GridView1_RowDataBound"
    OnRowEditing="GridView1_RowEditing"
    Width="100%">
    <Columns>
        <asp:BoundField DataField="TimeID" HeaderText="TimeID" ReadOnly="true" Visible="false" />
        <asp:BoundField DataField="WeekDayStart" HeaderText="Week Day Start" />
        <asp:BoundField DataField="WeekDayEnd" HeaderText="Week Day End" />
        <asp:BoundField DataField="StartTime" HeaderText="Start Time" />
        <asp:BoundField DataField="EndTime" HeaderText="End Time" />
    </Columns>
</asp:GridView>

// OnRowDataBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // Converts the text of WeekDayStart and WeekDayEnd to be actual week days
    if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)
    {
        e.Row.Cells[2].Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[Convert.ToInt32(e.Row.Cells[2].Text) - 1];
        e.Row.Cells[3].Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[Convert.ToInt32(e.Row.Cells[3].Text) - 1];
    }
}


// OnRowEditing
// CURRENTLY THROWING ERROR WHEN EDIT BUTTON IS CLICKED (JScript runtime error)
// "Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in a correct format."
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.Rows[e.NewEditIndex].Cells[2].Text = (Array.IndexOf(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames,
        GridView1.Rows[e.NewEditIndex].Cells[2].Text) + 1).ToString();
    GridView1.Rows[e.NewEditIndex].Cells[3].Text = (Array.IndexOf(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames,
        GridView1.Rows[e.NewEditIndex].Cells[3].Text) + 1).ToString();
}

The error is happening server side, you are getting a javascript error because you are using asp.net ajax (an update panel maybe). Debug on the server and it will catch your error. At a guess you are trying to parse an empty string or some other invalid value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM