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