''' <summary> ''' Insert a sub header in a grid view. ''' </summary> ''' <param name="gv">The GridView to insert the sub header row into.</param> ''' <param name="gvrBeforeRow">The GridViewRow to insert the sub header before.</param> ''' <param name="sSubHeaderText">The text to render in the sub header.</param> ''' <remarks></remarks> Protected Sub insertGroupHeaderToGridView(ByRef gv As GridView, ByVal gvrBeforeRow As GridViewRow, ByVal sSubHeaderText As String) Dim visibleColumns As Integer = gv.Columns.Count Dim tbl As Table = gv.Controls(0) Dim newRowIndex As Integer = tbl.Rows.GetRowIndex(gvrBeforeRow) Dim newRow As GridViewRow = New GridViewRow(newRowIndex, newRowIndex, DataControlRowType.DataRow, DataControlRowState.Normal) newRow.Cells.Add(New TableCell()) If (visibleColumns > 1) Then newRow.Cells(0).ColumnSpan = visibleColumns newRow.Cells(0).Text = sSubHeaderText End If tbl.Controls.AddAt(newRowIndex, newRow) End Sub
protected void Button3_Click(object sender, EventArgs e) { DataSet dsExport = dataSet; DataGrid dgExport = new DataGrid(); dgExport.DataSource = dsExport; //ExportToExcel(); DataGridToExcel(dgExport, Response); } protected void DataGridToExcel(DataGrid dGridExport, HttpResponse httpResp) { httpResp.Clear(); httpResp.Charset = ""; httpResp.ContentType = "application/vnd.ms-excel"; StringWriter stringWrite = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); DataGrid dGrid = new DataGrid(); dGrid = dGridExport; dGrid.HeaderStyle.Font.Bold = true; dGrid.DataBind(); dGrid.RenderControl(htmlWrite); httpResp.Write(stringWrite.ToString()); httpResp.End(); }
Or to export a DataGrid to Excel use:
protected void LinkButton1_Click(object sender, EventArgs e) { exportToExcel(); } protected void exportToExcel() { VerifyRenderingInServerForm(GridView1); /* This is a limit in Excel prior to Office 2007 */ if (GridView1.Rows.Count > 65536) { Page.Response.Clear(); Page.Response.Write("Too many lines to export."); Page.Response.End(); } GridView1.AllowPaging = false; GridView1.AllowSorting = false; GridView1.DataBind(); Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename=FileName.xls"); Response.Charset = ""; // If you want the option to open the Excel file without saving than // comment out the line below // Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); GridView1.RenderControl(htmlWrite); Response.Write(stringWrite.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { /* Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time. */ }
protected void Page_Load(object sender, EventArgs e) { // Show admin link if they have access to the admin directory. // hyperLinkAdmin.Visible = hasAccessToDir("admin"); } protected bool hasAccessToDir(string dir) { return System.IO.Directory.Exists(MapPath(dir)); }
C# using Visual Studio 2005…
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { // How many rows when the page initially loads. static int tDefaultRow_Count = 10; // Max number of rows. static int tMaxRows = 50; // Do not modify the following. private int tUserAddedRowCount = 0; static TextBox[] TextBox_Item = new TextBox[tMaxRows]; static TextBox[] TextBox_Desc = new TextBox[tMaxRows]; static TextBox[] TextBox_Quantity = new TextBox[tMaxRows]; static TextBox[] TextBox_Price = new TextBox[tMaxRows]; static TextBox[] TextBox_Total = new TextBox[tMaxRows]; protected void Page_Load(object sender, EventArgs e) { tUserAddedRowCount = (ViewState["tUserAddedRowCount"] != null) ? int.Parse(ViewState["tUserAddedRowCount"].ToString()) : 0; //Response.Write("User Rows: " + tUserAddedRowCount + "\r\n"); // Recreate the extra rows created by the user on postback // events (required to maintain state). Add_Table_Rows(tUserAddedRowCount + tDefaultRow_Count); } protected void Button1_Click(object sender, EventArgs e) { // User clicked "add a row" button. Add_Table_Rows(1); ViewState["tUserAddedRowCount"] = ++tUserAddedRowCount; } protected void Button2_Click(object sender, EventArgs e) { // User clicked "Submit" button. for (int x = 0; x < tDefaultRow_Count + tUserAddedRowCount; x++) { if (TextBox_Item[x].Text.ToString().Length > 0) { Response.Write(TextBox_Item[x].Text.ToString()); Response.Write(TextBox_Desc[x].Text.ToString()); Response.Write(TextBox_Quantity[x].Text.ToString()); Response.Write(TextBox_Price[x].Text.ToString()); Response.Write(TextBox_Total[x].Text.ToString()); } } } private void Add_Table_Rows(int NumberOfRows) { for (int x = 0; x < NumberOfRows; x++) { TableRow myRow = new TableRow(); Table1.Rows.Add(myRow); TableCell myCell0 = new TableCell(); myRow.Cells.Add(myCell0); TextBox myTextbox0 = new TextBox(); TextBox_Item[x] = myTextbox0; myCell0.Controls.Add(TextBox_Item[x]); TableCell myCell1 = new TableCell(); myRow.Cells.Add(myCell1); TextBox myTextbox1 = new TextBox(); TextBox_Desc[x] = myTextbox1; myCell1.Controls.Add(TextBox_Desc[x]); TableCell myCell2 = new TableCell(); myRow.Cells.Add(myCell2); TextBox myTextbox2 = new TextBox(); TextBox_Quantity[x] = myTextbox2; myCell2.Controls.Add(TextBox_Quantity[x]); TableCell myCell3 = new TableCell(); myRow.Cells.Add(myCell3); TextBox myTextbox3 = new TextBox(); TextBox_Price[x] = myTextbox3; myCell3.Controls.Add(TextBox_Price[x]); TableCell myCell4 = new TableCell(); myRow.Cells.Add(myCell4); TextBox myTextbox4 = new TextBox(); TextBox_Total[x] = myTextbox4; myCell4.Controls.Add(TextBox_Total[x]); } } }
From command line
C:\WINDOWS\Microsoft.NET\Framework\[framework version]\aspnet_regiis.exe -r
After that has run, goto IIS –> Web Service Extensions –> ASP.NET [framework version] –> Enable.