Click here to Skip to main content
Licence 
First Posted 10 Aug 2006
Views 57,228
Bookmarked 27 times

Read Text File and Bind With Gridview Control

By | 10 Aug 2006 | Article
An article on read text file line by line and split with some delimiter and bind with Gridview control

Introduction

An article on read text file line by line and split with some delimiter and
bind with Gridview control.

Sample screenshot

Using the code

First we need to create DataTable and define its columns.

private DataTable CreateTable()
    { 
          try
        {
            DataTable table = new DataTable();
            // Declare DataColumn and DataRow variables.
            DataColumn column;
            // Create new DataColumn, set DataType, ColumnName
            // and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Type";
            table.Columns.Add(column);
            // Create second column.
            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "Time";
            table.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Source";
            table.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Description";
            table.Columns.Add(column);
            return table;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

Next Read Text file line by line and each line split with "[" delimiter. Then
populate DataTable with split data.Paste following code on page load event.

        if (!IsPostBack)
        {
            string OpenPath,contents;
            int tabSize = 4;
            string[] arInfo;            
            string line;
            //Create new DataTable.            
            DataTable table = CreateTable();
            DataRow row;
            try
            {            
                    OpenPath = Server.MapPath(".") + @"\My File.log";
                    string FILENAME = OpenPath;
                    //Get a StreamReader class that can be used to read the file
                    StreamReader objStreamReader;
                    objStreamReader = File.OpenText(FILENAME);                    
                    while ((line = objStreamReader.ReadLine()) != null)
                    {
                        contents = line.Replace(("").PadRight(tabSize, ' '), "\t");
                        // define which character is seperating fields
                        char[] textdelimiter = { ']' };
                        arInfo = contents.Split(textdelimiter);
                        for (int i = 0; i <= arInfo.Length; i++)
                        {
                            row = table.NewRow();
                            if (i < arInfo.Length)
                                row["Type"] = arInfo[i].ToString().Replace("[", " ");
                            if (i + 1 < arInfo.Length)
                                row["Source"] = arInfo[i + 1].ToString().Replace("[", " ");
                            if (i + 2 < arInfo.Length)
                                row["Time"] = arInfo[i + 2].ToString().Substring(1);
                            if (i + 3 < arInfo.Length)
                            {
                                row["Description"] = arInfo[i + 3].ToString().Replace("[", " ");
                                table.Rows.Add(row);
                            }
                            i = i + 2;
                        }
                    }
                    objStreamReader.Close();            
                // Set to DataGrid.DataSource property to the table.
                    GridView1.DataSource = table;
                    GridView1.DataBind();                
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }  


 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Kamal.Afridi

Web Developer

Pakistan Pakistan

Member

Kamal Khan works as Software Engineer for about 3 years.In these days i am working on BPM.NET.
 
BPM.NET 2007 is BPM Workflow software that offers workflow automation and business process modeling capabilities along with full-fledged business activity monitoring (BAM) functions.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberzdlik9:33 3 Dec '09  
GeneralMy vote of 1 Pinmemberchetdes23:31 14 Dec '08  
GeneralRe: hi PinmemberKamalJKhan0:23 7 Dec '06  
GeneralRe: hi PinmemberKamalJKhan22:30 7 Dec '06  
GeneralRe: hi Pinmembersarithak54818:12 13 Dec '06  
GeneralRe: hi Pinmembersarithak54818:14 13 Dec '06  
GeneralRe: hi Pinmembersarithak54819:32 17 Dec '06  
QuestionHow can I make it work in pocket pc? PinmemberPengie22:26 16 Nov '06  
AnswerRe: How can I make it work in pocket pc? PinmemberKamalJKhan0:45 20 Nov '06  
QuestionIn Windows forms? Pinmemberwing70prayer21:58 4 Oct '06  
AnswerRe: In Windows forms? PinmemberKamalJKhan0:20 5 Oct '06  
GeneralRe: In Windows forms? Pinmemberwing70prayer3:50 5 Oct '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 10 Aug 2006
Article Copyright 2006 by Kamal.Afridi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid