【问题标题】:selecting gridview rows/cells error "input string was not in correct format"选择gridview行/单元格错误“输入字符串格式不正确”
【发布时间】:2012-12-09 06:07:46
【问题描述】:

我设置了gridview,用户可以在其中“选择”他们想要的行,这反过来应该通过数据库并重定向它们。但是,当我为任何给定的结果行单击“选择”时,我在此行收到错误“输入字符串格式不正确”: myEvent.EventID = Convert.ToInt32(row.Cells[0].Text);我把这件作品拿出来,我得到了永远与其中的转换一致的错误。所以我知道它写得不正确,但我不确定如何写。任何帮助都会很棒!

对不起,我是新手!

我的 girdview 代码:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Event myEvent = new Event();
        GridViewRow row = GridView1.SelectedRow;
        myEvent.EventID = Convert.ToInt32(row.Cells[0].Text);
        myEvent.Sport = row.Cells[1].Text;
        myEvent.EventName = row.Cells[2].Text;
        myEvent.Section = Convert.ToInt32(row.Cells[3].Text);
        myEvent.TicketsAvailable = Convert.ToInt32(row.Cells[4].Text);
        myEvent.EventDate = Convert.ToDateTime(row.Cells[5].Text);
        Session["myEvent"] = myEvent;

        Response.Redirect("Complete.aspx");
    }

我的活动代码,以防您也需要它:

public class Event
{
    public int EventID { get; set; }
    public string EventName { get; set; }
    public string Sport { get; set; }
    public int Section { get; set; }
    public int TicketsAvailable { get; set; }
    public DateTime EventDate { get; set; }
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    只需检查天气,您要转换的值不为空。尝试调试代码并检查您从该特定行获得的值是什么。

    另一件事是,如果您在 gridview 中创建了生成的选择按钮,那么该选择按钮的位置在 gridview 的 0 单元格处。所以你可以试试这样的代码。

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Event myEvent = new Event();
            GridViewRow row = GridView1.SelectedRow;
            myEvent.EventID = Convert.ToInt32(row.Cells[1].Text);
            myEvent.Sport = row.Cells[2].Text;
            myEvent.EventName = row.Cells[3].Text;
            myEvent.Section = Convert.ToInt32(row.Cells[4].Text);
            myEvent.TicketsAvailable = Convert.ToInt32(row.Cells[5].Text);
            myEvent.EventDate = Convert.ToDateTime(row.Cells[6].Text);
            Session["myEvent"] = myEvent;
    
            Response.Redirect("Complete.aspx");
        }
    

    【讨论】:

    • 哦,我很傻。在网格视图中,它将每个“选择”选项计为一列。所以我只需要从 [1] 而不是 [0] 开始就可以了。感谢您的快速回复!! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    相关资源
    最近更新 更多