Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to display columns such as FirstName and LastName from DB as Name column in gridview.
Posted
Updated 27-Mar-14 0:15am
v2

on your sql query add some code like this


SQL
SELECT firstname + ' ' +  lastname  as 'Name' from table
 
Share this answer
 
go with solution 1 else
C#
<asp:templatefield xmlns:asp="#unknown">
  <itemtemplate>
        <asp:label id="lblfname" runat="server" text="<%# Eval("firstname") %>"> </asp:label>

<asp:label id="lbllname" runat="server" text='<%# Eval("lastname") %>'>
        </asp:label>
    </itemtemplate>
</asp:templatefield>
 
Share this answer
 
v3
Comments
Ni!E$H_WAGH 27-Mar-14 4:42am    
Thank you very much..
King Fisher 27-Mar-14 4:57am    
welcome..;)
C#
Method 1

Use this query retrive data from database

Select Fistname +' '+Lastname as Name from mytable



<asp:templatefield headertext="Name" headerstyle-horizontalalign="Left">
    <itemtemplate>
        <asp:label id="lblname" runat="server" text='<%# Eval("Name") %>'>
        </asp:label>
    </itemtemplate>
</asp:templatefield>



Method 2

If USE this query

Select Fistname ,Lastname  from mytable

 protected void ...._RowDataBound(object sender, GridViewRowEventArgs e)
    {
     
     
 Label "lblname" = new Label();
 lblname = (Label)(e.Row.FindControl("lblname"));
lblname.Text =   Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Fistname")) +  Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Lastname")) ;
 
Share this answer
 
v3
Comments
Ni!E$H_WAGH 27-Mar-14 4:42am    
Thanks...
small change code instead of "<%# Eval("Name") %>" it should be '<%# Eval("Name") %>'
Murugesan22 27-Mar-14 4:51am    
thanks,It has been changed
Append Fname + lname From DB ,select Fname+''+Sname as fullname from ....
 
Share this answer
 
Hey Buddy,

Write this code in the Grid View on "aspx.cs" page:

XML
<asp:GridView ID="grd" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
          <Columns>
              <asp:TemplateField HeaderText="Test">
                  <ItemTemplate>
                       <%#DataBinder.Eval(Container.DataItem,"Student_Name")%>
                        <%#DataBinder.Eval(Container.DataItem,"Student_ID")%>
                  </ItemTemplate>
              </asp:TemplateField>
          </Columns>
      </asp:GridView>


Here Student_name and Student_ID are coming from the database
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900