locked
gridview wrapping column with no space in data RRS feed

  • Question

  • User-916797521 posted

    Hi All!

    I have a question concerning the wrapping of data in a gridview column.  I set my bound field item style as follows for a particular column. 

    <asp:BoundField DataField="url" HeaderText="URL" ItemStyle-Width="15%" SortExpression="url">
                    <ItemStyle Font-Size=<ITEMSTYLE class=st Font-Size=""8pt" Wrap="true" Width="20%"></asp:BoundField>
                                

     I noticed that the Wrap only breaks on spaces.  Is there a way to break the data when there is no spaces?  This column displays urls and can cause the column to extend too far and distorts the grid and other columns.  Especially when some urls have no spaces.

     Thanks

     

    This is my code retrieving and binding the data:

        Private Sub PopulateGrid(ByVal bind As Boolean)
    
            'Dim rpt As Reports
            Dim aReports As ArrayList = New ArrayList
            Dim sSQL As String = ""
            Dim sSQLWhere As String = ""
            Dim sCategory As String = ""
            Dim sCat As String = ""
            Dim DB As New ADODB.Connection
            Dim RS As ADODB.Recordset
    
            DB.Open("DSN=Test", "aa", "123")
    
            Try
                sSQL = "SELECT * FROM dep_reports"
                If txtSearchName.Text &lt;&gt; "" And ddlSearchCategory.Text <> "" Then
                    lblStatus.Text = "You must use one of the fields for your search"
                    GoTo StopGrid
                ElseIf txtSearchName.Text <> "" And ddlSearchCategory.Text = "" Then
                    sSQLWhere = " WHERE rep_name = '" & txtSearchName.Text.Trim.ToUpper & "'"
                ElseIf txtSearchName.Text = "" And ddlSearchCategory.Text <> "" Then
                    sCategory = ddlSearchCategory.Text.Trim.ToUpper
                    sCat = GetCategory(sCategory)
                    sSQLWhere = " WHERE rep_cat = '" & sCat & "'"
                Else
                    sSQLWhere = ""
                End If
    
                sSQL += sSQLWhere
                sSQL += " ORDER BY rep_name"
    
                RS = DB.Execute(sSQL)
    
                While Not RS.EOF
                    rpt = New Reports
                    rpt.Name = RS.Fields("rep_name").Value.ToString.Trim
                    rpt.Desc = RS.Fields("rep_desc").Value.ToString.Trim
                    rpt.Role = RS.Fields("rep_role").Value.ToString.Trim
                    rpt.Category = RS.Fields("rep_cat").Value.ToString.Trim
                    rpt.URL = RS.Fields("rep_url").Value.ToString.Trim
                    rpt.Keywords = RS.Fields("rep_keywords").Value.ToString.Trim
                    rpt.LastUsed = RS.Fields("rep_last_used").Value.ToString.Trim
                    rpt.LongDesc = RS.Fields("rep_long_desc").Value.ToString.Trim
    
    
                    aReports.Add(rpt)
    
                    RS.MoveNext()
                End While
    
                Session("Reports") = aReports
    
                If bind Then
                    BindData()
                End If
            Catch ex As Exception
                lblStatus.Text = "Error retrieving Report information." & ex.Message & "   " & sSQL
    
            End Try
    
            DB.Close()
    
    StopGrid:
        End Sub
    
        Private Sub BindData()
    
            gvReport.DataSource = Session("Reports")
            gvReport.DataBind()
            gvReport.SelectedIndex = -1
    
        End Sub
    
     
    Thursday, April 30, 2009 12:16 PM

