Click here to Skip to main content
15,895,799 members
Articles / Web Development / ASP.NET
Article

Read Text File and Bind With Gridview Control

Rate me:
Please Sign up or sign in to vote.
3.04/5 (13 votes)
10 Aug 2006 97K   2K   27   13
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


Written By
Web Developer
Pakistan Pakistan
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.

Comments and Discussions

 
QuestionHow to fill data into a DataGrid from a text file containing StatisticsIO/TIME Output Pin
Member 1091846523-Sep-14 22:05
Member 1091846523-Sep-14 22:05 
GeneralMy vote of 1 Pin
zdlik3-Dec-09 9:33
zdlik3-Dec-09 9:33 
GeneralMy vote of 1 Pin
chetdes14-Dec-08 23:31
chetdes14-Dec-08 23:31 
GeneralRe: hi Pin
Kamal.Afridi7-Dec-06 0:23
Kamal.Afridi7-Dec-06 0:23 
GeneralRe: hi Pin
Kamal.Afridi7-Dec-06 22:30
Kamal.Afridi7-Dec-06 22:30 
You can use above example to export text file to excel as well.

Know Yourself Then Grow Yourself.

GeneralRe: hi Pin
sarithak54813-Dec-06 18:12
sarithak54813-Dec-06 18:12 
GeneralRe: hi Pin
sarithak54813-Dec-06 18:14
sarithak54813-Dec-06 18:14 
GeneralRe: hi Pin
sarithak54817-Dec-06 19:32
sarithak54817-Dec-06 19:32 
QuestionHow can I make it work in pocket pc? Pin
Pengie16-Nov-06 22:26
Pengie16-Nov-06 22:26 
AnswerRe: How can I make it work in pocket pc? Pin
Kamal.Afridi20-Nov-06 0:45
Kamal.Afridi20-Nov-06 0:45 
QuestionIn Windows forms? Pin
wing70prayer4-Oct-06 21:58
wing70prayer4-Oct-06 21:58 
AnswerRe: In Windows forms? Pin
Kamal.Afridi5-Oct-06 0:20
Kamal.Afridi5-Oct-06 0:20 
GeneralRe: In Windows forms? Pin
wing70prayer5-Oct-06 3:50
wing70prayer5-Oct-06 3:50 

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

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