protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
DataTable dt = new DataTable();
try
{
}
catch(Exception ex)
{
}
using (SqlConnection conn = new SqlConnection(strConnString))
{
int selectedRow = e.RowIndex;
GridViewRow theRow = GridView1.Rows[selectedRow];
string str = ("Delete TDLoanRate where Id=@id");
SqlCommand insert = new SqlCommand(str, con);
insert.Parameters.AddWithValue("@id", Convert.ToInt32(theRow.Cells[0].Text));
using (SqlDataAdapter sda = new SqlDataAdapter())
{
insert.Connection = con;
con.Open();
sda.SelectCommand = insert;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Open();
con.Close();
}
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="371px" PageSize="5" AllowSorting="True" onsorting="GridView1_Sorting" OnRowDeleting="OnRowDeleting">
<Columns>
<asp:BoundField HtmlEncode="false" DataField="Id" HeaderText='<img src="Images/updown.png"/>Id' SortExpression="Id" />
<asp:BoundField DataField="LoanType" HeaderText="LoanType" SortExpression="LoanType" />
<asp:BoundField DataField="LoanTerm" HeaderText="Term(Years)" SortExpression="LoanTerm" />
<asp:BoundField DataField="LoanRate" HeaderText="LoanRate" SortExpression="LoanRate" />
<asp:BoundField DataField="LoanEffectFrom" HeaderText="Loan Effect From" SortExpression="LoanEffectFrom" />
<asp:BoundField DataField="LoanMaturity" HeaderText="Loan Maturity Date" SortExpression="LoanMaturity" />
<asp:CommandField HeaderText="Edit" SelectText="Edit" ShowSelectButton="True" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView2_SelectedIndexChanged" OnRowEditing=" GridView2_RowEditing" OnRowDataBound="GridView2_RowDataBound" >
<Columns>
<asp:BoundField DataField="id" HeaderText="Id" SortExpression="id" />
<asp:BoundField DataField="principal" HeaderText="Principal" SortExpression="principal" />
<asp:BoundField DataField="LoanTerm" HeaderText="Loan Term" SortExpression="LoanTerm" />
<asp:BoundField DataField="LoanEffectFrom" HeaderText="Loan Start" SortExpression="LoanEffectFrom" />
<asp:BoundField DataField="LoanInterest" HeaderText="Interest Payable" SortExpression="LoanInterest" />
<asp:BoundField DataField="LoanMaturity" HeaderText="Loan End" SortExpression="LoanMaturity" />
<asp:BoundField DataField="LoanMaturedAmt" HeaderText="Total Amount " SortExpression="LoanMaturedAmt" />
<asp:BoundField DataField="LoanRate" HeaderText="Rate" SortExpression="LoanRate" />
<asp:BoundField DataField="Loanloantype" HeaderText="Loan Type" SortExpression="Loanloantype" />
<asp:BoundField DataField="Loanmonthlyp" HeaderText="Monthly Payment" SortExpression="Loanmonthlyp" />
<asp:BoundField DataField="apply" HeaderText="Apply" SortExpression="apply" NullDisplayText="Not Seen" />
<asp:CommandField HeaderText="Check" SelectText="Check" ShowHeader="True" ShowSelectButton="True" />
</Columns>
</asp:GridView>
I want to be able to delete a row in gridview1 if there are no rows in gridview2 which has exactly the same values as gridview1
What should I put in onRowDeleting?