选择gridview行/单元格错误“输入字符串格式不正确”

时间:2012-12-09 06:07:46

标签: c# asp.net gridview

我有我的gridview设置,用户可以“选择”他们想要的行,而这些行又应该通过数据库并重定向它们。但是,当我点击任何给定行结果的“select”时,我在此行收到错误“输入字符串格式不正确”: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; }
}

1 个答案:

答案 0 :(得分:0)

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

另一件事是,如果您在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");
    }