在gridview中获取特定数据行?

如何在asp.net中获取GridView的特定数据行?

我有这个图像:

在此处输入图像描述

示例:我想获取studentID = 2011017997的特定数据行及其CourseNo =’CmpE 515′;

我如何获得他们的具体数据? GridViewRow是否具有内置函数之一来获取数据行?

这是aspx代码:

    Student Assessment Form           

Validation of Subjects


这是我的aspx代码:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; namespace SoftwareAnalysisAndDesign.SAD { public partial class ValidateSubjectTeacher : System.Web.UI.Page { CheckBox check = new CheckBox(); protected void Page_Load(object sender, EventArgs e) { if (Session["ValidateSubject"] == null) { Response.Redirect("TeacherPage.aspx", true); } if (!IsPostBack) { ValidateSubject.DataSource = Session["ValidateSubject"]; ValidateSubject.DataBind(); } foreach (GridViewRow row in ValidateSubject.Rows) { check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox; //position Check column on last row in gridview check.Enabled = true; check.CheckedChanged += ValidateSubject_Click; //Bind the event on the button check.AutoPostBack = true; //Set the AutoPostBack property to true } } protected void ValidateSubject_Click(object sender, EventArgs e) { CheckBox chk = (CheckBox)sender; GridViewRow grvRow = (GridViewRow)chk.NamingContainer;//This will give row string validated = "Validated"; string notyetvalidated = "Not yet validated"; if (chk.Checked) { grvRow.Cells[10].Text = validated; //Open Connection using (SqlConnection conn = new SqlConnection("Data Source=Keith;Initial Catalog=SAD;Integrated Security=True")) { //Open Connection to database try { conn.Open(); } catch (Exception E) { Console.WriteLine(E.ToString()); } using (SqlCommand cmd = new SqlCommand("Update AssessmentForm set Status = '@Validated' where StudentID = @studentID and CourseNo = '@Coursenumber'" ,conn)) { cmd.Parameters.AddWithValue("@Validated", validated); cmd.Parameters.AddWithValue("@studentID", ); cmd.Parameters.AddWithValue("@Coursenumber", ); } //Close Connection to database try { conn.Open(); } catch (Exception E) { Console.WriteLine(E.ToString()); } } } else { grvRow.Cells[10].Text = notyetvalidated; } } } } 

如何在gridview中获取studentID和courseNo的值? 我想获取学生ID和CourseNo的数据行,并将其放在我的一个参数中:

 cmd.Parameters.AddWithValue("@studentID", ); cmd.Parameters.AddWithValue("@Coursenumber", ); 

这个问题只是这个链接的后续问题(更改特定行中的文本)现在这次我想在选中复选框事件并触发按钮ValidateSubject_Click事件时更新我的​​查询。

由于您可以使用gvRow访问GridViewRow ,因此您可以从行单元格1和2访问StudentID和Coursenumber,如下所示

 var studentID = grvRow.Cells[0].Text; var courseNumber = grvRow.Cells[1].Text; 

解决问题的另一个例子是:

                    

在你的代码背后:

  protected void chkVerified_CheckedChanged(object sender, EventArgs e) { CheckBox chk = (CheckBox)sender; GridViewRow grvRow = (GridViewRow)chk.NamingContainer; HiddenField hf = grvRow.FindControl("hddSerial") as HiddenField; lblResult.Text = string.Format("You have validate:{0}", hf.Value); } 

这解决了这个问题:如果你重新排序colums,首先是方法,你必须改变后面的代码

在我的解决方案中,如果重新排序列,则后面的代码是相同的。