Reputation: 1482
How do I get the specific data row of my GridView in asp.net?
I have this image here:
Example: I want to get the specific data row of the studentID = 2011017997 and its CourseNo = 'CmpE 515';
how do I get their specific data? Does GridViewRow has one of that built in functions to get the data row?
Here is the aspx code:
<%@ 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>
Here is my aspx code behind:
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;
}
}
}
}
How can I fetch the value of the studentID and courseNo in my gridview? I want to get the data row of Student ID and CourseNo and put it in one of my parameters here:
cmd.Parameters.AddWithValue("@studentID", );
cmd.Parameters.AddWithValue("@Coursenumber", );
This question is just a follow up question to this link here (Change text in specific row) Now this time I want to update my query when checkbox event is checked and button ValidateSubject_Click event is fired.
Upvotes: 3
Views: 7009
Reputation: 319
An other example to resolve your problem is:
<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>
in your code behind:
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);
}
That resolve this problem: if you reorder colums, with first the method, you must change code behind
In my solution if you reorder columns the code behind is same.
Upvotes: 1
Reputation: 1634
Since you were able to access the GridViewRow using gvRow, you can access StudentID and Coursenumber from row cells 1 and 2 as follows
var studentID = grvRow.Cells[0].Text;
var courseNumber = grvRow.Cells[1].Text;
Upvotes: 2