You need to combine two database fields: Employee id and Name (EmpID : Name) and display in a single column of DataGrid. How can you do it?

 Posted by Bhakti on 12/31/2009 | Category: ASP.NET Interview questions | Views: 5058
Answer:

This can be achieved any of the two ways.
You can combine these two database fields in the template column of the gridview as shown below:
<asp:GridView ID="GridView1" runat="server">

<Columns>
<asp:TemplateColumn HeaderText="Name">
<itemtemplate>
<%# Container.DataItem("EmpID") %>, <%# Container.DataItem("Name") %>
</itemtemplate>
</asp:TemplateColumn>
</Columns>
</asp:GridView>

Other way of doing the same is to perform operation at database side. Retrieve the combined value as some column and bind the same with the databound column of the gridview.
Database side(SQL):
Select empid + ‘ : ‘ + name as name from emps;
 <asp:GridView ID="GridView1" runat="server">

<Columns>
<asp:BoundField DataField="Name">
</Columns>
</asp:GridView>

More preferable way is the second one as it will be less combursome.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response