Dynamically added cells disappear while exporting to Excel - c#

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.

Related

Remove top border of GridViewRow in c#

I am adding row controls in rowcreated event of gridview and top border line needs to be removed.
GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = strTotalRecords;
HeaderCell.HorizontalAlign = HorizontalAlign.Left;
HeaderCell.BorderStyle = BorderStyle.None;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderGridRow.BackColor = Color.White;
gvGrid.Controls[0].Controls.AddAt(totalRowIndex, HeaderGridRow);
I am using the below code to remove border and it doesn't work.
HeaderCell.Attributes.CssStyle.Add("border-top", "none");
HeaderCell.Attributes.CssStyle.Add("border-top-style", "none");
HeaderGridRow.Attributes.CssStyle.Add("border-top", "none");
HeaderGridRow.Attributes.CssStyle.Add("border-top-style", "none");
Please suggest me how this can be handled.
add a css class to your grid and add style to that css class like below.
.gridview
{
border-top:none !important;
}

create and add controls dynamically to dynamically created Table Control

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);
}

ASP.NET Webforms Gridview Footer

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/

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);
}

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