when i am adding rows into gridview then debit and credit column total is not getting display ,i also applied another condition on textbox of gridview (txtdebit and txtcredit) ,that when i input value in debit ,then credit textbox enable will be false, and
same for credit textbox
As far as I can tell there is no code that saves the new values. It is up to you to write this code.
I strongly recommend dropping the ViewState cache and writing directly to the table. There are a couple of reasons for approach. As a general rule of thumb, you should never cache data that changes. Cache is only efficient for Also, you are double caching
the data source in ViewState. The Gridview also stores data in ViewState.
When you have a design like yours, it is far more efficient and cleaner to save the record directly in the database and return a new result set.
I created an example using the code you provided, and the problem you mentioned-the total is not displayed. I think this is caused by the postback generated by the TextBox. At the moment of post back, you should see the total data displayed in the label,
but it just flickers and disappears.
Have you tried other ways to store total data? For example, use Session.
I made some changes to the code, I don’t know if it meets your expectations:
protected void gvtrans_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtcrcode = (TextBox)e.Row.FindControl("txtcodedr");
DropDownList ddlContnam = (DropDownList)e.Row.FindControl("ddlfourdr");
SqlCommand cmd = new SqlCommand("select * from tbl_Account_L_Four ", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
ddlContnam.DataSource = dt;
ddlContnam.DataTextField = "Level_Four_Name";
ddlContnam.DataValueField = "Level_Four_ID";
ddlContnam.DataBind();
ddlContnam.Items.Insert(0, new ListItem("--Select Debit--", "0"));
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
Label label = e.Row.FindControl("lblTotal") as Label;
label.Text = Session["DebitTotal"] == null ? "" : Session["DebitTotal"].ToString();
Label label2 = e.Row.FindControl("lblTotal2") as Label;
label2.Text = Session["CreditTotal"] == null ? "" : Session["CreditTotal"].ToString();
}
}
protected void txtDebit_TextChanged(object sender, EventArgs e)
{
decimal total = 0;
foreach (GridViewRow row in gvtrans.Rows)
{
TextBox Debits = row.FindControl("txtDebit") as TextBox;
TextBox Credits = row.FindControl("txtCredit") as TextBox;
DropDownList ddl = row.FindControl("ddltype") as DropDownList;
ddl.ClearSelection();
if (Debits.Text != "")
{
ddl.Items.FindByText("Debit").Selected = true;
Credits.Enabled = false;
}
total += Convert.ToDecimal(Debits.Text==""?"0": Debits.Text);
}
Session["DebitTotal"] = total;
(gvtrans.FooterRow.FindControl("lblTotal") as Label).Text = total.ToString("N2");
}
protected void txtCredit_TextChanged(object sender, EventArgs e)
{
decimal total = 0;
foreach (GridViewRow row in gvtrans.Rows)
{
TextBox Debits = row.FindControl("txtDebit") as TextBox;
TextBox Credits = row.FindControl("txtCredit") as TextBox;
DropDownList ddl = row.FindControl("ddltype") as DropDownList;
ddl.ClearSelection();
if (Credits.Text != "")
{
ddl.Items.FindByText("Credit").Selected = true;
Debits.Enabled = false;
}
total += Convert.ToDecimal(Credits.Text==""?"0": Credits.Text);
}
Session["CreditTotal"] = total;
(gvtrans.FooterRow.FindControl("lblTotal2") as Label).Text = total.ToString("N2");
}
Result:
Hope this can help you.
Best regards,
Xudong Peng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
116 Points
274 Posts
Column (Debit and Credit ) total of footer is not displaying ,when adding row in gridview?
Aug 03, 2020 11:12 AM|akhterr|LINK
when i am adding rows into gridview then debit and credit column total is not getting display ,i also applied another condition on textbox of gridview (txtdebit and txtcredit) ,that when i input value in debit ,then credit textbox enable will be false, and same for credit textbox
below is my html
C#
All-Star
53081 Points
23655 Posts
Re: Column (Debit and Credit ) total of footer is not displaying ,when adding row in gridview?
Aug 03, 2020 11:56 AM|mgebhard|LINK
As far as I can tell there is no code that saves the new values. It is up to you to write this code.
I strongly recommend dropping the ViewState cache and writing directly to the table. There are a couple of reasons for approach. As a general rule of thumb, you should never cache data that changes. Cache is only efficient for Also, you are double caching the data source in ViewState. The Gridview also stores data in ViewState.
When you have a design like yours, it is far more efficient and cleaner to save the record directly in the database and return a new result set.
Member
116 Points
274 Posts
Re: Column (Debit and Credit ) total of footer is not displaying ,when adding row in gridview?
Aug 03, 2020 11:59 AM|akhterr|LINK
Hi mgebhard,
can you mentioned or correct my code ,as you know that i am not up to your level of thinking.
Contributor
2110 Points
674 Posts
Re: Column (Debit and Credit ) total of footer is not displaying ,when adding row in gridview?
Aug 14, 2020 07:03 AM|XuDong Peng|LINK
Hi akhterr,
I created an example using the code you provided, and the problem you mentioned-the total is not displayed. I think this is caused by the postback generated by the TextBox. At the moment of post back, you should see the total data displayed in the label, but it just flickers and disappears.
Have you tried other ways to store total data? For example, use Session.
I made some changes to the code, I don’t know if it meets your expectations:
Result:
Hope this can help you.
Best regards,
Xudong Peng