// 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 [email protected] "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 <datagridview1.rows. Count-1; I ++)
{
For (Int J = 0; j <datagridview1.columns. Count; 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 ();
}
C # datagirdview manually add data, export TXT files, and automatically align