2014年7月13日 星期日

[RESOLVED] Insert new line in DataTable column


I have a webapplication that calls a webservice. The data from webservice is returned in an xml.  I then bind ths data as a datasource to a simple gridview.  Here is what I am trying to achieve:



DataTable dtPolicy = new DataTable();



dtPolicy.Columns.Add("TransType");



dtPolicy.Columns.Add("TranDesc");


using (DataView dvPolicies =
new DataView(dsData.Tables[0]))

{


foreach (DataRowView drvPolicy
in dvPolicies)

{


using (DataView dvTrans =
new DataView(dsData.Tables[1],
"PolicyID = " + drvPolicy["PolicyID"].ToString(),
"", DataViewRowState.CurrentRows))

{


foreach (DataRowView drvTrans
in dvTrans)

{


string mystring =
"";

DataRow drSummary = dtPolicy.NewRow();



drSummary["TransType"] = drvTrans["TransType"].ToString();



mystring += drvTrans["TransDescription1"].ToString().Trim() +
Environment.NewLine +


    drvTrans["TransDescription2"].ToString().Trim();


drSummary["TranDesc"] = mystring;

dtPolicy.Rows.Add(drSummary);



The problem s that I wasn TransDescription1 and TransDescription2 on seperate lines but it is not working.  I also tried "\r\n" instead of Environment.NewLine.  It just adds a space and combines both descriptions together.  Does anyone knows how can
I add a linefeed (new line) between the two descriptions in my Gridview column? So for each row of TransType there could be multiple descriptions in my gridiew.


Thank you!!!   



 try with
since it finally is rendered as HTML



i think when u said Environment.NewLine didn't work, the new_line_character already added to your string, but it didn't displayed in your screen in 2 separated line.


try using
instead of NewLine to show new line in html [:P]

 



adding line break will help you



How do I use here?


 


mystring += drvTrans["TransDescription1"].ToString().Trim() + ""  + drvTrans["TransDescription2"].ToString().Trim()


The above statement won't work, it will just think is a string. Sorry guys, I am a bit confused here......



how about


mystring += drvTrans["TransDescription1"].ToString().Trim() + "<br\>"  + drvTrans["TransDescription2"].ToString().Trim()


 


?? 



It is giving me "Unrecognized escape sequence" error when I compile.  "<br\>


 I am working in Visual studios 2005 if that helps?



Hi aamir01 ,


 

protected void Page_Load(object sender, EventArgs e)
{


DataTable table = new DataTable();
table.Columns.Add("test");


DataRow dr = table.NewRow();
dr["test"] = Server.HtmlDecode( "abc
aaa");
table.Rows.Add(dr);
this.GridView1.DataSource = table;
GridView1.DataBind();
}
  
    <form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="test" HtmlEncode="False" />
Columns>
asp:GridView>


form>


 


 



Thank you for your response.  This is VERY close to what I am trying to achieve.  What if abc and aaa in your example are two string variables? This is what I have in my code:


string aaa = "first string";


string aaa = "second string";


now I want to display in my gridview column: first string


                                                                 second string...


how do I put vale of a variable inside Server.HtmlDecode? Sorry, didn't mean to make this post an html tutorial but I goggled like crazy but couldn't find a solution to this problem which should be relatively simple code :)


 Thanks!



anyone? any ideas? would it help if I use a datalist instead of a Gridview?



According to Zamu,


Make this change in gridview,


<asp:BoundField DataField="test" HtmlEncode="False" />

Try with


mystring = "First string   Second String "

This works, i tested it.

reply if you face any difficulties.

沒有留言:

張貼留言