Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,085 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,552 people online right now. Registration is fast and FREE... Join Now!




GridView Formatting - DataBinder.Eval

 
Reply to this topicStart new topic

GridView Formatting - DataBinder.Eval, Trying to format data pulled after DataBind()

Jordan CB
16 Apr, 2008 - 07:59 AM
Post #1

New D.I.C Head
*

Joined: 16 Apr, 2008
Posts: 15

Hello!

I have been messing with this for a while now and thought I would ask the gurus. You all will probably know exactly how to do what I'm wanting.

I am pulling info from a MySQL DB and displaying the DataReader results in a GridView. I have the following code using the DataBinder.Eval and ItemTemplates within the GridView in order to bind my columns.

CODE


public partial class _Default : System.Web.UI.Page
{
    MySql.Data.MySqlClient.MySqlConnection myConnection = new MySql.Data.MySqlClient.MySqlConnection("Data Source=localhost;Database=portaldb;pooling=false;");
    MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand();
    MySql.Data.MySqlClient.MySqlDataReader myDR;
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        /* Agent Grid View Properties */
        gv_Virginia.AutoGenerateColumns = false;
        
        /* Agent Grid View - MySQL Declarations */
        myCommand.CommandText = "SELECT FName, LName, CellPhone FROM agents_master";
        myCommand.Connection = myConnection;

        /* Connect To Database And Pull Records */
        myConnection.Open();
        myDR = myCommand.ExecuteReader();

        /* Bind The GridView to our DataSource */
        gv_Virginia.DataSource = myDR;
        gv_Virginia.DataBind();

        /* Close DB Connection */
        myConnection.Close();
     }
}


And then I am using this code snippet to format my Cell Phone numbers (stored as 5555551234) as (555) 555 1234

CODE

public string FormatPhone(string phoneNumber)
        {
            string.Format("({0}) {1}-{2}",
                    phoneNumber.Substring(0, 3),
                    phoneNumber.Substring(3, 3),
                    phoneNumber.Substring(6));

            return phoneNumber;
        }


On The actual page_Load for the page this is how I am binding the columns to the fields out of the DataReader. I don't know if this is the best way to accomplish this, but I thought I could try calling my FormatPhone function around the DataBinder.Eval but guess not. Any ideas?

CODE

    <asp:GridView ID="gv_VirginiaStaff" runat="server" AutoGenerateColumns="False"
        style="top: 37px; left: 403px; position: absolute; height: 35px; width: 363px">
        <Columns>
            <asp:TemplateField HeaderText="Employee Name">
                <ItemTemplate>
                    <%# DataBinder.Eval(Container.DataItem, "LName")%> , <%# DataBinder.Eval(Container.DataItem, "FName")%>
                </ItemTemplate>
            </asp:TemplateField>
            
            <asp:TemplateField HeaderText="Cell Phone">
                <ItemTemplate>
                    <%# DataBinder.Eval(Container.DataItem, "CellPhone")%>
                </ItemTemplate>
            </asp:TemplateField>
            
            <asp:TemplateField HeaderText="Extension">
                <ItemTemplate>
                    <%# DataBinder.Eval(Container.DataItem, "Extension")%>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>


Any help is appreciated. Thanks for your time !

-- Jordan

User is offlineProfile CardPM
+Quote Post

skaoth
RE: GridView Formatting - DataBinder.Eval
16 Apr, 2008 - 09:32 AM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 342



Thanked: 10 times
Dream Kudos: 100
My Contributions
There are several ways to do this. Its been a while now but I think you can just wrap
your Eval() calls around FormatPhone() function

CODE

<%#this.FormatPhone( DataBinder.Eval(Container.DataItem, "Extension") )%>


The other way to do this is to handle the RowDataBound event
and call FormatPhone on the columns your interested in

CODE

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        // You should have access to the data via e.Row
}


User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: GridView Formatting - DataBinder.Eval
16 Apr, 2008 - 09:47 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8600
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
First off, I really wouldnt use a DataReader for this, I would go the simple route and populate a DataSet with the results from the query, then bind that to your GridView, then in your ItemTemplate files you only need this small line to bind your fields


csharp

<%#FormatPhone(Eval("YourColumnName"))%>


Notice how much more simple that is than using it the way you currently are (this is just a personal preference Im sure, but when doing it this way, with the DataSet, you have been accessibility to the data being displayed, paged and sorted).

Also, for formatting your phone number, it's a little more efficient to use RegularExpressions to accomplish this, here is a snippet I constantly use when I need to format a phone number


csharp

/// <summary>
/// method to format a US phone number
/// </summary>
/// <param name="num">phone number to format</param>
/// <returns></returns>
public static string FormatPhone(string num)
{
//first we must remove all non numeric characters
num = num.Replace("(", "").Replace(")","").Replace("-","");
string results = string.Empty;
string formatPattern = @"(\d{3})(\d{3})(\d{4})";
results = Regex.Replace(num, formatPattern, "($1) $2-$3");
//now return the formatted phone number
return results;
}

User is online!Profile CardPM
+Quote Post

Jordan CB
RE: GridView Formatting - DataBinder.Eval
16 Apr, 2008 - 11:39 AM
Post #4

New D.I.C Head
*

Joined: 16 Apr, 2008
Posts: 15

PsychoCoder, skaoth

Thanks for the reply! I appreciate it. Sounds like I was going the long way to do this. Glad you told me. Looks like what you've got will do the trick. I will give it a try and hopefully have no more questions! Thanks a bunch.

-- Jordan

This post has been edited by Jordan CB: 16 Apr, 2008 - 11:55 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:04PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month