Solved

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

Posted on 2013-01-25
6
738 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

634 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