Playing with strings b4 displaying via DataGrid

Results 1 to 2 of 2

Thread: Playing with strings b4 displaying via DataGrid

  1. #1
    Join Date
    Dec 1969

    Default Playing with strings b4 displaying via DataGrid

    Hi there. I'm still new to ASP.NET so bear with me as I'm trying to make sense here. I already know how to display data from the database via the DataGrid web control (thanks to the excellent series of DataGrid articles posted here on this very website) but how do I make changes to the string value retreived from the database before displaying on the webpage? For example, it retrieves a value of " Bobby ." from the database and I want to remove a period and trim off whitespace on both ends so it would appear "Bobby" on the webpage. The DataFormatString property of the BoundColumn class is just for formatting numeric values. Can anyone help me please with an example of how to manipulate database values using String methods in this situation? Many thanks in advance, Shellhead

  2. #2
    Join Date
    Dec 1969

    Default RE: Playing with strings b4 displaying via DataGri

    Try using a TemplateColumn that calls a method to format the string. Define that method in your code behind to do the trimming and replacing. I took this example from one of my files. It uses a hyperlink control inside an itemTemplate. Notice the naviageURL property, it sets the value to the return string of the makeURL method, which takes the username from the db as a parameter:<BR> &#060;asp:DataGrid ID="dgFoo" Runat="server" AutoGenerateColumns="False"&#062;<BR> &#060;Columns&#062;<BR> &#060;asp:TemplateColumn&#062;<BR> &#060;ItemTemplate&#062;<BR> &#060;asp:HyperLink ID="hlFoo" Runat="server" NavigateUrl=&#039;&#060;%# makeURL(DataBinder.Eval(Container.DataItem, "UserName")) %&#062;&#039;&#062;<BR> &#060;%# DataBinder.Eval(Container.DataItem, "UserName") %&#062;<BR> &#060;/asp:HyperLink&#062;<BR> &#060;/ItemTemplate&#062;<BR> &#060;/asp:TemplateColumn&#062;<BR> &#060;/Columns&#062;<BR> &#060;/asp:DataGrid&#062;<BR>--------------------------------------------------------<BR>The method in the code behind is simple this:<BR> protected string makeURL(object strQsVal)<BR> {<BR> return "navUrl.aspx?userName=" + strQsVal;<BR> }<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts