在 gridview 中获取特定的数据行?
Get specific data row in gridview?
如何在 asp.net 中获取我的 GridView 的特定数据行?
我这里有这张图片:
示例:
我想获取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 的值?我想获取 Student ID 和 CourseNo 的数据行并将其放入我的参数之一:
cmd.Parameters.AddWithValue("@studentID", );
cmd.Parameters.AddWithValue("@Coursenumber", );
此问题只是此 link 此处 的后续问题
现在我想在选中复选框事件并触发按钮 ValidateSubject_Click 事件时更新我的查询。
由于您可以使用 gvRow 访问 GridViewRow,因此您可以从行单元格 1 和 2 访问 StudentID 和 Coursenumber,如下所示
var studentID = grvRow.Cells[0].Text;
var courseNumber = grvRow.Cells[1].Text;
另一个解决您问题的例子是:
<asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False" DataSourceID="StudentDataSource">
<Columns>
<asp:BoundField DataField="Serial" HeaderText="Serial" SortExpression="Serial" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:TemplateField HeaderText="Verified" SortExpression="Verified">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Verified") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Verified") %>'
AutoPostBack="true" OnCheckedChanged="chkVerified_CheckedChanged" />
<asp:HiddenField ID="hddSerial" runat="server" Value='<%# Bind("Serial") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblResult" runat="server" Text="You have validate: N/a"></asp:Label>
<asp:ObjectDataSource ID="StudentDataSource" runat="server" SelectMethod="Get" TypeName="WebFormApp._32846281.Models.Student"></asp:ObjectDataSource>
在你后面的代码中:
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);
}
这解决了这个问题:如果你重新排序列,首先用这个方法,你必须改变后面的代码
在我的解决方案中,如果您对列重新排序,后面的代码是相同的。
如何在 asp.net 中获取我的 GridView 的特定数据行?
我这里有这张图片:
示例: 我想获取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 的值?我想获取 Student ID 和 CourseNo 的数据行并将其放入我的参数之一:
cmd.Parameters.AddWithValue("@studentID", );
cmd.Parameters.AddWithValue("@Coursenumber", );
此问题只是此 link 此处
由于您可以使用 gvRow 访问 GridViewRow,因此您可以从行单元格 1 和 2 访问 StudentID 和 Coursenumber,如下所示
var studentID = grvRow.Cells[0].Text;
var courseNumber = grvRow.Cells[1].Text;
另一个解决您问题的例子是:
<asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False" DataSourceID="StudentDataSource">
<Columns>
<asp:BoundField DataField="Serial" HeaderText="Serial" SortExpression="Serial" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:TemplateField HeaderText="Verified" SortExpression="Verified">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Verified") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Verified") %>'
AutoPostBack="true" OnCheckedChanged="chkVerified_CheckedChanged" />
<asp:HiddenField ID="hddSerial" runat="server" Value='<%# Bind("Serial") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblResult" runat="server" Text="You have validate: N/a"></asp:Label>
<asp:ObjectDataSource ID="StudentDataSource" runat="server" SelectMethod="Get" TypeName="WebFormApp._32846281.Models.Student"></asp:ObjectDataSource>
在你后面的代码中:
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);
}
这解决了这个问题:如果你重新排序列,首先用这个方法,你必须改变后面的代码
在我的解决方案中,如果您对列重新排序,后面的代码是相同的。