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);
}
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
My question i asked for "Randomly" adding controls in spefic
Table Cells. e.g.
if i call funtion CreateTable() , it should create tables
with vary number of rows and columns, and then for example if i
want a text box in Cell(0,1), a dropdown in cell(2,1) then again a text
area control in Cell(5,1) etc etc.( u getting my point, i am not
putting one type of control), how can i code that.The control state should be saved in the table.And whenever i open particylar webform it show shos all the control which i have created before
private void CreateTable(short noOfRows)
{
PlaceHolder p1 = new PlaceHolder();
Table table1 = new Table();
table1.BorderWidth = 1;
table1.BorderStyle = BorderStyle.Solid;
TableRow[] rows = new TableRow[noOfRows];
for (int i = 0; i < rows.Length; i++)
{
rows[i] = new TableRow();
}
table1.Rows.AddRange(rows);
TextBox t1 = new TextBox();
t1.Text = "Hello";
t1.EnableViewState = true;
TextBox t2 = new TextBox();
t2.Text = "World";
t2.EnableViewState = true;
Button btnOk = new Button();
btnOk.EnableViewState = true;
btnOk.Text = "OK";
Button btnCancel = new Button();
btnCancel.EnableViewState = true;
btnCancel.Text = "Cancel";
TableCell cell1=new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
TableCell cell4 = new TableCell();
cell1.Controls.Add(t1);
cell2.Controls.Add(t2);
cell3.Controls.Add(btnOk);
cell4.Controls.Add(btnCancel);
table1.Rows[0].Cells.AddAt(0, cell1);
table1.Rows[0].Cells.AddAt(1, cell2);
table1.Rows[1].Cells.AddAt(0, cell3);
table1.Rows[1].Cells.AddAt(1, cell4);
p1.Controls.Add(table1);
form1.Controls.Add(p1);
}
I have a gridview. I have created two columns dynamically in the Row_DataBound event of the gridview.
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell cell1 = new TableCell();
cell1.Width = 100;
e.Row.Cells.Add(cell1);
TableCell cell2 = new TableCell();
cell2.Width = 100;
e.Row.Cells.Add(cell2);
}
else
{
TableCell cell1 = new TableCell();
cell1.Width = 100;
cell1.Text = "<span style='font-weight:bold'>Total Punches";
e.Row.Cells.Add(cell1);
TableCell cell2 = new TableCell();
cell2.Width = 110;
cell2.Text = "<span style='font-weight:bold'>Hours Worked";
e.Row.Cells.Add(cell2);
}
I want to export the whole grid to Excel sheet. When I click the Export button, the last two columns which I created on Row_DataBound disappear in the excel sheet.
Can anyone help me solving this.
Thanks in Advance.
Try to move the logic to RowCreated event.
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);
}