gridview wrapping column with no space in data

Last post 04-30-2009 4:03 PM by vinz. 5 replies.

Sort Posts:

  • gridview wrapping column with no space in data

    04-30-2009, 12:16 PM
    • Member
      point Member
    • lkrterp
    • Member since 04-17-2009, 6:14 PM
    • Posts 25

    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="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
    
     
  • Re: gridview wrapping column with no space in data

    04-30-2009, 1:37 PM
    • All-Star
      94,406 point All-Star
    • vinz
    • Member since 10-05-2007, 3:47 PM
    • Cebu, PH
    • Posts 13,998
    • TrustedFriends-MVPs

    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


    "Code,Beer and Music ~ my way of being a programmer"



  • Re: gridview wrapping column with no space in data

    04-30-2009, 2:58 PM
    • Member
      point Member
    • lkrterp
    • Member since 04-17-2009, 6:14 PM
    • Posts 25

    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.

     

  • Re: gridview wrapping column with no space in data

    04-30-2009, 3:13 PM
    Answer
    • All-Star
      94,406 point All-Star
    • vinz
    • Member since 10-05-2007, 3:47 PM
    • Cebu, PH
    • Posts 13,998
    • TrustedFriends-MVPs

    lkrterp:
    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();
    }

    }

    }

     



    "Code,Beer and Music ~ my way of being a programmer"



  • Re: gridview wrapping column with no space in data

    04-30-2009, 3:41 PM
    • Member
      point Member
    • lkrterp
    • Member since 04-17-2009, 6:14 PM
    • Posts 25

    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!    Smile

  • Re: gridview wrapping column with no space in data

    04-30-2009, 4:03 PM
    • All-Star
      94,406 point All-Star
    • vinz
    • Member since 10-05-2007, 3:47 PM
    • Cebu, PH
    • Posts 13,998
    • TrustedFriends-MVPs

    lkrterp:

    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! :)

    lkrterp:
    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

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

    Big Smile  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...



    "Code,Beer and Music ~ my way of being a programmer"



Page 1 of 1 (6 items)