Solved

Manipulate strings in datagrid

Posted on 2004-09-22
5
263 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys, I split because praneetha was first but blueforce was more detailed.

dave
0
 
LVL 15

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now