Skip to main content
gamedev.net
🔒 Locked

[.net] Data formatting expressions: Substring?

Started by Ravuya 6 replies 5.6k views
Original Post
Ravuya
Ravuya
I'm working on an ASP.NET application and need a way to quickly get the substring of a column (e.g. really big TEXT column and I want to pare it down to the first n characters). Just {0} isn't going to pull it if the column has a huuuuge string. [crying] I'd really rather not have to write the code for this and clutter up my load routines, so I'm leaning towards a string format expression in my DataGrid's columns collection. I can't seem to find stuff on this usage anywhere; the only String.Format stuff I can find for text involves padding. It seems like it must exist, but googling returns nothing.
Digibo
Digibo
I'm not sure exactly what are you trying to achieve, so my suggestions may sound lame :P

You mention getting a substring - maybe .Substring(0, n) is what you are looking for?

If you need to build a huuuge string then maybe the StringBuilder class is what you need.
Ravuya
Ravuya
I think you've misunderstood my question. Not surprising; I wrote it like an idiot.

1. I have a DataGrid.
2. Somewhere in one of the property pages for DataGrid is an option to set a "data formatting expression" for a column, so you can make it look prettier. Usually it's something like {0:#.##) for decimal numbers, or whatever. I assume it takes the same args as String.Format.
3. I need to figure out a data formatting expression that will just give me a substring.

I stated above that I could just iterate through my local dataset and trim all of that column, but I don't really want to, because it will clutter up the code.
Digibo
Digibo
Now it sounds like a candidate for a regular expressions. They should do the job, unless the text is not that big and a RegEx is an overkill.

I can't be of much help here, but googling for "RegEx .NET" gave some seemingly good results.
Ravuya
Ravuya
Uhhh, I don't think you can use regular expressions to obtain substrings.

I suspect I'll have to end up writing the column filter myself.
invective
invective
I don't think you can do it with format. You can trap ItemDataBound like so or like so. but its probably less trouble to just loop through the table and truncate the strings or copy the truncated strings into a new column and hide the old one. For an exercise in masochism you could try your own format.
Pouya
Pouya
There's no default formatting provider for setting maximum length of strings. (I've tried a lot)

But trimming the cells is actually easy. Add the event for ItemDataBound:

myDataGrid.ItemDataBound += new DataGridItemEventHandler(ItemBound);

Defined as:

void ItemBound(Object sender, DataGridItemEventArgs e) {    e.Item.Cells[1].Text = e.Item.Cells[1].Text.Substring(0, 50);}


This will trim column 1 of the grid to 50 characters. Change those accordingly.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.