Solved

Manipulate strings in datagrid

Posted on 2004-09-22
5
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

734 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