我正在尝试读取 gridview 行。这是我的代码:
protected void allStudents_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Approval")
{
// *** Retreive the DataGridRow
int row = -1;
int.TryParse(e.CommandArgument as string, out row);
GridViewRow gdrow = allStudents.Rows[row];
// *** Get the underlying data item - in this case a DataRow
DataRow dr = ((DataTable)this.allStudents.DataSource).Rows[gdrow.DataItemIndex];
// *** Retrieve our context
string courseCode = dr["CourseCode"].ToString();
string courseNumber = dr["CourseNumber"].ToString();
string term = dr["Term"].ToString();
SqlConnection con = new SqlConnection();
con.ConnectionString = Userfunctions.GetConnectionString();
con.Open();
SqlCommand cmd = new SqlCommand("SELECT RegisterTable.StudentID, StudentTable.Name, StudentTable.Surname FROM RegisterTable JOIN StudentTable ON RegisterTable.StudentID = StudentTable.ID WHERE RegisterTable.CourseCode = @courseCode AND RegisterTable.Term = @term AND RegisterTable.CourseNumber = @courseNumber", con);
cmd.Parameters.AddWithValue("@courseCode", courseCode);
cmd.Parameters.AddWithValue("@courseNumber", courseNumber);
cmd.Parameters.AddWithValue("@term", term);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
transcript.DataSource = dt;
transcript.DataBind();
Label1.Visible = true;
Label1.Text = "Students who are registered to " + courseCode + " " + courseNumber + " are listed below:";
}
}
但是在开始的那一行
DataRow dr = ((DataTable)this.allStudents.DataSource).Rows[gdrow.DataItemIndex];
我收到一条错误消息,提示“对象引用未设置为对象的实例。”。谁能告诉这里的问题?
注意:allStudents 是我的 gridview 的 ID
谢谢