In this article I will explain with an example, how to display multiple data Fields (Columns) in GridView BoundField in ASP.Net.
By default, the BoundField column of GridView does not allow to show multiple data Fields (Columns), thus the solution is to use TemplateField or using the RowDataBound event, multiple data Fields (Columns) can be displayed in BoundField column of GridView.
HTML Markup
The following HTML Markup consists of an ASP.Net GridView with three BoundField columns. The second BoundField column will display two data fields.
The GridView is also assigned RowDataBound event handler.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Customer Id" ItemStyle-Width="90" />
<asp:BoundField DataField="" HeaderText="Name" ItemStyle-Width="120" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the ASP.Net GridView control
The GridView is populated with a dynamic DataTable with some dummy data inside the Page Load event. The FirstName and LastName columns will be concatenated and displayed in the Name BoundField column of GridView.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("FirstName"), new DataColumn("LastName"), new DataColumn("Country") });
dt.Rows.Add(1, "John", "Hammond", "United States");
dt.Rows.Add(2, "Mudassar", "Khan", "India");
dt.Rows.Add(3, "Suzanne", "Mathews", "France");
dt.Rows.Add(4, "Robert", "Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Id"), New DataColumn("FirstName"), New DataColumn("LastName"), New DataColumn("Country")})
dt.Rows.Add(1, "John", "Hammond", "United States")
dt.Rows.Add(2, "Mudassar", "Khan", "India")
dt.Rows.Add(3, "Suzanne", "Mathews", "France")
dt.Rows.Add(4, "Robert", "Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Display multiple data Fields (Columns) in GridView BoundField in ASP.Net
Inside the RowDataBound event handler of GridView, the FirstName and LastName column values are pulled from the DataItem object and displayed in the second column i.e. Name BoundField column of GridView.
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Text = string.Format("{0} {1}", DataBinder.Eval(e.Row.DataItem, "FirstName"), DataBinder.Eval(e.Row.DataItem, "LastName"));
}
}
VB.Net
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(1).Text = String.Format("{0} {1}", DataBinder.Eval(e.Row.DataItem, "FirstName"), DataBinder.Eval(e.Row.DataItem, "LastName"))
End If
End Sub
Screenshot
Demo
Downloads