Answers

  • User-1034726716 posted

    You can set style to GridView like below

     

    protected void Page_Load(object sender, EventArgs e)
    {

    GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");

    }
      

    See the following threads below for more detail:

    http://forums.asp.net/p/1396420/3003621.aspx

    http://forums.asp.net/t/1307972.aspx
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2009 1:37 PM
  • User-1034726716 posted

    but is there a way to just set that to the url column in the grid and not all the columns? 
     

    have tried adding the attributes for that column only?

     

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
            }
        }
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            this.GridView1.Columns[0].ItemStyle.Width = new Unit(100);
        }
      

     

    Another way that I could think of is to limit the data to be displayed in the GridView instead and display the Full content of the data in the ToolTip.. see below

     

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    ViewState["OrigData"] = e.Row.Cells[0].Text;
    if (e.Row.Cells[0].Text.Length >= 30) //Just change the value of 30 based on your requirements
    {
    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Substring(0, 30) + "...";
    e.Row.Cells[0].ToolTip = ViewState["OrigData"].ToString();
    }

    }

    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2009 3:13 PM
  • User-1034726716 posted

    Thanks again Vinz!

    I had to convert to vb; and your first suggestion is working perfectly in my code.

     

    Glad to hear that it works or you! :)

    One question... what is the purpose of the new Unit[100].  It works, but just curious.
     

    The Unit structure represents a length measurement that can be expressed in any HTML-compatible size unit. For more info then see: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.unit.aspx

    Now I would like to have one of those beers to go with my music and code!    Smile
     

    [:D]  Cheers for that!

     

    BTW,If you're issue was resolved, then don't forget to mark those helpful post(s) as anwser(s) for future references...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2009 4:03 PM

All replies

  • User-1034726716 posted

    You can set style to GridView like below

     

    protected void Page_Load(object sender, EventArgs e)
    {

    GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");

    }
      

    See the following threads below for more detail:

    http://forums.asp.net/p/1396420/3003621.aspx

    http://forums.asp.net/t/1307972.aspx
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2009 1:37 PM
  • User-916797521 posted

    Thanks for the response vinz!

    It works nicely, but is there a way to just set that to the url column in the grid and not all the columns?  I would like the other columns to still break on the spaces since I don't have a long string issue with any of them.

     

    Thursday, April 30, 2009 2:58 PM
  • User-1034726716 posted

    but is there a way to just set that to the url column in the grid and not all the columns? 
     

    have tried adding the attributes for that column only?

     

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
            }
        }
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            this.GridView1.Columns[0].ItemStyle.Width = new Unit(100);
        }
      

     

    Another way that I could think of is to limit the data to be displayed in the GridView instead and display the Full content of the data in the ToolTip.. see below

     

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    ViewState["OrigData"] = e.Row.Cells[0].Text;
    if (e.Row.Cells[0].Text.Length >= 30) //Just change the value of 30 based on your requirements
    {
    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Substring(0, 30) + "...";
    e.Row.Cells[0].ToolTip = ViewState["OrigData"].ToString();
    }

    }

    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2009 3:13 PM
  • User-916797521 posted

    Thanks again Vinz!

    I had to convert to vb; and your first suggestion is working perfectly in my code.

    One question... what is the purpose of the new Unit[100].  It works, but just curious.

    Now I would like to have one of those beers to go with my music and code!    [:)]

    Thursday, April 30, 2009 3:41 PM
  • User-1034726716 posted

    Thanks again Vinz!

    I had to convert to vb; and your first suggestion is working perfectly in my code.

     

    Glad to hear that it works or you! :)

    One question... what is the purpose of the new Unit[100].  It works, but just curious.
     

    The Unit structure represents a length measurement that can be expressed in any HTML-compatible size unit. For more info then see: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.unit.aspx

    Now I would like to have one of those beers to go with my music and code!    Smile
     

    [:D]  Cheers for that!

     

    BTW,If you're issue was resolved, then don't forget to mark those helpful post(s) as anwser(s) for future references...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2009 4:03 PM
  • User945338570 posted

    You can set style to GridView like below
    
    
     
    protected void Page_Load(object sender, EventArgs e)
    {
    
    GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
    
    }

      

    See the following threads below for more detail:

    http://forums.asp.net/p/1396420/3003621.aspx

    http://forums.asp.net/t/1307972.aspx

    Thanks it worked great for me.

    Thursday, August 22, 2013 6:35 AM