Solved

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

Posted on 2013-01-25
6
617 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
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:Element1910
Comment Utility
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
Comment Utility
I was getting error:
"operator '+' cannot be applied to operands of type object and object.
0
 
LVL 7

Expert Comment

by:Element1910
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 7

Expert Comment

by:Element1910
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now