其他分享
首页 > 其他分享> > 在GridView中获取特定的数据行?

在GridView中获取特定的数据行?

作者:互联网

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

我在这里有这张图片:

enter image description here

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

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

这是aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidateSubjectTeacher.aspx.cs" Inherits="SoftwareAnalysisAndDesign.SAD.ValidateSubjectTeacher" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Student Assessment Form</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link rel="stylesheet" href="Bootstrap/css/bootstrap-theme.css" />
    <link rel="stylesheet" href="Bootstrap/css/bootstrap.min.css"/> 
    <link rel="stylesheet" type="text/css" href="Stylesheets/ValidateSubjectTeacherStyle.css"/>

    <!--Side bar link-->
    <link rel="stylesheet" href="SidebarBootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="SidebarBootstrap/css/simple-sidebar.css" />
</head>
<body>
    <form id="form1" runat="server">
    <div class="container" style="text-align: center; margin: 0 auto;">
        <br />
        <h1>Validation of Subjects</h1>
        <br />
        <asp:GridView runat="server" CssClass="table table-hover table-bordered" ID="ValidateSubject" Style="text-align: center"></asp:GridView>
    </div>

    <div style="float: right; padding-right: 75px;">
        <button type="button" runat="server" class="btn btn-primary" onserverclick="ValidateSubject_Click">Validate</button>
    </div>
    </form>

    <script src="jquery/jquery.min.js"></script>
    <script src="Bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

这是我后面的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", );

这个问题只是这里链接的后续问题(Change text in specific row)
现在,这次我想在选中复选框事件并且触发按钮ValidateSubject_Click事件时更新查询.

解决方法:

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

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

标签:gridview,code-behind,asp-net,c,sql-server
来源: https://codeday.me/bug/20191119/2037851.html