Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Manipulate strings in datagrid

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
KaiserSose
Asked:
KaiserSose
2 Solutions
 
praneethaCommented:
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
 
blueforceCommented:
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
 
SimonTockerCommented:
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
 
KaiserSoseAuthor Commented:
Thanks guys, I split because praneetha was first but blueforce was more detailed.

dave
0
 
praneethaCommented:
Thank you KaiserSose(nice name - big fan of usual suspects)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now