Solved

Manipulate strings in datagrid

Posted on 2004-09-22
5
266 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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…

756 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