DataGrid displaying line breaks in bound column

Discussion in 'ASP .Net' started by =?Utf-8?B?Um9za28=?=, Feb 9, 2006.

  1. Hello - I'm displaying a SQL Server text field in a bound column. The text
    field has line breaks (CrLf - 0D0A) but the text displays in a long string in
    the DataGrid. Anybody know how to display them?
    --
    Rosko
     
    =?Utf-8?B?Um9za28=?=, Feb 9, 2006
    #1
    1. Advertising

  2. Sorry...forgot to mention this is ASP.NET on our intranet.
    --
    Rosko


    "Rosko" wrote:

    > Hello - I'm displaying a SQL Server text field in a bound column. The text
    > field has line breaks (CrLf - 0D0A) but the text displays in a long string in
    > the DataGrid. Anybody know how to display them?
    > --
    > Rosko
     
    =?Utf-8?B?Um9za28=?=, Feb 9, 2006
    #2
    1. Advertising

  3. Rosko,

    Use a databinding expression that replaces the occurrences of
    Environment.NewLine with "<br/>" as I did in this demo:
    http://www.societopia.net/Samples/DataGrid_Hierarchy.aspx

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Rosko" wrote:

    > Sorry...forgot to mention this is ASP.NET on our intranet.
    > --
    > Rosko
    >
    >
    > "Rosko" wrote:
    >
    > > Hello - I'm displaying a SQL Server text field in a bound column. The text
    > > field has line breaks (CrLf - 0D0A) but the text displays in a long string in
    > > the DataGrid. Anybody know how to display them?
    > > --
    > > Rosko
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Feb 9, 2006
    #3
  4. Works great. So simple, so elusive. Thanks so much for your help!
    --
    Rosko


    "Phillip Williams" wrote:

    > Rosko,
    >
    > Use a databinding expression that replaces the occurrences of
    > Environment.NewLine with "<br/>" as I did in this demo:
    > http://www.societopia.net/Samples/DataGrid_Hierarchy.aspx
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Rosko" wrote:
    >
    > > Sorry...forgot to mention this is ASP.NET on our intranet.
    > > --
    > > Rosko
    > >
    > >
    > > "Rosko" wrote:
    > >
    > > > Hello - I'm displaying a SQL Server text field in a bound column. The text
    > > > field has line breaks (CrLf - 0D0A) but the text displays in a long string in
    > > > the DataGrid. Anybody know how to display them?
    > > > --
    > > > Rosko
     
    =?Utf-8?B?Um9za28=?=, Feb 9, 2006
    #4
  5. You are quite welcome.
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Rosko" wrote:

    > Works great. So simple, so elusive. Thanks so much for your help!
    > --
    > Rosko
    >
    >
    > "Phillip Williams" wrote:
    >
    > > Rosko,
    > >
    > > Use a databinding expression that replaces the occurrences of
    > > Environment.NewLine with "<br/>" as I did in this demo:
    > > http://www.societopia.net/Samples/DataGrid_Hierarchy.aspx
    > >
    > > --
    > > HTH,
    > > Phillip Williams
    > > http://www.societopia.net
    > > http://www.webswapp.com
    > >
    > >
    > > "Rosko" wrote:
    > >
    > > > Sorry...forgot to mention this is ASP.NET on our intranet.
    > > > --
    > > > Rosko
    > > >
    > > >
    > > > "Rosko" wrote:
    > > >
    > > > > Hello - I'm displaying a SQL Server text field in a bound column. The text
    > > > > field has line breaks (CrLf - 0D0A) but the text displays in a long string in
    > > > > the DataGrid. Anybody know how to display them?
    > > > > --
    > > > > Rosko
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Feb 9, 2006
    #5
  6. =?Utf-8?B?Um9za28=?=

    mohanms

    Joined:
    Mar 21, 2011
    Messages:
    1
    hi guys.
    its not workin for me... :(
    im using Ajax grid. but the methods r almost the same. im jut bindin the data from the front end insead of the backend. the rest should be similar.

    when i used the methods suggested, "<br/ >". its displayin the <br/ > instead of movin to newline. any help? thanks
     
    mohanms, Mar 21, 2011
    #6
  7. =?Utf-8?B?Um9za28=?=

    CraigPaardekooper

    Joined:
    Dec 20, 2011
    Messages:
    1
    Easy way to add line breaks to Datagrid bound columns

    So you want to add line breaks to a datagrid, and you are using bound columns.

    You can achieve this as follows -

    1. When you insert text into your database use - replace(vbcrlf, "<br>")

    2. When you click on the Edit button (ie the datagrid_editcommand) column then use - replace("<br", vbcrlf) . The way I do this is I create a Method and call it immediately after the databinding event.



    Protected Sub GridView1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles GridView1.EditCommand

    GridView1.EditItemIndex = e.Item.ItemIndex
    Dim ds As DataSet = GetDataset()

    BindGrid(ds)

    AddLineBreaks()

    End Sub


    The AddLineBreaks Method loops through all the rows of the datagrid, and when I find the row that is in EditMode, then I can replace the "<br>" in that row with vbcrlf

    Sub AddLineBreaks()

    For i = 0 To GridView1.Items.Count - 1

    If GridView1.Items(i).ItemType = ListItemType.EditItem Then

    CType(GridView1.Items(i).Cells(1).Controls(0), TextBox).TextMode = TextBoxMode.MultiLine

    CType(GridView1.Items(i).Cells(1).Controls(0), TextBox).Width = System.Web.UI.WebControls.Unit.Pixel(400)

    CType(GridView1.Items(i).Cells(1).Controls(0), TextBox).Height = System.Web.UI.WebControls.Unit.Pixel(100)

    CType(GridView1.Items(i).Cells(1).Controls(0), TextBox).Text = CType(GridView1.Items(i).Cells(1).Controls(0), TextBox).Text.Replace("<br>", vbCrLf)

    End If

    Next

    Note that this will also allow you to resize the textboxes during edit mode for a datagrid

    The Items collection of a datagrid are it's rows, and each row contains a collection of Cells, and each cell conatins a collection of controls. So you can speify which cells you want to replace <br> with vbcrlf, and which textboxes you want to resize.

    3. When you click on the update button then use - replace(vbcrlf, "<br") . The update button in a datagrid triggers the update_command event. This will read the contents of a textbox while in edit mode, and replace vbcrlf with <br>

    YOURCOLUMNNAME =
    CType(e.Item.Cells(1).Controls(0), TextBox).Text.Replace(vbCrLf, "<br>")

    I hope this helps datagrid people with both line breaks and with resizing editmode textboxes.
     
    CraigPaardekooper, Dec 20, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John E.
    Replies:
    3
    Views:
    4,485
    masoud bayan
    Mar 25, 2005
  2. Replies:
    29
    Views:
    938
    Roy A.
    May 26, 2008
  3. Ravichandran Mahalingam

    Bound Column or Template Column (w dAdapater?) in DataGrid

    Ravichandran Mahalingam, Jan 9, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    230
    Ravichandran Mahalingam
    Jan 9, 2004
  4. John E.

    Move bound column to right of dynamic column in datagrid?

    John E., Mar 24, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    239
    Elton Wang
    Mar 28, 2005
  5. Marek Stepanek
    Replies:
    12
    Views:
    422
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page