ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Uncheck ASP.Net GridView Rows based on condition using C# and VB.Net<p>Hey <a class="username" rel="dorsa">dorsa</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;select&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;chkSelect&#34; runat=&#34;server&#34; OnCheckedChanged=&#34;OnCheckedChanged&#34; AutoPostBack=&#34;true&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField DataField=&#34;RoomTitle&#34; HeaderText=&#34;RoomTitle&#34; /&gt; &lt;asp:BoundField DataField=&#34;DayWeek&#34; HeaderText=&#34;DayWeek&#34; /&gt; &lt;asp:BoundField DataField=&#34;EndDate&#34; HeaderText=&#34;EndTime&#34; /&gt; &lt;asp:BoundField DataField=&#34;StartDate&#34; HeaderText=&#34; StartTime&#34; /&gt; &lt;asp:BoundField DataField=&#34;ProfessorLastName&#34; HeaderText=&#34;ProfessorLastName&#34; /&gt; &lt;asp:BoundField DataField=&#34;ProfessorFirstName&#34; HeaderText=&#34;ProfessorName&#34; /&gt; &lt;asp:BoundField DataField=&#34;LessonCode&#34; HeaderText=&#34;LessonCode&#34; /&gt; &lt;asp:BoundField DataField=&#34;LessonTitle&#34; HeaderText=&#34;LessonTitle&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.GridView1.DataSource = GetData(); this.GridView1.DataBind(); } } private DataTable GetData() { &nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp; dt.Columns.AddRange(new DataColumn[] { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("LessonTitle", typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("LessonCode", typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("ProfessorFirstName", typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("ProfessorLastName", typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("StartDate", typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("EndDate", typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("DayWeek", typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("RoomTitle", typeof(string)) }); &nbsp;&nbsp;&nbsp; dt.Rows.Add("mathematical1", "962001", "john", "leni", "10:00:00", "11:30:00", "Saturday", "201"); &nbsp;&nbsp;&nbsp; dt.Rows.Add("mathematical1", "962001", "john", "leni", "09:00:00", "10:30:00", "Sunday", "201"); &nbsp;&nbsp;&nbsp; dt.Rows.Add("mathematical1", "962001", "john", "leni", "11:00:00", "12:30:00", "Monday", "201"); &nbsp;&nbsp;&nbsp; dt.Rows.Add("mathematical1", "962216", "ali", "brn", "14:00:00", "16:30:00", "Saturday", "202"); &nbsp;&nbsp;&nbsp; dt.Rows.Add("mathematical1", "962216", "ali", "brn", "16:30:00", "18:00:00", "Tuesday", "301"); &nbsp;&nbsp;&nbsp; dt.Rows.Add("operating System", "962101", "sara", "kan", "08:00:00", "09:00:00", "Tuesday", "301"); &nbsp;&nbsp;&nbsp; return dt; } protected void OnCheckedChanged(object sender, EventArgs e) { GridViewRow selectedRow = (sender as CheckBox).NamingContainer as GridViewRow; string lessionName = selectedRow.Cells[8].Text; string firstName = selectedRow.Cells[6].Text; if ((sender as CheckBox).Checked) { DataRow[] dr = GetData().Select("ProfessorFirstName='" + firstName + "' AND LessonTitle = '" + lessionName + "'"); if (dr.Length &gt; 1) { foreach (GridViewRow row in GridView1.Rows) { string lession = row.Cells[8].Text; string name = row.Cells[6].Text; if (lessionName.ToLower().Trim() == lession.ToLower().Trim() &amp;&amp; firstName.ToLower().Trim() == name.ToLower().Trim()) { (row.FindControl("chkSelect") as CheckBox).Checked = true; } } } else { (sender as CheckBox).Checked = false; } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Me.GridView1.DataSource = GetData() Me.GridView1.DataBind() End If End Sub Private Function GetData() As DataTable Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;LessonTitle&#34;, GetType(String)), New DataColumn(&#34;LessonCode&#34;, GetType(String)), New DataColumn(&#34;ProfessorFirstName&#34;, GetType(String)), New DataColumn(&#34;ProfessorLastName&#34;, GetType(String)), New DataColumn(&#34;StartDate&#34;, GetType(String)), New DataColumn(&#34;EndDate&#34;, GetType(String)), New DataColumn(&#34;DayWeek&#34;, GetType(String)), New DataColumn(&#34;RoomTitle&#34;, GetType(String))}) dt.Rows.Add(&#34;mathematical1&#34;, &#34;962001&#34;, &#34;john&#34;, &#34;leni&#34;, &#34;10:00:00&#34;, &#34;11:30:00&#34;, &#34;Saturday&#34;, &#34;201&#34;) dt.Rows.Add(&#34;mathematical1&#34;, &#34;962001&#34;, &#34;john&#34;, &#34;leni&#34;, &#34;09:00:00&#34;, &#34;10:30:00&#34;, &#34;Sunday&#34;, &#34;201&#34;) dt.Rows.Add(&#34;mathematical1&#34;, &#34;962001&#34;, &#34;john&#34;, &#34;leni&#34;, &#34;11:00:00&#34;, &#34;12:30:00&#34;, &#34;Monday&#34;, &#34;201&#34;) dt.Rows.Add(&#34;mathematical1&#34;, &#34;962216&#34;, &#34;ali&#34;, &#34;brn&#34;, &#34;14:00:00&#34;, &#34;16:30:00&#34;, &#34;Saturday&#34;, &#34;202&#34;) dt.Rows.Add(&#34;mathematical1&#34;, &#34;962216&#34;, &#34;ali&#34;, &#34;brn&#34;, &#34;16:30:00&#34;, &#34;18:00:00&#34;, &#34;Tuesday&#34;, &#34;301&#34;) dt.Rows.Add(&#34;operating System&#34;, &#34;962101&#34;, &#34;sara&#34;, &#34;kan&#34;, &#34;08:00:00&#34;, &#34;09:00:00&#34;, &#34;Tuesday&#34;, &#34;301&#34;) Return dt End Function Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Dim selectedRow As GridViewRow = TryCast((TryCast(sender, CheckBox)).NamingContainer, GridViewRow) Dim lessionName As String = selectedRow.Cells(8).Text Dim firstName As String = selectedRow.Cells(6).Text If (TryCast(sender, CheckBox)).Checked Then Dim dr As DataRow() = GetData().[Select](&#34;ProfessorFirstName=&#39;&#34; &amp; firstName &amp; &#34;&#39; AND LessonTitle = &#39;&#34; &amp; lessionName &amp; &#34;&#39;&#34;) If dr.Length &gt; 1 Then For Each row As GridViewRow In GridView1.Rows Dim lession As String = row.Cells(8).Text Dim name As String = row.Cells(6).Text If lessionName.ToLower().Trim() = lession.ToLower().Trim() AndAlso firstName.ToLower().Trim() = name.ToLower().Trim() Then TryCast(row.FindControl(&#34;chkSelect&#34;), CheckBox).Checked = True End If Next Else TryCast(sender, CheckBox).Checked = False End If End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/Gc3ANvP.gif" alt="" width="472" height="186" /></p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/158774/Uncheck-ASPNet-GridView-Rows-based-on-condition-using-C-and-VBNet/https://www.aspforums.net:443/Threads/158774/Uncheck-ASPNet-GridView-Rows-based-on-condition-using-C-and-VBNet/Mon, 10 Dec 2018 04:54:19 GMT