ASP.NET Webforms Gridview Footer - c#

In ASP.NET webforms, is there a way to configure the FooterTemplate of the GridView to allow for overflow in the footer, but to also retain the above columns width like below? As it is now, any footer overflow also stretches the cells above it.

You can do it using gridview_rowcreated() event.
Just in the event allow rowspan and column with desired column and row
Modify it with your requirement.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
GridView FooterGrid = (GridView)sender;
GridViewRow FooterRow = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
TableCell Cell_Footer = new TableCell();
Cell_Footer.Text =""; // As per your requirement
Cell_Footer.HorizontalAlign = HorizontalAlign.Center;
Cell_Footer.ColumnSpan = 2;
HeaderRow.Cells.Add(Cell_Footer);
Cell_Footer = new TableCell();
Cell_Footer.Text = ""; // As per your requirement
Cell_Footer.HorizontalAlign = HorizontalAlign.Center;
Cell_Footer.ColumnSpan = 1;
Cell_Footer.RowSpan = 2;
FooterRow.Cells.Add(Cell_Footer);
Cell_Footer = new TableCell();
Cell_Footer.Text = ""; // as per your requiremnt
Cell_Footer.HorizontalAlign = HorizontalAlign.Center;
Cell_Footer.ColumnSpan = 3;
FooterRow.Cells.Add(Cell_Footer);
GridView1.Controls[0].Controls.AddAt(0, FooterRow);
}
}
If want more explanation go through the link
http://codedisplay.com/merge-merging-or-split-spliting-gridview-header-row-or-columns-in-asp-net-c-vb-net/

Related

Gridview Footer Calcucation

I have 5 GridViews on a page. All of them show different table data, and show calculation of totals in footer.
But I need calculate all data from all GridViews and show it in a Label.
[]
use OnRow Created..
Declare variables at the top
double Counter;
double GrandTotalCounter;
this is sample code refer and make change in yours..
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
bool IsGrandTotalRowNeedtoAdd = false;
if ((strPreviousRowID != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "city_name") == null))
{
IsGrandTotalRowNeedtoAdd = true;
intTotalIndex = 0;
}
if (IsGrandTotalRowNeedtoAdd)
{
GridView grdViewProducts = (GridView)sender;
GridViewRow GrandTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
GrandTotalRow.Font.Bold = true;
GrandTotalRow.BackColor = System.Drawing.Color.LightPink;
GrandTotalRow.ForeColor = System.Drawing.Color.White;
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "Grand Total";
HeaderCell.HorizontalAlign = HorizontalAlign.Left;
HeaderCell.ColumnSpan = 3;
HeaderCell.Font.Bold = true;
HeaderCell.ForeColor = System.Drawing.Color.Red;
HeaderCell.Font.Size = 10;
GrandTotalRow.Cells.Add(HeaderCell);
// you can use how many fields you want to calculate
HeaderCell = new TableCell();
HeaderCell.Text = string.Format("{0:0.00}", GrandTotalCounter);
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Font.Bold = true;
HeaderCell.ForeColor = System.Drawing.Color.Red;
HeaderCell.Font.Size = 10;
GrandTotalRow.Cells.Add(HeaderCell);
//////////////
GrandTotalRow.Cells.Add(HeaderCell);
grdViewProducts.Controls[0].Controls.AddAt(e.Row.RowIndex,
GrandTotalRow);
}}
on RowDataBound
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
strtotalID = DataBinder.Eval(e.Row.DataItem, "city_name").ToString();
double TtCounter = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "CounterAmt").ToString());
TotalCounter += TtCounter;
GrandTotalCounter += TCounter;
/// Add how much you want
}

Add Grand Total To Footer Of Grid View

