Solved

Merge Three Fields in Gridview Itemtemplate Field into one ASP.NET 4.0

Posted on 2013-01-25
6
698 Views
Last Modified: 2013-01-25
Hello Experts,

I have a Field phone, where I need to bind three fields into one.
    <asp:TemplateField ItemStyle-HorizontalAlign="Left">
 <HeaderTemplate>
   Phone</HeaderTemplate>
  <HeaderStyle HorizontalAlign="Left" Width="175px" />
   <ItemTemplate>
 <asp:Label ID="lblPhnNbr" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PHN_NUMBER") !="" ? String.Format("{0:000-0000}",Convert.ToInt64(DataBinder.Eval(Container.DataItem, "NUMBER"))) : ""%>'
Font-Underline="false" ForeColor="Blue" style="display:none" />
</ItemTemplate>
</asp:TemplateField>

Open in new window


To the above code I need to Add Area Code bind field("AREA") and EXTN all equal to just one field like this:

212-222-2222 0000
0
Comment
Question by:ASPDEV
[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
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:Element1910
ID: 38820143
You are close.

Try this:
<asp:TemplateField HeaderText="PhoneNum">
            <ItemTemplate>
                <asp:Label ID="lblPhnNbr" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PHN_NUMBER") !="" ? String.Format("{0:000-0000}",Convert.ToInt64(DataBinder.Eval(Container.DataItem, "AREA") + DataBinder.Eval(Container.DataItem, "NUMBER") + " " + DataBinder.Eval(Container.DataItem, "EXTN"))) : ""%>' Font-Underline="false" ForeColor="Blue" style="display:none" />
            </ItemTemplate>
            </asp:TemplateField>

Open in new window


Wasn't able to test it, but it should work.
0
 

Author Comment

by:ASPDEV
ID: 38820173
I was getting error:
"operator '+' cannot be applied to operands of type object and object.
0
 
LVL 7

Expert Comment

by:Element1910
ID: 38820217
Ok, then try to do this then:
<asp:TemplateField HeaderText="PhoneNum">
            <ItemTemplate>
                <asp:Label ID="lblPhnNbr" runat="server" Text='Label' Font-Underline="false" ForeColor="Blue" style="display:none">
                <%# Eval("AREA") + "-" + (String.Format("{0:000-0000}", Eval("NUMBER"))) + " " + Eval("EXTN")%>
                </asp:Label>
            </ItemTemplate>
            </asp:TemplateField>

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 7

Expert Comment

by:Element1910
ID: 38820244
That didn't work either...Ok, got a running visual studio test project now. I'll get this working for you shortly :) That's what I get for trying to do things from memory :P
0
 
LVL 7

Accepted Solution

by:
Element1910 earned 500 total points
ID: 38820376
Ok, I got it working with this in my ASPX page from my test database:

<asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:BoundField DataField="AREA" HeaderText="AREA" visible="false"/>
            <asp:BoundField DataField="PHONE" HeaderText="PHONE" visible="false"/>
            <asp:BoundField DataField="EXTN" HeaderText="EXTN" visible="false"/>
            <asp:TemplateField HeaderText="Phone">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text="">
                        <%# Eval("AREA") + "-" + (String.Format("{0:000-0000}", Eval("PHONE"))) + " Ex:" + Eval("EXTN")%>                    
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Open in new window


then in my code-behind file in the page load event, I did this:


protected void Page_Load(object sender, EventArgs e)
        {
            string con = ConfigurationManager.ConnectionStrings["TestConnectionStringFromWebConfig"].ConnectionString;

            string sql = "SELECT * FROM [Test]";

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(ds);

            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

Open in new window

0
 

Author Closing Comment

by:ASPDEV
ID: 38820590
Excellent post, except with the format.

The result was like this:
212-2222111 ext 113 instead of 212-2222-2111 ext 113

I tried this and worked
 <asp:Label ID="Label1" runat="server" Text="">
                        <%# Eval("AREA") + "-" + (String.Format("{0:000-0000}", Convert.ToInt64(DataBinder.Eval(Container.DataItem,"PHONE")))) + " Ex:" + Eval("EXTN")%>                    
                    </asp:Label>

Thanks,
ASPDEV
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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