本文介绍了[解决]如何在gridview中将数据库中的两列显示为单列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在网格视图中将数据库中的FirstName和LastName等列显示为名称列。

I wanted to display columns such as FirstName and LastName from DB as Name column in gridview.

推荐答案

SELECT firstname + ' ' +  lastname  as 'Name' from table


<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>


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")) ;


这篇关于[解决]如何在gridview中将数据库中的两列显示为单列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:44