Solved

Manipulate strings in datagrid

Posted on 2004-09-22
5
265 Views
Last Modified: 2010-04-15
I have a datagrid that pulls its data from a datareader.  Pretty basic stuff...

conn.Open();
dataReader = cmd.ExecuteReader();
dgUsage.DataSource = dataReader;
dgUsage.DataBind();

That part works fine.  One of the feilds is a GUID that I want to format.  instead of {xxxxxxxx-yyyy-zzzz-bbbb-cccccccccccc}  I want to strip everything except the xxxxxxxx.  I know how this is done using plain strings, but I can't find a way to use dataformatstring to get it done.  

Is there any way to accomplish this?

thanks
dave
0
Comment
Question by:KaiserSose
5 Comments
 
LVL 15

Assisted Solution

by:praneetha
praneetha earned 125 total points
ID: 12127343
in itemdatabound of the datagrid...

say it  is in e.item.cells[5].text - xxxxxxxx-yyyy-zzzz-bbbb-cccccccccccc

then string []temp=e.item.cells[5].text .split('-')

e.item.cells[5].text=temp[0]

good luck
0
 
LVL 1

Accepted Solution

by:
blueforce earned 125 total points
ID: 12127457
There's no format specifier for a GUID.

You can still do what you want though, you'll just have to use the datagrid OnItemDatabound event handler.

In your code you'll need to wire-up an event handler as such:

public void myDataGrid_ItemDataBound( object sender, DataGridItemEventArgs e)
{
     if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
     {
         TableCell cell = (TableCell)e.Item.Cells[X]; // X being the ordinal of the column in your grid
         string s = cell.Text;  
         s = s.Substring( 0, s.IndexOf("-") );
         cell.Text = s;
         cell.Dispose(); cell = null;
     }
}

Then in HTML set the OnItemDatabound property of the datagrid = myDataGrid_ItemDataBound and don't forget to wire it up in the page's InitializeComponent method.

0
 
LVL 2

Expert Comment

by:SimonTocker
ID: 12133991
I agree do it in on item databind and use whatever string manipulation you know how to do, substring, regexs whatever.  Blueforce is correct.  

There are other ways but they are just as nasty as databinding in the markup, it makes your source look so messy I always bind in the codebehind.

0
 
LVL 2

Author Comment

by:KaiserSose
ID: 12135208
Thanks guys, I split because praneetha was first but blueforce was more detailed.

dave
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12135239
Thank you KaiserSose(nice name - big fan of usual suspects)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question