Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to show firstname  & lastname in GridView

Posted on 2008-06-14
8
Medium Priority
?
402 Views
Last Modified: 2013-11-26
Hi expert,

I have a girdview and I have a boundfield, how I can show firstname and lastname together in one column, they  are fields in database table,  when user click on name, it should sort by lastname.

<asp:BoundField DataField=lastname" HeaderText="Name" SortExpression=lastname" />

I didn't use sqldatasource control.

Thanks in advance.
0
Comment
Question by:dshi15
[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
  • 4
8 Comments
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 21786667
you could put them in a template field
<asp:TemplateField HeaderText="lname" SortExpression="lname">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("fname") %>'></asp:TextBox>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("lname") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("fname") %> '></asp:Label> <asp:Label ID="Label1" runat="server" Text='<%# Bind("lname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

Open in new window

0
 
LVL 1

Author Comment

by:dshi15
ID: 21787199
can I just put one lable? why it doesn't work. I like "," between first and last name, I don't need edit them.

<ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("lname")%>,<%# Bind("fname")%>'></asp:Label>
 </ItemTemplate>
0
 
LVL 14

Accepted Solution

by:
Dustin Hopkins earned 2000 total points
ID: 21787340
if you don't want to use multiple labels then you could use this below, or you could just place a comma between the labels.
<ItemTemplate>
                    <%#DataBinder.Eval(Container.DataItem, "lname")%>, <%#DataBinder.Eval(Container.DataItem, "fname")%>
                    </ItemTemplate>

Open in new window

0
URL rewriting in AWS CloudFront

A quick how-to guide to implement with a Lambda function!

 
LVL 1

Author Comment

by:dshi15
ID: 21788456
thanks, it works, but when I click on Name and try to sort, it said

"The GridView 'gvCustomers' fired event Sorting which wasn't handled"


 <asp:TemplateField HeaderText="Name" SortExpression="lname">
                     <ItemTemplate>
                                <%#DataBinder.Eval(Container.DataItem, "lname")%>, <%#DataBinder.Eval(Container.DataItem, "fname")%>
                            </ItemTemplate>
                        </asp:TemplateField>

Open in new window

0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 21788503
What type of datasource are you useing? How are you binding the gridview?
0
 
LVL 1

Author Comment

by:dshi15
ID: 21788681
I didn't use sqldatasoure control, I used datatable to bind gridview, it looks like I can not use autosort, I found code online and I copied them, it works now, thanks.

Protected Sub gvCustomers_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)

            Dim DataTable As DataTable
            DataTable = gvCustomers.DataSource

            If Not IsDBNull(DataTable) Then

                Dim DataView As DataView
                DataView = New DataView(DataTable)
                DataView.Sort = e.SortExpression & " " & ConvertSortDirectionToSql(e.SortDirection)

                gvCustomers.DataSource = DataView
                gvCustomers.DataBind()

            End If
        End Sub
        Private Function ConvertSortDirectionToSql(ByVal SortDirection As SortDirection)

            Dim newSortDirection As String = ""

            Select Case SortDirection

                Case SortDirection.Ascending
                    newSortDirection = "ASC"
                Case SortDirection.Descending
                    newSortDirection = "DESC"

            End Select

            Return newSortDirection
        End Function
0
 
LVL 1

Author Comment

by:dshi15
ID: 21788701
Hi Guru,

after I just closed question, I find one problem, when I click company header, it can sort by ASC order, but when I click on it again, it still ASC order and it do not change to DESC order, do you know why? Thanks.
0
 
LVL 14

Expert Comment

by:Dustin Hopkins
ID: 21788961
hmm, honestly i don't program manual sorting much, and I can't seem to reproduce your problem, you may want to post that issue as a new question.
0

Featured Post

Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

664 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