.NET, ASP, microsoft



ASP.NET Add Sub Header Row (Group) To GridView Control

    ''' <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

Post Comment Now


.NET DataGrid (or GridView) To Excel Utility
    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. */
    }

Post Comment Now


.NET: Convert A String Into A Compressed String

Written with .NET 2.0. Should work with 3.x as well.

using System.IO.Compression;
using System.Text;
 
protected string Compress(string text)
{
    byte[] buffer = Encoding.UTF8.GetBytes(text);
    MemoryStream ms = new MemoryStream();
    using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
    {
        zip.Write(buffer, 0, buffer.Length);
    }
 
    ms.Position = 0;
    MemoryStream outStream = new MemoryStream();
 
    byte[] compressed = new byte[ms.Length];
    ms.Read(compressed, 0, compressed.Length);
 
    byte[] gzBuffer = new byte[compressed.Length + 4];
    System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
    System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
    return Convert.ToBase64String(gzBuffer);
}
 
 
protected string Decompress(string compressedText)
{
    byte[] gzBuffer = Convert.FromBase64String(compressedText);
    using (MemoryStream ms = new MemoryStream())
    {
        int msgLength = BitConverter.ToInt32(gzBuffer, 0);
        ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
 
        byte[] buffer = new byte[msgLength];
 
        ms.Position = 0;
        using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
        {
            zip.Read(buffer, 0, buffer.Length);
        }
 
        return Encoding.UTF8.GetString(buffer);
    }
}

Post Comment Now


.net versions rant

I am fairly certain the .NET versioning (numbering release) scheme is the dumbest ever created. Seriously who in their right mind does a point release that is a extension of the previous point but not the one before that (2.0 -> 3.0)? Then 3.0 -> to 3.5? wtf? Remember 1.0 and 1.1? Hmmm. How does that fit in? Oh yea, it doesn’t.

It went like this…

1.0 to 1.1. Perfect. Basically the 1.0 framework fixed with a couple new things. That’s actually how it took place.

Then 2.0. A whole new framework. Yay! We are good at this point.

Then 3.0. Huh? Another new framework? Yay!? Wait, no, huh? It’s not a framework? It’s a extension but only for 2.0 and not related at all to 1.0 or 1.1? Oh good heavens MS.

Then 3.5 Another extension to 2.0 or actually 3.0? Ok, now I’m getting mad. What happened to 3.1, 3.2, etc.?

What’s next? 8.0 with 10.0 extentions?

Might as well number it 38.5. What’s the difference at this point? There is NO consistency anyway and the higher the number the better right?

Anyway here is the 2.0 -> 3.5 relationships…
dotnetframework35.png

Seriously, if you know what the hell MS was thinking, post a comment.


Post Comment Now


ASP.NET – Test NTFS Directory Access
    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));
    }

Post Comment Now


Blog | Contact | Gallery | Links | Sandbox | Social Networking | Weather | Web Design


Copyright © 1997-2009 KCSH. All rights reserved.