Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Using validation controls with gridview when editing data - Part 22

Suggested Videos 
Part 19 - Using optimistic concurrency when editing data in gridview
Part 20 - Keep gridview in edit mode when update fails due to data conflict 
Part 21 - GridView TemplateField in

Please watch Part 21 of the gridview tutorial before proceeding. We want to make "Gender" required field. When editing data in the gridview control, if a user has selected "Select Gender", then the user should not be able to submit the page. We should get "Gender is a required field" error message as shown in the screen shot below.
Using validation controls with gridview when editing data

At the moment we do not have any validation in place when editing data in gridview control. If you have selected "Select Gender" from dropdownlist and submit the page, we get a crash with error message - String or binary data would be truncated.

This is because "Gender" column length in the database is nvrahcar(10). This size is not enough to store the word "Select Gender". To get rid of the error change the length to nvarchar(20) using the sql script below.
ALTER TABLE tblemployee
ALTER COLUMN Gender nvarchar(20)

Now, if you have selected "Select Gender" from dropdownlist and submit the page, the word "Select Gender" will be stored in the database table. We want to avoid this. We only want the page to be submitted, if the user has selected the gender - Male or Female.

Since we are already using a TemplateField to display Gender in the GridView control, this should be very easy to achieve. All we need to do is drag and drop a "RequiredFieldValidator" control next to the dropdownlist in EditItemTemplate. Then configure "RequiredFieldValidator" as shown below.
<asp:TemplateField HeaderText="Gender" SortExpression="Gender">
        <asp:DropDownList ID="DropDownList1" runat="server" 
                          SelectedValue= '<%# Bind("Gender") %>'>
            <asp:ListItem>Select Gender</asp:ListItem>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
            runat="server" ErrorMessage="Gender is a required field" 
            ControlToValidate="DropDownList1" InitialValue="Select Gender" 
            Text="*" ForeColor="Red" >
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Gender") %>'>

Finally drag and drop a "ValidationSummary" control on the webform, after the closing tag of GridView. Set ForeColor="Red" on "ValidationSummary" control. 
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" />

That's it. We are done. Run the application, and when you try to submit the page without selecting "Gender", you get a validation error message as expected. If you are new to validation controls, please watch videos on  validation controls from video tutorial.


  1. I got the following error!!
    WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

    1. on the top of WebForms.cs page at page directive set UnobtrusiveValidationMode = none

  2. Raj Baral follow the link for that error:

  3. I got the following error !
    'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.


It would be great if you can help share these free resources