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