sumit sankpal

sumit sankpal

  • 1.7k
  • 68
  • 2k

last row of a gridview can empty validation is not needed .

Jan 11 2024 6:51 AM

aspx

 <div class="row">
      <div class="col-lg-12 mb-4">
               <div  class="card table-responsive">
              <asp:GridView ID="gvAcademicQualification" runat="server" OnRowDataBound="gvAcademicQualification_RowDataBound" DataKeyNames="DegreeName" Width="100%" AutoGenerateColumns="False"  BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
                  <Columns>
                      <asp:BoundField DataField="DegreeName" HeaderText="DegreeName"  />

                      <asp:TemplateField HeaderText="Stream / Discipline">
                          <ItemTemplate>
                              <asp:TextBox ID="txtStream" runat="server" Width="95%" ></asp:TextBox>
                                   <asp:RequiredFieldValidator ID="rtxtStream" runat="server" ControlToValidate="txtStream"
                                  ErrorMessage="*" Font-Bold="False" Font-Names="Verdana" Font-Size="8pt"
                                  SetFocusOnError="True" ValidationGroup="v1" ForeColor="Red" ></asp:RequiredFieldValidator>    
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="Institute Name">
                          <ItemTemplate>
                              <asp:TextBox ID="txtInstituteName" runat="server" Width="95%"  ></asp:TextBox>
                                   <asp:RequiredFieldValidator ID="rtxtInstituteName" runat="server" ControlToValidate="txtInstituteName"
                                  ErrorMessage="*" Font-Bold="False" Font-Names="Verdana" Font-Size="8pt"
                                  SetFocusOnError="True" ValidationGroup="v1" ForeColor="Red" ></asp:RequiredFieldValidator>    
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="Percentage/ Grade">
                          <ItemTemplate>
                              <asp:TextBox ID="txtGrade" runat="server" Width="95%" ></asp:TextBox>
                                   <asp:RequiredFieldValidator ID="rtxtGrade" runat="server" ControlToValidate="txtGrade"
                                  ErrorMessage="*" Font-Bold="False" Font-Names="Verdana" Font-Size="8pt"
                                  SetFocusOnError="True" ValidationGroup="v1" ForeColor="Red" ></asp:RequiredFieldValidator>    
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="Year of Passing">
                          <ItemTemplate>
                              <asp:TextBox ID="txtYearOfPassing" runat="server"  Width="95%"  onkeypress="return isNumberKey(event)"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="rtxtYearOfPassing" runat="server" ControlToValidate="txtYearOfPassing"
                                  ErrorMessage="*" Font-Bold="False" Font-Names="Verdana" Font-Size="8pt"
                                  SetFocusOnError="True" ValidationGroup="v1" ForeColor="Red" ></asp:RequiredFieldValidator>    

                          </ItemTemplate>
                      </asp:TemplateField>
                  </Columns>
                  <FooterStyle BackColor="White" ForeColor="#000066" />
                  <HeaderStyle BackColor="#6777ef" Font-Bold="True" ForeColor="White" />
                  <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                  <RowStyle ForeColor="#000066" />
                  <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                  <SortedAscendingCellStyle BackColor="#F1F1F1" />
                  <SortedAscendingHeaderStyle BackColor="#007DBB" />
                  <SortedDescendingCellStyle BackColor="#CAC9C9" />
                  <SortedDescendingHeaderStyle BackColor="#00547E" />
              </asp:GridView>
          </div>
      </div>
  </div>

code behind file in aspx.cs in submit button event.

for (int i = 0; i < gvAcademicQualification.Rows.Count; i++)
{
    // Only perform validation for the first, second, and third rows not on 4th row
    if (i == 3)
    {
        GridViewRow row = gvAcademicQualification.Rows[i];

        // Find the TextBox controls within the row
        TextBox txtStream = (TextBox)row.FindControl("txtStream");
        TextBox txtInstituteName = (TextBox)row.FindControl("txtInstituteName");
        TextBox txtGrade = (TextBox)row.FindControl("txtGrade");
        TextBox txtYearOfPassing = (TextBox)row.FindControl("txtYearOfPassing");

        // Perform validation for the desired fields
        if (string.IsNullOrEmpty(txtStream.Text))
        {
            // Handle the validation error for Stream field
            // You can display an error message or highlight the invalid row

            RequiredFieldValidator rtxtStream = FindControl("rtxtStream") as RequiredFieldValidator;
            if (rtxtStream != null)                               
                rtxtStream.Visible = false;
        }

        if (string.IsNullOrEmpty(txtInstituteName.Text))
        {
            // Handle the validation error for Institute Name field
            // You can display an error message or highlight the invalid row
            RequiredFieldValidator rtxtStream = FindControl("rtxtStream") as RequiredFieldValidator;
            if (rtxtStream != null)
                rtxtStream.Visible = false;
        }

        if (string.IsNullOrEmpty(txtGrade.Text))
        {
            // Handle the validation error for Grade field
            // You can display an error message or highlight the invalid row
            RequiredFieldValidator rtxtStream = FindControl("rtxtStream") as RequiredFieldValidator;
            if (rtxtStream != null)
                rtxtStream.Visible = false;
        }

        if (string.IsNullOrEmpty(txtYearOfPassing.Text))
        {
            // Handle the validation error for Year of Passing field
            // You can display an error message or highlight the invalid row
            RequiredFieldValidator rtxtStream = FindControl("rtxtStream") as RequiredFieldValidator;
            if (rtxtStream != null)
                rtxtStream.Visible = false;
        }
    }
}

 


Answers (3)