I am using the below syntax to manipulate the RowDataBound and the RowCreated methods of a gridview so that each time the employeeid is changed to add a total row. Very basic for me, which is good. Well I need to take it a step further now and in the footer add a SUM of all the total rows.
How can I achieve this? Below is what I am using to add the Totals each time the employeeid is changed:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView1.Columns[14].Visible = false;
if (e.Row.RowType == DataControlRowType.DataRow)
{
employeeid = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "employeeid").ToString());
decimal tmpfield1 = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field1").ToString());
decimal tmpfield2 = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field2").ToString());
decimal tmpfield3= Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field3").ToString());
decimal tmpfield4 = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field4”).ToString());
decimal tmpfield5 = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field5").ToString());
decimal tmpfield6 = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field6").ToString());
qtyfield1 += tmpfield1;
qtyfield2 += tmpfield2;
qtyfield3 += tmpfield3
qtyfield4+= tmpfield4;
qtyfield5+= tmpfield5;
qtyfield6 += tmpfield6;
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
bool newRow = false;
if ((employeeid > 0) && (DataBinder.Eval(e.Row.DataItem, "employeeid") != null))
{
if (employeeid != Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "employeeid").ToString()))
newRow = true;
}
if ((employeeid > 0) && (DataBinder.Eval(e.Row.DataItem, "employeeid") == null))
{
newRow = true;
rowIndex = 0;
}
if (newRow)
{
wh = “11”;
GridView GridView1 = (GridView)sender;
GridViewRow NewTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
NewTotalRow.Font.Bold = true;
NewTotalRow.BackColor = System.Drawing.Color.Gray;
NewTotalRow.ForeColor = System.Drawing.Color.White;
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "Total";
HeaderCell.HorizontalAlign = HorizontalAlign.Left;
HeaderCell.ColumnSpan = 4;
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Text = qtyfield1.ToString();
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Text = qtyfield2.ToString();
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Text = qtyfield3.ToString();
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.Text = qtyfield4.ToString();
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.Text = qtyfield5.ToString();
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
decimal grandtotalfield6
try { grandtotalfield6 = Convert.ToDecimal(qtyfield6) / Convert.ToDecimal(wh); }
catch { grandtotalfield6 = 0.00M; }
HeaderCell.Text = grandtotalfield6.ToString("P", CultureInfo.InvariantCulture);
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Text = "";
HeaderCell.ColumnSpan = 4;
NewTotalRow.Cells.Add(HeaderCell);
GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + rowIndex, NewTotalRow);
rowIndex++;
qtyTotal = 0;
qtyfield1 = 0;
qtyfield2 = 0;
qtyfield3 = 0;
qtyfield4 = 0;
qtyfield5 = 0;
qtyfield6 = 0;
}
}
EDIT
Upon further thought and analysis, I think I can capture the grand total by doing this, but how would I then turn it around and be able to write this info to the footer?
sumf1 += qtyfield1;
sumf2 += qtyfield2;
sumf3 += qtyfield3;
sumf4 += qtyfield4;
sumf5 += qtyfield5;
sumf6 += qtyfield6;
You are almost there. In your RowDataBound code add in the check to determine if you are looking at a DataRow or a Footer How you would do such is like so (of course add in for all of the quantities you want to show, but should be a quick text change, as syntax will remain the same)
This is your C# code behind
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Process like you do in your method
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblSum1 = (Label)e.Row.FindControl("lblTotalqty");
lblSum1.Text = Sum1.ToString();
}
}
Modify your HTML to add in a footertemplate and populate like so
<FooterTemplate>
<div style="text-align: right;">
<asp:Label ID="lblSum1" runat="server" />
</div>
</FooterTemplate>
Why don't you use the option GridView1.Rows.Count ?
GridView1.FooterRow.Cells[2].Text = Convert.ToString(GridView1.Rows.Count);
If i understand you good he start with x rows "Save that amount in a hidden field"
Then every time a row is added subtract it from the total rows and the result will be the total rows added for that person , then set that value to the footer cell

Read Gridview exact rows after adding a dynamically added header?

