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
}
Related
I'm trying add buttons into DataGridView to delete items from it. To do it, I'm trying to use DataGridViewButtonCell, because I saw that it can contain images. I wan't my button to stay on the first column of DataGridView, but I have an IList that I use as DataSource and I don't know how to add that button to the GridView in this case.
How could I do this ?
Here is my code:
private void setDataSource(){
grid.DataSource = ivDAO.findAllItemVenda(venda);
grid.ClearSelection();
defineGrid();
}
private void defineGrid() {
//header
grid.Columns["produto"].HeaderText = "Produto";
grid.Columns["valorUn"].HeaderText = "Unit.R$";
grid.Columns["quantidade"].HeaderText = "Qtd.";
grid.Columns["total"].HeaderText = "Total R$";
//hide
grid.Columns["id"].Visible = false;
grid.Columns["venda"].Visible = false;
//width
grid.Columns["produto"].Width = 235;
grid.Columns["valorUn"].Width = 80;
grid.Columns["quantidade"].Width = 50;
grid.Columns["total"].Width = 80;
//align
grid.Columns["valorUn"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
grid.Columns["quantidade"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
grid.Columns["total"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
//add button
DataGridViewButtonCell btnDelete = new DataGridViewButtonCell();
btnDelete.Value = "delete";
grid.Rows.Add(btnDelete);
}
I want this.
Done. Finally I did what I wanted.
I did.
private void setDataSource(){
//added column with image before set datasource
if (grid.Columns["Del"] == null) {
var col = new DataGridViewImageColumn();
col.HeaderText = "";
col.Name = "Del";
col.Image = TerminalControleDeVendas.Properties.Resources.trash;
grid.Columns.Add(col);
}
grid.DataSource = ivDAO.findAllItemVenda(venda);
grid.ClearSelection();
defineGrid();
}
private void defineGrid() {
//header
grid.Columns["produto"].HeaderText = "Produto";
grid.Columns["valorUn"].HeaderText = "Unit.R$";
grid.Columns["quantidade"].HeaderText = "Qtd.";
grid.Columns["total"].HeaderText = "Total R$";
//hide
grid.Columns["id"].Visible = false;
grid.Columns["venda"].Visible = false;
//width
grid.Columns["Del"].Width = 30;
grid.Columns["produto"].Width = 220;
grid.Columns["valorUn"].Width = 80;
grid.Columns["quantidade"].Width = 50;
grid.Columns["total"].Width = 80;
//align
grid.Columns["valorUn"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
grid.Columns["quantidade"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
grid.Columns["total"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
}
Finally I added the handler to delete item
private void grid_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if (this.grid.Columns[e.ColumnIndex].Name == "Del") {
DialogResult confirma = MessageBox.Show("Deseja realmente excluir o item ?", "Excluir", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (confirma == DialogResult.Yes) {
ItemVenda iv = (ItemVenda)grid.CurrentRow.DataBoundItem;
ivDAO.delete(iv);
}
}
}
The result is
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 creating a web application that requires controls to be created dynamically on the click of a button, and then add the controls to an already constructed asp:table.
The problem is that the events for the dynamic controls do not fire (ciDD_SelectedIndexChanged).
In order: the button is clicked, it sets a Session variable for the control IDs, creates the controls, then adds them to the table row. This is done OnClick, and OnInit for post back.
Is there an easy way to set the event for the dynamic controls, or am I going about this in a horrible way?
The button and it's event:
<asp:ImageButton ID="addHardware" runat="server" ImageUrl="~/Images/plus.png" Height="24" Width="24" OnClick="addHardware_Click" ImageAlign="AbsMiddle" style="margin-left:7px" Visible="false" />
protected void addHardware_Click(object sender, ImageClickEventArgs e)
{
Session["rowCount"] = (Convert.ToInt32(Session["rowCount"]) + 1);
CreateControls(Convert.ToInt32(Session["rowCount"]));
CreateRow(Convert.ToInt32(Session["rowCount"]));
}
The Control creation function and the list they are saved to:
private static List<Control> _controls = new List<Control>();
private static List<Control> HWControls
{
get { return _controls; }
}
protected void CreateControls(int i)
{
DropDownList ci = new DropDownList();
ci.AutoPostBack = true;
ci.ID = "ciDD" + i;
ci.SelectedIndexChanged += new EventHandler(this.ciDD_SelectedIndexChanged);
ci.Items.Add(new ListItem("", ""));
ci.Items.Add(new ListItem("test1", "test1"));
ci.Items.Add(new ListItem("test2", "test2"));
DropDownList dt = new DropDownList();
dt.ID = "deviceTypeDD" + i;
DropDownList m = new DropDownList();
m.ID = "modelDD" + i;
TextBox qt = new TextBox();
qt.ID = "qtTB" + i;
DropDownList dv = new DropDownList();
dv.ID = "deviceNumDD" + i;
TextBox sn = new TextBox();
sn.ID = "serialTB" + i;
HWControls.Add(ci);
HWControls.Add(dt);
HWControls.Add(m);
HWControls.Add(qt);
HWControls.Add(dv);
HWControls.Add(sn);
}
The Row creation function, and it's corresponding List:
private static List<TableRow> _rows = new List<TableRow>();
private static List<TableRow> Rows
{
get { return _rows; }
}
protected void CreateRow(int i)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
TableCell cell4 = new TableCell();
TableCell cell5 = new TableCell();
cell.Controls.Add(HWControls.Find(x => x.ID.Contains("ciDD" + i)));
cell1.Controls.Add(HWControls.Find(x => x.ID.Contains("deviceTypeDD" + i)));
cell2.Controls.Add(HWControls.Find(x => x.ID.Contains("modelDD" + i)));
cell3.Controls.Add(HWControls.Find(x => x.ID.Contains("qtTB" + i)));
cell4.Controls.Add(HWControls.Find(x => x.ID.Contains("deviceNumDD" + i)));
cell5.Controls.Add(HWControls.Find(x => x.ID.Contains("serialTB" + i)));
row.Cells.Add(cell);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);
row.Cells.Add(cell4);
row.Cells.Add(cell5);
Rows.Add(row);
hardwareTable.Rows.Add(row);
}
The OnInit to recreate the controls/rows after post back:
protected override void OnInit(EventArgs e)
{
int i = Convert.ToInt32(Session["rowCount"]);
if (i != 0)
{
for (int j = 1; j <= i; j++)
{
CreateControls(j);
CreateRow(j);
}
}
base.OnInit(e);
}
I found that creating the eventhandlers in the PreInit phase solved the issue.
protected void Page_PreInit(object sender, EventArgs e)
{
foreach (Control c in HWControls)
{
if(c.ID.Contains("ciDD"))
{
((DropDownList)c).SelectedIndexChanged += new EventHandler(this.ciDD_SelectedIndexChanged);
}
else if (c.ID.Contains("deviceTypeDD"))
{
((DropDownList)c).SelectedIndexChanged += new EventHandler(this.deviceTypeDD_SelectedIndexChanged);
}
}
}
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);
}