GridView Formatting - DataBinder.Eval

Trying to format data pulled after DataBind()

Page 1 of 1

3 Replies - 77620 Views - Last Post: 16 April 2008 - 12:39 PM Rate Topic: -----

#1 Jordan CB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-April 08

GridView Formatting - DataBinder.Eval

Posted 16 April 2008 - 08:59 AM

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.


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

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?

	<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

Is This A Good Question/Topic? 0
  • +

Replies To: GridView Formatting - DataBinder.Eval

#2 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: GridView Formatting - DataBinder.Eval

Posted 16 April 2008 - 10:32 AM

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

<%#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

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


Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: GridView Formatting - DataBinder.Eval

Posted 16 April 2008 - 10:47 AM

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


<%#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


/// <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;
}


Was This Post Helpful? 1
  • +
  • -

#4 Jordan CB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-April 08

Re: GridView Formatting - DataBinder.Eval

Posted 16 April 2008 - 12:39 PM

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 April 2008 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1