I am adding a table header row dynamically as shown below. It is rendered correctly.
So now I have 2 headers.
protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell1 = new TableHeaderCell();
cell1.ColumnSpan = 1; //e.Row.Cells.Count;
cell1.Text = "Expected";
TableCell cell2 = new TableCell();
cell2.ColumnSpan = 2;
cell2.Text = "One";
TableCell cell3 = new TableCell();
cell3.ColumnSpan = 2;
cell3.Text = "Two";
TableCell cell4 = new TableCell();
cell4.ColumnSpan = 2;
cell4.Text = "Three";
newHeaderRow.Cells.Add(cell1);
newHeaderRow.Cells.Add(cell2);
newHeaderRow.Cells.Add(cell3);
newHeaderRow.Cells.Add(cell4);
((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
}
}
If my gridview is having 3 rows, and loop through gridview it will read only 2 row.
It is skiping the last row.
Any thoughts
I solved this. write this code on RowCreated event.
I wrote this code on RowDataBound so created header row was not consistent.
protected void gvCustomers_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell1 = new TableHeaderCell();
cell1.ColumnSpan = 1; //e.Row.Cells.Count;
cell1.Text = "Expected";
TableCell cell2 = new TableCell();
cell2.ColumnSpan = 2;
cell2.Text = "One";
TableCell cell3 = new TableCell();
cell3.ColumnSpan = 2;
cell3.Text = "Two";
TableCell cell4 = new TableCell();
cell4.ColumnSpan = 2;
cell4.Text = "Three";
newHeaderRow.Cells.Add(cell1);
newHeaderRow.Cells.Add(cell2);
newHeaderRow.Cells.Add(cell3);
newHeaderRow.Cells.Add(cell4);
((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
}

GridView RowDataBound, Deleted cells reappear

I am working on a .Net Project and I have some problems with GridView.
I have several buttons in my page. When I click the button which binds my GridView everything is OK, but after that, when I click on some other button there appears one more cell, which is empty, at the end of Rows which contains the text "Monday".
Can someone tell me what the problem is?
Thanks
This is my code.
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[0].Text.Contains("Monday"))
{
e.Row.BackColor = Color.Gray;
e.Row.ForeColor = Color.White;
e.Row.Cells[0].ColumnSpan = 2;
e.Row.Cells.Remove(e.Row.Cells[1]);
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Center;
}
else
{
e.Row.Cells[0].Width = 45;
e.Row.Cells[1].Width = 400;
e.Row.Cells[2].Width = 40;
e.Row.Cells[3].Width = 40;
e.Row.Cells[4].Width = 40;
e.Row.Cells[5].Width = 40;
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Center;
}
}
Reproduced the issue. It appears the gridview adds cells in order to have the same number as columns. The only fix I could find was adding the following code in the Page_Load event (you can also add it to any button method - but that's not practical):
foreach (GridViewRow gridRow in GridView1.Rows) {
if (gridRow.Cells[0].Text.Contains("Monday") && gridRow.Cells[0].ColumnSpan == 2 && gridRow.Cells.Count == 6)
gridRow.Cells.RemoveAt(5);
}
Another solution (with jquery) I found googling but have not tested is here:
http://forums.asp.net/t/1413833.aspx/1
Hope it helps!

Read Gridview header value of a dynamically added header

I am adding a table header row dynamically as shown below. It is rendered correctly. Now I need to read the first cell value of the newly added header row in a button click event. How can we read the header value?
I cannot use gvCustomers.Rows since it will not take header row.
I cannot use gvCustomers.HeaderRow.Cells[0].Text; also since there are two header rows
CODE
protected void gvCustomers_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell1 = new TableHeaderCell();
cell1.ColumnSpan = 1; //e.Row.Cells.Count;
cell1.Text = "Expected";
TableCell cell2 = new TableCell();
cell2.ColumnSpan = 2;
cell2.Text = "One";
TableCell cell3 = new TableCell();
cell3.ColumnSpan = 2;
cell3.Text = "Two";
TableCell cell4 = new TableCell();
cell4.ColumnSpan = 2;
cell4.Text = "Three";
newHeaderRow.Cells.Add(cell1);
newHeaderRow.Cells.Add(cell2);
newHeaderRow.Cells.Add(cell3);
newHeaderRow.Cells.Add(cell4);
((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
}
}
protected void Btn_Click(object sender, EventArgs args)
{
}
I am using the following approach. Any improvement suggestions?
int current = 0;
int headerCount = grdTransactions.HeaderRow.Cells.Count;
for (current = 0; current < headerCount; current++)
{
TableHeaderCell cell = new TableHeaderCell();
cell.Text = grdTransactions.HeaderRow.Cells[current].Text;
originalHeaderRow.Cells.Add(cell);
}

Categories