473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

GridView DataFormatString for handling strings? asp.net (C# code behind)

14
Hey,

does anyone know of a way (or an expression) to format strings into proper capitalization using a DataFormatString in an asp:boundfield.

eg.I'm wanting to turn "john smith" into "John Smith"

thanks in adavance
Aug 20 '07 #1
4 14953
nateraaaa
663 Expert 512MB
Hey,

does anyone know of a way (or an expression) to format strings into proper capitalization using a DataFormatString in an asp:boundfield.

eg.I'm wanting to turn "john smith" into "John Smith"

thanks in adavance
OK. After playing around with the RowDataBound event I found a way to accomplish what you are trying to do. You will need to use a MatchEvaluator and a StringBuilder to get this to work. See code below.

Expand|Select|Wrap|Line Numbers
  1. using System.Text.RegularExpressions;
  2.  
  3. protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
  4.         {
  5.             if(e.Row.RowType == DataControlRowType.DataRow)
  6.             {
  7.                 Search search = new Search(); //create a new instance of your page.
  8.                 MatchEvaluator evaluator = new MatchEvaluator(search.CapitalizeWord);
  9.  
  10.                 //the @ means do not escape anymore
  11.                 Regex re = new Regex(@"\w+");
  12.                 string output = re.Replace(e.Row.Cells[2].Text, evaluator);
  13.                 e.Row.Cells[2].Text = output;
  14.             }
  15.         }
  16.  
  17.         public string CapitalizeWord(Match match)
  18.         {
  19.             StringBuilder sb = new StringBuilder(match.Value);
  20.  
  21.             //Take the first letter, capitalize it, and set it back to the first letter.
  22.             sb[0] = Char.ToUpper(sb[0]);
  23.             return sb.ToString();
  24.         }
This has been tested on one word only. You may need to add additional code to make this work for your last name.

Nathan
Aug 20 '07 #2
ahoyer
14
OK. After playing around with the RowDataBound event I found a way to accomplish what you are trying to do. You will need to use a MatchEvaluator and a StringBuilder to get this to work. See code below.

Expand|Select|Wrap|Line Numbers
  1. using System.Text.RegularExpressions;
  2.  
  3. protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
  4.         {
  5.             if(e.Row.RowType == DataControlRowType.DataRow)
  6.             {
  7.                 Search search = new Search(); //create a new instance of your page.
  8.                 MatchEvaluator evaluator = new MatchEvaluator(search.CapitalizeWord);
  9.  
  10.                 //the @ means do not escape anymore
  11.                 Regex re = new Regex(@"\w+");
  12.                 string output = re.Replace(e.Row.Cells[2].Text, evaluator);
  13.                 e.Row.Cells[2].Text = output;
  14.             }
  15.         }
  16.  
  17.         public string CapitalizeWord(Match match)
  18.         {
  19.             StringBuilder sb = new StringBuilder(match.Value);
  20.  
  21.             //Take the first letter, capitalize it, and set it back to the first letter.
  22.             sb[0] = Char.ToUpper(sb[0]);
  23.             return sb.ToString();
  24.         }
This has been tested on one word only. You may need to add additional code to make this work for your last name.

Nathan
thanks very much for the help, but how would i implement this so that when the gridview is populated, that it calls these functions to format only one column?
Aug 22 '07 #3
nateraaaa
663 Expert 512MB
thanks very much for the help, but how would i implement this so that when the gridview is populated, that it calls these functions to format only one column?
When you reference the Cell number that you want to modify. The cells for that column will be updated with the text formatting. In my example I used Cell[2].Text which is the 3rd column in the grid.

Nathan
Aug 22 '07 #4
ahoyer
14
thanks very much for the help, but how would i implement this so that when the gridview is populated, that it calls these functions to format only one column?
hey forget what i said, i found an easier way to get it done, all i had to do was just get the grid view to add a small div tag around the words i wanted capitalized, i did this by setting the DataFormatString to the string below.

DataFormatString="<div style="text-transform:capitalize">{0}</div>"

when the page loads, it creates something like this:
[HTML]
<div style="text-transform:capitalize">some_text</div> [/HTML]

thanks again for your help, and i hope this helps other people!
Aug 22 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: sck10 | last post by:
Hello, I have a GridView that is using the following to connect to a SQL Server 2000 stored procedure: <asp:SqlDataSource ID="dsWebDocList" SelectCommand="sp_web_WebDocument"...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
4
by: MasterChief | last post by:
I am trying to learn how to delete multple items in a database using the code behind file. I would just like somebody to tell me what is wrong with my code. I am new to connecting to the database...
3
by: Dorte | last post by:
Hi, Could someone help me with a couple of links to SqlDatasource documentation on how to use the Gridview and SqlDatasource components in code behind? Basically I'm missing some documentation...
2
by: Charlie | last post by:
Hi: The new databound controls (GridView, DetailsView, ect.) use declaritive syntax (i.e, setup in HTML at design time). However, at times sql statements and parameters may need to be derived...
0
by: jmacduff | last post by:
Big question: How to enable edit/update commands to work when setting the sqldatasource select command from code behind. Details: I have a GridView using a sqldatasouce with the select and...
13
by: Mich | last post by:
Hi, 1) i defined a html line in the aspx file like this but it's still visible:: <hr id="hr1" visible="false" /> So i defined it like this and now it's hidden: <hr id="hr1"...
2
by: sivagururaja | last post by:
Hi All, How can i sorting the Gridview Columns via the code behind. When i tried to sorting the column it doesn't work. SqlConnection con = new SqlConnection("Connection string");...
3
by: RPhlb | last post by:
This is my first post, so excuse me if I don't get this right the first time. I have an issue where when I use DropDownList I only get the first, or "SelectedValue" back when I update a GridView...
8
by: Brett | last post by:
I wrote an ASP.NET application that queries a SQL Server database (on a different box from the web server) and displays the result in a GridView. The datasource for the GridView is a SQLDataSource....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.