C # DataGirdview manually add data, export txt files, and automatically align
// DataGirdview manually add data
Private void btn1_irdview_click (object sender, EventArgs e)
{
DataGridView1.Columns. Add ("a", "Title 1 ");
DataGridView1.Columns. Add ("B", "title 2 ");
DataGridView1.Columns. Add ("c", "Title 3 ");
String [] dr1 = {"1", "22", "3333 "};
String [] dr2 = {"4444", "55", "6 "};
DataGridViewRow Row = new DataGridViewRow ();
Int index = maid. Add (Row );
DataGridView1.Rows [index]. Cells [0]. Value = dr1 [0]. ToString ();
DataGridView1.Rows [index]. Cells [1]. Value = dr1 [1]. ToString ();
DataGridView1.Rows [index]. Cells [2]. Value = dr1 [2]. ToString ();
Maid Row2 = new maid ();
Int index2 = maid. Add (Row2 );
DataGridView1.Rows [index2]. Cells [0]. Value = dr2 [0];
DataGridView1.Rows [index2]. Cells [1]. Value = dr2 [1];
DataGridView1.Rows [index2]. Cells [2]. Value = dr2 [2];
}
// Export the txt file with DataGirdview automatically aligned
Private void btnOutPutText_Click (object sender, EventArgs e)
{
String FullFileName = @ "D: \ aaa.txt ";
// FileStream fs = new FileStream (FullFileName, FileMode. CreateNew );
StreamWriter sw = new StreamWriter (FullFileName, true, Encoding. Default );
String str = "";
For (int I = 0; I {
For (int j = 0; j {
Str = maid [I]. Cells [j]. Value. ToString (). Trim ();
If (str. Length <10)
Str = str. PadRight (10 ,'');
Str = str + "| ";
Sw. Write (str );
}
Sw. WriteLine ("");
}
Sw. Close ();
}