Hi All!
I want to copy a text file from another client system and copy it on our own system. In C drive I copy a text file but I unable to show it on the Datagrid. Please help me with full code. I am using C# windows application.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Dorababu MekaPosted Jun 14, 2011, 6:54 AM
Santosh KumarPosted Jun 14, 2011, 6:48 AM
Dorababu MekaPosted Jun 10, 2011, 6:20 AM
http://www.c-sharpcorner.com/UploadFile/Dorababu742/7452/?ArticleID=bddaae7d-d2c8-4393-9891-1d8411fc3abe
Mukesh KumarPosted Jun 10, 2011, 6:00 AM
using System;
using System.Data;
using System.IO;
namespace TestTextToDataSet
{
public class TextToDataSet
{
public TextToDataSet()
{ }
///
/// Converts a given delimited file into a dataset.
/// Assumes that the first line
/// of the text file contains the column names.
///
/// The name of the file to open
/// The name of the
/// Table to be made within the DataSet returned
/// The string to delimit by
///
public static DataSet Convert(string File,
string TableName, string delimiter)
{
//The DataSet to Return
DataSet result = new DataSet();
//Open the file in a stream reader.
StreamReader s = new StreamReader(File);
//Split the first line into the columns
string[] columns = s.ReadLine().Split(delimiter.ToCharArray());
//Add the new DataTable to the RecordSet
result.Tables.Add(TableName);
//Cycle the colums, adding those that don't exist yet
//and sequencing the one that do.
foreach (string col in columns)
{
bool added = false;
string next = "";
int i = 0;
while (!added)
{
//Build the column name and remove any unwanted characters.
string columnname = col + next;
columnname = columnname.Replace("#", "");
columnname = columnname.Replace("'", "");
columnname = columnname.Replace("&", "");
//See if the column already exists
if (!result.Tables[TableName].Columns.Contains(columnname))
{
//if it doesn't then we add it here and mark it as added
result.Tables[TableName].Columns.Add(columnname);
added = true;
}
else
{
//if it did exist then we increment the sequencer and try again.
i++;
next = "_" + i.ToString();
}
}
}
//Read the rest of the data in the file.
string AllData = s.ReadToEnd();
//Split off each row at the Carriage Return/Line Feed
//Default line ending in most windows exports.
//You may have to edit this to match your particular file.
//This will work for Excel, Access, etc. default exports.
string[] rows = AllData.Split("\r\n".ToCharArray());
//Now add each row to the DataSet
foreach (string r in rows)
{
//Split the row at the delimiter.
string[] items = r.Split(delimiter.ToCharArray());
//Add the item
result.Tables[TableName].Rows.Add(items);
}
//Return the imported data.
return result;
}
}
}
Step 2 > Add this namespace to your working page
using TestTextToDataSet;
Step 3> Add this code to your working page.
DataSet ds = TextToDataSet.Convert( @"c:\TestImport.txt","MyNewTable", "\t");
if (ds.Tables[0].Rows.Count > 0)
{
DataGridView1.DataSource = ds.Tables["MyNewTable"];
DataGridView1.DataBind();
}
Sam HobbsPosted Jun 7, 2011, 3:42 PM
I hope you don't mind me saying that your avatar (or maybe it is called profile picture) animation is real cute but it is distracting for me. I don't know why.
Posted Jun 7, 2011, 8:01 AM
string s = "Name=Rajan|EmpID=12345|Company Name=ABC|Address=ABCDE|PIN=110001";
//string[] split = new string("|");
char[] delimiters = new char[] { '|'};
string[] items = s.Split(delimiters);
delimiters = new char[] { '=' };
string[] columns = null;
DataTable dt = new DataTable();
foreach(string str in items)
{
columns = str.Split(delimiters);
dt.Columns.Add(new DataColumn(columns[0]));
}
//import the row
DataRow dr = dt.NewRow();
foreach (string str in items)
{
columns = str.Split(delimiters);
dr[columns[0]] = columns[1];
}
dt.Rows.Add(dr);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dt;
Hope this helps you,.
Santosh KumarPosted Jun 7, 2011, 7:49 AM
Name=Rajan|EmpID=12345|Company Name=ABC|Address=ABCDE|PIN=110001
and so on.
Posted Jun 7, 2011, 7:25 AM