locked
show alert message if two records are same in Gridview RRS feed

  • Question

  • User2033107836 posted

    Hello

    My C# code button click

      protected void Add_Data_ToGridview_Click(object sender, EventArgs e)
        {
    
            DataTable dt = new DataTable();
            dt = (DataTable)ViewState["Data"];
            DataRow dr = dt.NewRow();
            dr["LN"] = DropDownList1.Text;
            dr["Sel"] = DropDownList2.Text;
            dr["SR"] = DropDownList3.Text;
            dt.Rows.Add(dr);
    
    //Now bind the datatable to the gridview
    
            GridView1.DataSource = dt;
            GridView1.DataBind();
    
            ViewState["Data"] = dt;
        }

    My giridview1 show data like this on button click

    Field1	Filed2	Field3
    A	B	0.5
    D	0.5	C
    D	0.5	C

    Now on on button click i want to show alert message if data same  example alert message "Two row has same data" 

    then after automtically remove one row from Gridview

    How to do using asp.net 2.0 C#

    Saturday, November 4, 2017 8:38 AM

Answers

  • User-707554951 posted

    Hi asp.ambur,

    Working sample as below:

     <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            <asp:Button ID="Button1" runat="server" Text="Button"  OnClick="Button1_Click" />
            <asp:DropDownList ID="DropDownList1" runat="server"  AutoPostBack="true">
                <asp:ListItem Value="0" Text="A"></asp:ListItem>
                 <asp:ListItem Value="1" Text="D"></asp:ListItem>
            </asp:DropDownList>
              <asp:DropDownList ID="DropDownList2" runat="server"  AutoPostBack="true">
                <asp:ListItem Value="0" Text="B" ></asp:ListItem>
                 <asp:ListItem Value="1" Text="0.5"></asp:ListItem>
            </asp:DropDownList>
              <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true">
                <asp:ListItem Value="0" Text="0.5"> </asp:ListItem>
                 <asp:ListItem Value="1" Text="C"></asp:ListItem>
            </asp:DropDownList>

    Codebehind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[] { new DataColumn("LN",typeof (string)),
                    new DataColumn("Sel", typeof(string)),
                    new DataColumn("SR", typeof(string)), });
                    ViewState["Data"] = dt;
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                if (ViewState["Data"] != null)
                {
                    dt = (DataTable)ViewState["Data"];
                }
                string ln = DropDownList1.SelectedItem.Text;
                string Sel = DropDownList2.SelectedItem.Text;
                string SR = DropDownList3.SelectedItem.Text;
                DataRow dr = dt.NewRow();
                dr["LN"] = ln;
                dr["Sel"] = Sel;
                dr["SR"] = SR;
                var flag = false;
                foreach (DataRow row in dt.Rows)
                {
                    if (row["LN"].ToString() == ln)
                    {
                        if (row["Sel"].ToString() == Sel)
                        {
                            if (row["SR"].ToString() == SR)
                            {
                                flag = true;
    
                            }
                        }
                    }
                }
                if (flag == true)
                {
    
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "MessageThenRedirect", "alert('Two row has same data')", true);
                }
                else
                {
                    dt.Rows.Add(dr); ;
                }
                GridView1.DataSource = dt;
                GridView1.DataBind();
    
                ViewState["Data"] = dt;
            }

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 6, 2017 8:40 AM