从gridview读取数据,然后将输出写入文本文件 [英] Reading Data from gridview then write output to textfile

查看:104
本文介绍了从gridview读取数据,然后将输出写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据从gridview读取到文本文件中.问题是输出写入文件的方式.

reason1
reason2
Date1
Date2
Location1
位置2

但我需要将其显示为:

原因1日期1位置1
reason2 date2 location2

我该如何解决?还是有更好的方法呢?

Im reading data from gridview to a text file. Problem is the way the output is writen to file.

reason1
reason2
Date1
Date2
Location1
Location2

But I need the outpt to look like:

reason1 date1 location1
reason2 date2 location2

How can I solve this?Or is there a better way to do this?

protected void btnSendTravelingClaims_Click(object sender, EventArgs e) 
        { 
 
            string lblReason, lblDate, lblLocation; 
            List<string> listlblReason = new List<string>(); 
            List<string> listlblDate = new List<string>(); 
            List<string> listlblLocation = new List<string>(); 
         
        System.Text.StringBuilder _buildReason = new System.Text.StringBuilder(); 
        System.Text.StringBuilder _buildDate = new System.Text.StringBuilder(); 
        System.Text.StringBuilder _buildLocation = new System.Text.StringBuilder(); 
             
            foreach (GridViewRow row in grvTravelClaims.Rows) 
            { 
                 lblReason = ((Label)row.FindControl("lblReason")).Text; 
                 lblDate = ((Label)row.FindControl("lblDate")).Text; 
                 lblLocation = ((Label)row.FindControl("lblLocation")).Text; 
                                 
                 listlblReason.Add(lblReason); 
                 listlblDate.Add(lblDate); 
                 listlblLocation.Add(lblLocation); 
                           } 
            foreach (string a in listlblReason ) 
            { 
                _buildReason.Append(a.ToString()).AppendLine(); 
                 
            } 
            foreach (string a in listlblDate) 
            { 
                _buildDate.Append(a.ToString()).AppendLine(); 
            } 
            foreach (string a in listlblLocation) 
            { 
                _buildLocation.Append(a.ToString()).AppendLine(); 
            } 
            
         
            string pathFileName = Server.MapPath("~/bin/ClaimRecords.txt"); 
            StreamWriter _StreamWriter = new StreamWriter(pathFileName, true); 
 
            _StreamWriter.Write(_buildReason.ToString()); 
            _StreamWriter.Write( _buildDate.ToString()); 
            _StreamWriter.Write( _buildLocation.ToString()); 
             
            _StreamWriter.Close(); 
                      
 
        }

推荐答案

尝试更改这些行
Try to change these lines
_StreamWriter.Write(_buildReason.ToString()); 
_StreamWriter.Write( _buildDate.ToString()); 
_StreamWriter.Write( _buildLocation.ToString());  


与此


with this

_StreamWriter.WriteLine((string.Format("{0} {1} {2}",_buildReason.ToString(),_buildDate.ToString(), _buildLocation.ToString()))); 


这篇关于从gridview读取数据,然后将输出写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