Hi,
I am doing the loan calculator and in that I am facing some gridview related problems. so please help me out!!!!!!!!!!
I have taken the template field in grid view like........
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EnableViewState="true">
<Columns>
<asp:BoundField HeaderText="No." DataField="No" />
<asp:BoundField HeaderText="Payment Date" DataField="PaymentDate" />
<asp:BoundField HeaderText="Beginning Balance" DataField="BeginningBalance" />
<asp:BoundField HeaderText="Scheduled Payment" DataField="ScheduledPayment" />
<%-- <asp:BoundField HeaderText="Extra Payment" DataField="ExtraPayment" /> --%>
<asp:TemplateField HeaderText="Extra Payment">
<ItemTemplate>
<asp:TextBox ID="txtExtraPayment" Text="0" EnableViewState="true" AutoPostBack="true"
runat="server" OnTextChanged="txtExtraPayment_TextChanged" DataField="ExtraPayment"
Width="75"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Total Payment" DataField="TotalPayment" />
<asp:BoundField HeaderText="Principal" DataField="principal" />
<asp:BoundField HeaderText="Interest" DataField="interest" />
<asp:BoundField HeaderText="Ending Balance" DataField="EndingBalance" />
</Columns>
</asp:GridView>
now i have created one create grid function in which i have creted first row dynamically and then another loop for second to further rows based on the calculation of the first row. so i have to do the dynamic gridview is must.
i have not used any database for this puppose.
the code is as ................
protected void createGrid()
{
loanYears = Convert.ToDecimal(txtLoanYears_BoundControl.Text.Trim());
scheduledNumberOfPayment = loanYears * 12;
txtScheduledNumberOfPayments.Text = scheduledNumberOfPayment.ToString();
beginingBal = Convert.ToDecimal(txtLoanAmount.Text.Trim());
annInterest = Convert.ToDecimal(txtAnnInterest_BoundControl.Text.Trim());
Double roi = fn_InterestRate(Convert.ToDouble(annInterest));
Double LY = Convert.ToDouble(loanYears);
Double BBal = Convert.ToDouble(beginingBal);
Double emi = fn_ScheduledMonthlyPayment(roi, LY, BBal);
scheduledMonthlyPayment = Convert.ToDecimal(emi);
txtScheduledMonthlyPayment.Text = scheduledMonthlyPayment.ToString("C");
optionalPayment = Convert.ToDecimal(0.00d);
totalPayment = fn_TotalPayment(scheduledMonthlyPayment, Convert.ToDecimal(txtOptionalPayment.Text.Trim()));
currentInterest = fn_CurrentInterest(beginingBal, annInterest);
principal = fn_Principal(totalPayment, currentInterest);
endingBal = fn_EndingBalance(beginingBal, principal);
indexNo = indexNo + 1;
if (dt.Rows.Count < 1)
{
dt.Columns.Add("No", typeof(int));
dt.Columns.Add("PaymentDate", typeof(string));
dt.Columns.Add("BeginningBalance", typeof(string));
dt.Columns.Add("ScheduledPayment", typeof(string));
dt.Columns.Add("ExtraPayment", typeof(string));
dt.Columns.Add("TotalPayment", typeof(string));
dt.Columns.Add("principal", typeof(string));
dt.Columns.Add("interest", typeof(string));
dt.Columns.Add("EndingBalance", typeof(string));
dtRow = dt.NewRow();
dtRow["No"] = "" + indexNo;
dtRow["PaymentDate"] = "" + txtStartDateOfLoan.Text;
dtRow["BeginningBalance"] = beginingBal.ToString("C");
dtRow["ScheduledPayment"] = "" + scheduledMonthlyPayment.ToString("C");
dtRow["ExtraPayment"] = "" + optionalPayment.ToString("C");
dtRow["TotalPayment"] = "" + totalPayment.ToString("C");
dtRow["principal"] = "" + principal.ToString("C");
dtRow["interest"] = "" + currentInterest.ToString("C");
dtRow["EndingBalance"] = "" + endingBal.ToString("C");
dt.Rows.Add(dtRow);
}
if (dt.Rows.Count >= 1)
{
for (int i = 1; i < 360; i++)
{
rowCount = dt.Rows.Count - 1;
o = dt.Rows[rowCount]["PaymentDate"];
date = Convert.ToDateTime(o).AddMonths(1);
string n = date.ToShortDateString();
if (date.Month == 12)
{
}
beginingBal = Convert.ToDecimal(remoFirstChar(dt.Rows[rowCount]["EndingBalance"].ToString()));
schPay = remoFirstChar(dt.Rows[rowCount]["ScheduledPayment"].ToString());
extPay = remoFirstChar(dt.Rows[rowCount]["ExtraPayment"].ToString());
totalPayment = fn_TotalPayment(Convert.ToDecimal(schPay), Convert.ToDecimal(extPay));
currentInterest = fn_CurrentInterest(beginingBal, annInterest);
principal = fn_Principal(totalPayment, currentInterest);
endingBal = fn_EndingBalance(beginingBal, principal);
indexNo = indexNo + 1;
dtRow = dt.NewRow();
dtRow["No"] = "" + indexNo;
dtRow["PaymentDate"] = "" + n;
dtRow["BeginningBalance"] = "" + beginingBal.ToString("C");
dtRow["ScheduledPayment"] = "" + scheduledMonthlyPayment.ToString("C");
dtRow["ExtraPayment"] = "" + optionalPayment.ToString("C");
dtRow["TotalPayment"] = "" + totalPayment.ToString("C");
dtRow["principal"] = "" + principal.ToString("C");
dtRow["interest"] = "" + currentInterest.ToString("C");
dtRow["EndingBalance"] = "" + endingBal.ToString("C");
dt.Rows.Add(dtRow);
}
}
GridView1.DataSource = dt;
Session["tempDt"] = dt;
GridView1.DataBind();
}
Then i am using the gridview texbox's checkedchange event to calculate the further loan amount if extra payment payed. so I create and bind the rows again here....
code..........
protected void txtExtraPayment_TextChanged(object sender, EventArgs e)
{
TextBox txtsample = (TextBox)sender;
GridViewRow gvr = (GridViewRow)txtsample.Parent.Parent;
int index = gvr.RowIndex;
dt = (DataTable)Session["tempDt"];
loanYears = Convert.ToDecimal(txtLoanYears_BoundControl.Text.Trim());
scheduledNumberOfPayment = loanYears * 12;
txtScheduledNumberOfPayments.Text = scheduledNumberOfPayment.ToString();
beginingBal = Convert.ToDecimal(txtLoanAmount.Text.Trim());
annInterest = Convert.ToDecimal(txtAnnInterest_BoundControl.Text.Trim());
Double roi = fn_InterestRate(Convert.ToDouble(annInterest));
Double LY = Convert.ToDouble(loanYears);
Double BBal = Convert.ToDouble(beginingBal);
Double emi = fn_ScheduledMonthlyPayment(roi, LY, BBal);
scheduledMonthlyPayment = Convert.ToDecimal(emi);
txtScheduledMonthlyPayment.Text = scheduledMonthlyPayment.ToString("C");
optionalPayment = Convert.ToDecimal(txtOptionalPayment.Text.ToString());
totalPayment = fn_TotalPayment(scheduledMonthlyPayment, Convert.ToDecimal(txtOptionalPayment.Text.Trim()));
currentInterest = fn_CurrentInterest(beginingBal, annInterest);
principal = fn_Principal(totalPayment, currentInterest);
endingBal = fn_EndingBalance(beginingBal, principal);
indexNo = indexNo + 1;
if (dt.Rows.Count < 1)
{
dt.Columns.Add("No", typeof(int));
dt.Columns.Add("PaymentDate", typeof(string));
dt.Columns.Add("BeginningBalance", typeof(string));
dt.Columns.Add("ScheduledPayment", typeof(string));
dt.Columns.Add("ExtraPayment", typeof(string));
dt.Columns.Add("TotalPayment", typeof(string));
dt.Columns.Add("principal", typeof(string));
dt.Columns.Add("interest", typeof(string));
dt.Columns.Add("EndingBalance", typeof(string));
dtRow = dt.NewRow();
dtRow["No"] = "" + indexNo;
dtRow["PaymentDate"] = "" + txtStartDateOfLoan.Text;
dtRow["BeginningBalance"] = beginingBal.ToString("C");
dtRow["ScheduledPayment"] = "" + scheduledMonthlyPayment.ToString("C");
dtRow["ExtraPayment"] = "" + optionalPayment.ToString("C");
dtRow["TotalPayment"] = "" + totalPayment.ToString("C");
dtRow["principal"] = "" + principal.ToString("C");
dtRow["interest"] = "" + currentInterest.ToString("C");
dtRow["EndingBalance"] = "" + endingBal.ToString("C");
dt.Rows.Add(dtRow);
}
if (dt.Rows.Count >= 1)
{
for (int i = 1; i < 360; i++)
{
rowCount = dt.Rows.Count - 1;
o = dt.Rows[rowCount]["PaymentDate"];
date = Convert.ToDateTime(o).AddMonths(1);
string n = date.ToShortDateString();
beginingBal = Convert.ToDecimal(remoFirstChar(dt.Rows[rowCount]["EndingBalance"].ToString()));
int ind = dt.Rows.Count;
schPay = remoFirstChar(dt.Rows[rowCount]["ScheduledPayment"].ToString());
if (ind == index)
{
extPay = txtsample.Text;
extPay = Convert.ToDecimal(extPay);
}
else
{
extPay = 0.00;
extPay = Convert.ToDecimal(extPay);
}
totalPayment = fn_TotalPayment(Convert.ToDecimal(schPay), Convert.ToDecimal(extPay));
currentInterest = fn_CurrentInterest(beginingBal, annInterest);
principal = fn_Principal(totalPayment, currentInterest);
endingBal = fn_EndingBalance(beginingBal, principal);
indexNo = indexNo + 1;
dtRow = dt.NewRow();
dtRow["No"] = "" + indexNo;
dtRow["PaymentDate"] = "" + n;
dtRow["BeginningBalance"] = "" + beginingBal.ToString("C");
dtRow["ScheduledPayment"] = "" + scheduledMonthlyPayment.ToString("C");
dtRow["ExtraPayment"] = "$" + extPay.ToString();
dtRow["TotalPayment"] = "" + totalPayment.ToString("C");
dtRow["principal"] = "" + principal.ToString("C");
dtRow["interest"] = "" + currentInterest.ToString("C");
dtRow["EndingBalance"] = "" + endingBal.ToString("C");
dt.Rows.Add(dtRow);
}
GridView1.DataSource = dt;
GridView1.DataBind();
TextBox src = ((TextBox)sender);
src.Text = "2000";
}
}
Now I am facing the problem is on cheked change event
1) all calculations are performing well at first time event raise but the texbox I inserted value and fire the event get cleared after the postback. since I am not able to calculate it again at second time when I raise the same event for another textbox. So please tell me how to preserve the values of gridview textbox during all the postback caused by checkedchange event.