-1

我在 gridview 中的行命令上收到此错误。这是代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = University.GetConnectionString();
        con.Open();

        string query = "select [CourseCode], [CourseNumber], [CourseName], [CRN], [Level], [Credit] from CourseTable where Term='" + MyGlobals.currentTerm + " " + MyGlobals.currentYear + "'";

        SqlDataAdapter adap = new SqlDataAdapter(query, con);

        DataTable tab = new DataTable();

        adap.Fill(tab);

        gCourses.DataSource = tab;
        gCourses.DataBind();
    }

}



protected void gCourses_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // *** Retreive the DataGridRow
    int row = -1;
    int.TryParse(e.CommandArgument as string, out row);

    GridViewRow gdrow = gCourses.Rows[row];
    DataRow dr = ((DataTable)this.gCourses.DataSource).Rows[gdrow.DataItemIndex];
    string crn = dr["CRN"].ToString();
}

.

DataRow dr = ((DataTable)this.gCourses.DataSource).Rows[gdrow.DataItemIndex]; 

行抛出异常。

这里有什么问题?谢谢

4

1 回答 1

1

去除

if (!isPostback)

从您的页面加载

于 2013-06-10T13:13:43.310 回答