I have a method which is executed when the page is loaded and when the page of the gridview is changed and then there is another method for onrowcreated. This method imports the data, as well setting the rowspans. The data import is working on every site but the Rowspan only gets set on the first page.
This is my C# Code:
protected void grdvProductChurn_DataBound()
{
for (int rowIndex = grdvProductChurn.Rows.Count - 2; rowIndex >= 0; rowIndex += -1)
{
GridViewRow gvRow = grdvProductChurn.Rows[rowIndex];
GridViewRow gvPreviousRow = grdvProductChurn.Rows[rowIndex + 1];
for (int cellCount = 0; cellCount <= gvRow.Cells.Count - 8; cellCount++)
{
if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text)
{
if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
{
gvRow.Cells[cellCount].RowSpan = 2;
}
else
{
gvRow.Cells[cellCount].RowSpan = gvPreviousRow.Cells[cellCount].RowSpan + 1;
}
gvPreviousRow.Cells[cellCount].Visible = false;
}
}
}
}
And this is my asp GridView:
<asp:GridView ID="grdvProductChurn" runat="server" CellPadding="4" HeaderStyle-BorderStyle="None" autopostback="true"
BorderColor="#666666" BorderStyle="Solid" AllowPaging="True" AutoGenerateColumns="false" PageSize="30" DataSourceID="DataSource_ProductChurn"
AllowSorting="True" ForeColor="#666666" CellSpacing="1" DataFormatString="{0:###,###,###,###,###}"
CaptionAlign="Left" Width="960px" HeaderStyle-HorizontalAlign="Center" HorizontalAlign="Center" CssClass="GridView2"
Height="119px" OnRowCreated="grdvProductChurn_RowCreated" onpageindexchanged="grdvProductChurn_PageIndexChanged" >
Then I also have the method for the onpageindexchanged. It gets executed when I change the page, but the rowspan doesn't work.
public void grdvProductChurn_PageIndexChanged(object sender, EventArgs e)
{
grdvProductChurn_DataBound();
}
This is my method for OnRowCreated:
protected void grdvProductChurn_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView HeaderGrid = (GridView)sender;
GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "";
HeaderCell.ColumnSpan = 2;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "Anzahl";
HeaderCell.ColumnSpan = 6;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderCell.CssClass = "headercell";
grdvProductChurn.Controls[0].Controls.AddAt(0, HeaderGridRow);
}
}
On the right it looks how it is on the first page(when I go to the second page and then back to the first page it also doesn't work) And on the left it's how it looks on the second page.
Related
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
}
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
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/
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);
}
I'm trying to add another eventHandler to RadioButton. This is the sample code (which is working):
ASP.NET:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Button" />
C#:
protected void Page_Load(object sender, EventArgs e)
{
RadioButton RB1 = new RadioButton();
RB1.ID = "1";
RB1.GroupName = "bla";
RB1.CheckedChanged += new EventHandler(CheckedChanged);
RadioButton RB2 = new RadioButton();
RB2.ID = "2";
RB2.GroupName = "bla";
RB2.CheckedChanged += new EventHandler(CheckedChanged);
PlaceHolder1.Controls.Add(RB1);
PlaceHolder1.Controls.Add(RB2);
}
protected void CheckedChanged(object sender, EventArgs e)
{
Label1.Text = ((RadioButton)sender).ID;
}
In my project I have dynamic creating of RadioButtons (the number of rows I get from database). The same adding eventHandler does not work, but if I write
MyRadioButton.Load += new EventHandler(Another_method);
The Another_method will start, but in
MyRadioButton.CheckedChanged += new EventHandler(Main_method);
the Main_method will not start if I choose one of the RadioButtons.
What is wrong?
#KevinP
This is my code:
Table tb1 = new Table();
PlaceHolder1.Controls.Add(tb1);
TableRow tr = new TableRow();
TableCell tc = new TableCell();
//adding the first row with title"
tr.Cells.Add(tc);
for (int i = 0; i < ((ArrayList)(result[0])).Count; i++)
{
tc = new TableCell();
Label example = new Label();
example.Text = ((columnsResultFromSqlClients)(i)).ToString();
tc.Controls.Add(example);
tr.Cells.Add(tc);
}
tb1.Rows.Add(tr);
//filling the table
for (int i = 0; i < result.Count; i++)
{
tr = new TableRow();
tc = new TableCell();
//adding radio button
RadioButton RB = new RadioButton();
RB.Attributes.Add("value", ((ArrayList)(result[i]))[0].ToString());
RB.GroupName = "for_selecting";
RB.ID = ((ArrayList)(result[i]))[0].ToString();
RB.CheckedChanged += new EventHandler(RB_CheckedChanged2);
//RB.AutoPostBack = true;
RB.Attributes.Add("AutoPostBack", "True");
tc.Controls.Add(RB);
tr.Cells.Add(tc);
//adding content
for (int j = 0; j < ((ArrayList)(result[i])).Count; j++)
{
tc = new TableCell();
Label example = new Label();
example.Text = ((ArrayList)(result[i]))[j].ToString();
tc.Controls.Add(example);
tr.Cells.Add(tc);
}
tb1.Rows.Add(tr);
}
If I use RB.AutoPostBack = true;, I have no time to press the button to submit my choice, cause the page will reload when i click the one of the Radio Buttons.
Also the RB_CheckedChanged2 code:
protected void RB_CheckedChanged2(object sender, EventArgs e)
{
RadioButton tempRB = (RadioButton)sender;
if (tempRB.Checked)
{
selected_id = tempRB.ID;
}
}
The select_id is a static int varible with standart value = "-1".
If I am not mistaken, with dynamic controls in ASP.Net, you need to "rewire" their events on a postback. Remember that on a postback, dynamic controls are no longer there. You have to recreate them.