Validate GridView rows based on condition in ASP.Net

Last Reply 2 months ago By dharmendr

Posted 2 months ago

Hi all,

I have the below condition to full fill my requirement.

below one is my gridview

                            <asp:GridView ID="Gv1Data" runat="server" Width="1150px" GridLines="Vertical" AutoGenerateColumns="false" ShowHeader="false">
                                <Columns>
                                    <asp:TemplateField HeaderText="Column1">
                                        <ItemTemplate>
                                            <asp:TextBox runat="server" ID="Section_Column1" Text='<%# Eval("Section_Column1") %>' CssClass="TxtBox" ToolTip='<%# Eval("Section_Column1") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Column2">
                                        <ItemTemplate>
                                            <asp:TextBox runat="server" ID="Section_Column2" Text='<%# Eval("Section_Column2") %>' CssClass="TxtBox" ToolTip='<%# Eval("Section_Column2") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Column3">
                                        <ItemTemplate>
                                            <asp:TextBox runat="server" ID="Section_Column3" Text='<%# Eval("Section_Column3") %>' CssClass="TxtBox" ToolTip='<%# Eval("Section_Column3") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Column4">
                                        <ItemTemplate>
                                            <asp:TextBox runat="server" ID="Section_Column4" Text='<%# Eval("Section_Column4") %>' CssClass="TxtBox" ToolTip='<%# Eval("Section_Column4") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
								</Columns>
                            </asp:GridView>

 

i am binding database with gridview at page load, after next i am click the save button i want to validate below conditions.

if i entered gridview details are below like ,

Column1 Column2 Column3 Column4
       
       
       

in above grid i am entered first row details like below and save button click then no need to alert fill row details gridview.

Column1 Column2 Column3 Column4
Column      
       

in above condition the user entered first row first cell then we don't need alert message enter cell details.

Column1

Column2

Column3

Column4

F1Cell1

F1Cell2

 

F1Cell4

F2Cell2

F2Cell2

F2Cell3

F2Cell4

 

 

 

 

 

 

 

 

in above condition also no need to alert message , why the 1st and 2nd row , first cell is filled.

Column1

Column2

Column3

Column4

 

F1Cell2

F1Cell3

F1Cell4

 

F2Cell2

F2Cell3

F2Cell4

 

 

 

 

 

 

 

 

In above condition we require alert message why the 1st and 2nd row first cells are not entered. so, we required alert message.

Column1

Column2

Column3

Column4

 

 

 

 

F2Cell1

F2Cell2

F2Cell3

F2Cell4

 

 

 

 

 

 

 

 

In this case the first row first cell or entire first row all cells are not enter. so, we want to give alert message for enter cell fields.

Column1

Column2

Column3

Column4

F1Cell1

F1Cell2

F1Cell3

F1Cell4

 

 

 

 

F3Cell1

F3Cell2

F3Cell3

F3Cell4

 

 

 

 

In this case the 2nd row first cell or entire 2nd row all cells are not enter. so, we want to give alert message for enter cell fields.

How to validate above conditions. please help me.

Thanks in advance

Posted 2 months ago Modified on 2 months ago

Hi nagaraju60,

Check this sample code. Now please take its reference and correct your code.

HTML

<asp:GridView ID="Gv1Data" runat="server" Width="1150px" GridLines="Vertical" AutoGenerateColumns="false"
    ShowHeader="false">
    <Columns>
        <asp:TemplateField HeaderText="Column1">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Section_Column1" Text='<%# Eval("Section_Column1") %>'
                    CssClass="TxtBox" ToolTip='<%# Eval("Section_Column1") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Column2">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Section_Column2" Text='<%# Eval("Section_Column2") %>'
                    CssClass="TxtBox" ToolTip='<%# Eval("Section_Column2") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Column3">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Section_Column3" Text='<%# Eval("Section_Column3") %>'
                    CssClass="TxtBox" ToolTip='<%# Eval("Section_Column3") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Column4">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Section_Column4" Text='<%# Eval("Section_Column4") %>'
                    CssClass="TxtBox" ToolTip='<%# Eval("Section_Column4") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnValidate" runat="server" Text="Submit" OnClick="btnValidate_Click" />

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("Section_Column1", typeof(string)),
                    new DataColumn("Section_Column2", typeof(string)),
                    new DataColumn("Section_Column3", typeof(string)),
                    new DataColumn("Section_Column4", typeof(string)) });
        dt.Rows.Add("", "", "", "");
        dt.Rows.Add("", "", "", "");
        dt.Rows.Add("", "", "", "");
        Gv1Data.DataSource = dt;
        Gv1Data.DataBind();
    }
}

protected void btnValidate_Click(object sender, EventArgs e)
{
    if (AllRowEmptyOrHasValue())
    {
        return;
    }

    bool allColumnEmpty = false;
    bool continiousEmpty = false;
    bool previousRowEmpty = false;
    for (int row = 0; row < Gv1Data.Rows.Count; row++)
    {
        bool column1Empty = false;
        string column1 = (Gv1Data.Rows[row].FindControl("Section_Column1") as TextBox).Text;
        string column2 = (Gv1Data.Rows[row].FindControl("Section_Column2") as TextBox).Text;
        string column3 = (Gv1Data.Rows[row].FindControl("Section_Column3") as TextBox).Text;
        string column4 = (Gv1Data.Rows[row].FindControl("Section_Column4") as TextBox).Text;

        if (!string.IsNullOrEmpty(column2) || !string.IsNullOrEmpty(column3) || string.IsNullOrEmpty(column4))
        {
            if (string.IsNullOrEmpty(column1))
            {
                column1Empty = true;
                if (!previousRowEmpty && continiousEmpty)
                {
                    ShowMessage();
                    return;
                }
            }
        }
        if (string.IsNullOrEmpty(column2) && string.IsNullOrEmpty(column3) && string.IsNullOrEmpty(column4))
        {
            if (column1Empty)
            {
                previousRowEmpty = true;
                allColumnEmpty = true;
                continiousEmpty = true;
            }
            else
            {
                if (allColumnEmpty)
                {
                    ShowMessage();
                    return;
                }
                continiousEmpty = false;
                previousRowEmpty = true;
            }
        }
        else
        {
            if (previousRowEmpty)
            {
                ShowMessage();
                return;
            }
            if ((!string.IsNullOrEmpty(column2) || !string.IsNullOrEmpty(column3) || !string.IsNullOrEmpty(column4)) && column1Empty)
            {
                ShowMessage();
                return;
            }
        }
    }
    if (!continiousEmpty || !allColumnEmpty)
    {
        ShowMessage();
    }
}

private bool AllRowEmptyOrHasValue()
{
    bool valid = false;
    bool isAllRowEmpty = true;
    int isAllRowHasValue = 0;
    foreach (GridViewRow row1 in Gv1Data.Rows)
    {
        TextBox txt11 = (TextBox)row1.FindControl("Section_Column1");
        TextBox txt21 = (TextBox)row1.FindControl("Section_Column2");
        TextBox txt31 = (TextBox)row1.FindControl("Section_Column3");
        TextBox txt41 = (TextBox)row1.FindControl("Section_Column4");
        if (txt11.Text != "" || txt21.Text != "" || txt31.Text != "" || txt41.Text != "")
        {
            isAllRowEmpty = false;
            if (txt11.Text != "")
            {
                isAllRowHasValue++;
            }
        }
    }
    if (isAllRowEmpty || isAllRowHasValue == Gv1Data.Rows.Count)
    {
        valid = true;
    }

    return valid;
}

private void ShowMessage()
{
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please fill all cells record')", true);
}