Please refer this
HTML
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkBxSelect" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="SubjetcName" HeaderText="Name" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)),
new DataColumn("SubjetcName", typeof(string))});
dt.Rows.Add(1, "Medical Record");
dt.Rows.Add(2, "ProgrammingLang");
dt.Rows.Add(3, "Nursing");
dt.Rows.Add(4, "Nursing");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
bool b = false;
GridViewRow row = (sender as CheckBox).NamingContainer as GridViewRow;
if ((row.FindControl("chkBxSelect") as CheckBox).Checked)
{
if (ViewState["PreviousValue"] != null && ViewState["PreviousRow"] != null)
{
if (ViewState["PreviousValue"].ToString() != row.Cells[2].Text)
{
foreach (GridViewRow gvRow in this.GridView1.Rows)
{
if (ViewState["PreviousValue"].ToString() == gvRow.Cells[2].Text && Convert.ToInt32(ViewState["PreviousRow"]) != gvRow.RowIndex && !(gvRow.FindControl("chkBxSelect") as CheckBox).Checked)
{
b = true;
}
}
if (b)
{
string message = "Please Select the same subject";
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "')", true);
}
else
{
// If you dont want to show this message you can hide it.
string message = "No more same subject found";
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "')", true);
}
}
}
string checkedSubject = row.Cells[2].Text;
ViewState["PreviousValue"] = checkedSubject;
ViewState["PreviousRow"] = row.RowIndex;
}
}