C# DataGirdview手動添加數據,導出txt文件並自動對齊
//DataGirdview手動添加數據
private void btnDataGirdView_Click(object sender,EventArgs e)
{
dataGridView1.Columns.Add("a","標題1");
dataGridView1.Columns.Add("b","標題2");
dataGridView1.Columns.Add("c","標題3");
string[] dr1= { "1","22","3333" };
string[] dr2= { "4444","55","6" };
DataGridViewRow Row=new DataGridViewRow();
int index=dataGridView1.Rows.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();
DataGridViewRow Row2=new DataGridViewRow();
int index2=dataGridView1.Rows.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];
}
//DataGirdview導出txt文件,並自動對齊
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=dataGridView1.Rows[i].Cells[j].Value.ToString().Trim();
if(str.Length<10)
str=str.PadRight(10,' ');
str=str+"|";
sw.Write(str);
}
sw.WriteLine("");
}
sw.Close();
}