Last active
August 29, 2015 13:57
-
-
Save nisar1/9782371 to your computer and use it in GitHub Desktop.
txt to datagridview and vice versa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//method 1 txt to grid | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
var lines = File.ReadAllLines(@"D:\Nisar\workspace\biometric\backup data\ASL_001.TXT"); | |
if (lines.Count() > 0) | |
{ | |
foreach (var columnName in lines.FirstOrDefault() | |
.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries)) | |
{ | |
dataGridView1.Columns.Add(columnName, columnName); | |
} | |
foreach (var cellValues in lines.Skip(1)) | |
{ | |
var cellArray = cellValues | |
.Split(new[] { '\t' }); | |
if (cellArray.Length == dataGridView1.Columns.Count) | |
dataGridView1.Rows.Add(cellArray); | |
} | |
} | |
} | |
//method 2 little problem in this code that it take two consucative tabs at once | |
// so just refer it for logic | |
private void Form2_Load(object sender, EventArgs e) | |
{ | |
System.IO.StreamReader file = new System.IO.StreamReader(@"D:\Nisar\workspace\biometric\backup data\AGL_001.TXT"); | |
string[] columnnames = file.ReadLine().Split('\t'); | |
DataTable dt = new DataTable(); | |
foreach (string c in columnnames) | |
{ | |
dt.Columns.Add(c); | |
} | |
string newline; | |
while ((newline = file.ReadLine()) != null) | |
{ | |
DataRow dr = dt.NewRow(); | |
string[] values = newline.Split('\t'); | |
for (int i = 0; i < values.Length; i++) | |
{ | |
dr[i] = values[i]; | |
} | |
dt.Rows.Add(dr); | |
} | |
file.Close(); | |
dataGridView1.DataSource = dt; | |
} | |
// datagridview to txt | |
Public static void gtotxt() | |
{ | |
string strdestination = "D:\\kk.txt"; | |
TextWriter tw = new StreamWriter(strdestination); | |
for (int x = 0; x < gridSLogData.Columns.Count; x++) | |
{ | |
tw.Write(gridSLogData.Columns[x].HeaderText); | |
if (x != gridSLogData.Columns.Count - 1) | |
{ | |
tw.Write("\t"); | |
} | |
} | |
tw.WriteLine(); | |
for (int x = 0; x < gridSLogData.Rows.Count - 1; x++) | |
{ | |
for (int y = 0; y < gridSLogData.Columns.Count; y++) | |
{ | |
tw.Write(gridSLogData.Rows[x].Cells[y].Value); | |
if (y != gridSLogData.Columns.Count - 1) | |
{ | |
tw.Write("\t"); | |
} | |
} | |
tw.WriteLine(); | |
} | |
tw.Close(); | |
} | |
//datatable to txt | |
public static void WriteDataToFile(DataTable submittedDataTable, string submittedFilePath) | |
{ | |
int i = 0; | |
StreamWriter sw = null; | |
sw = new StreamWriter(submittedFilePath, false); | |
for (i = 0; i < submittedDataTable.Columns.Count - 1; i++) | |
{ | |
sw.Write(submittedDataTable.Columns[i].ColumnName + "\t"); | |
} | |
sw.Write(submittedDataTable.Columns[i].ColumnName); | |
sw.WriteLine(); | |
foreach (DataRow row in submittedDataTable.Rows) | |
{ | |
object[] array = row.ItemArray; | |
for (i = 0; i < array.Length - 1; i++) | |
{ | |
sw.Write(array[i].ToString() + "\t"); | |
} | |
sw.Write(array[i].ToString()); | |
sw.WriteLine(); | |
} | |
sw.Close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment