Hello-World
Hello-World

Reputation: 9555

grid view display 2 columns from code behind datatable binding

How do I only display two colums in my grid view if the gridview is bound in the code behind by an auto generated table? Right now it displays six columns when I only want it to display two?

Here is my .aspx page code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">

                <columns>
          <asp:boundfield datafield="Drug" headertext="ddddrug"/>
          <asp:boundfield datafield="date" headertext="ddddate"/>

        </columns>


        </asp:GridView>
    </div>
    </form>
</body>
</html>

Here is my code behind code:

Imports System.Data

Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ' auto generated table
        Dim table2 As New DataTable
        ' Create four typed columns in the DataTable.
        table2.Columns.Add("ID", GetType(Integer))
        table2.Columns.Add("Drug", GetType(String))
        table2.Columns.Add("Patient", GetType(String))
        table2.Columns.Add("Date", GetType(DateTime))
        ' Add five rows with those columns filled in the DataTable.
        table2.Rows.Add(25, "Indocin", "David", DateTime.Now)
        table2.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
        table2.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
        table2.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
        table2.Rows.Add(1100, "Dilantin", "Melanie", DateTime.Now)
        table2.Rows.Add(125, "Indocin", "David", DateTime.Now)
        table2.Rows.Add(150, "Enebrel", "Sam", DateTime.Now)
        table2.Rows.Add(110, "Hydralazine", "Christoff", DateTime.Now)

        GridView1.DataSource = table2

        GridView1.DataBind()

    End Sub

    End Sub
End Class

Upvotes: 1

Views: 3513

Answers (3)

4b0
4b0

Reputation: 22323

Set AutoGenerateColumns false and bound field as:

  <asp:BoundField DataField="Drug"  HeaderText="ddddrug" >
        <HeaderStyle CssClass="Your class" />
  </asp:BoundField>
  <asp:BoundField DataField="date" DataFormatString="{0:yyyy/MM/dd}"  HeaderText="ddddate" >
        <HeaderStyle CssClass="Your class" />
  </asp:BoundField>

If you want you also apply CssClass in your header.

Upvotes: 1

nunespascal
nunespascal

Reputation: 17724

By default the grid will try to display all the columns(public browseable properties) in the DataSource. If you want to show only two, you should turn this off as follows:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
       <columns>
          <asp:boundfield datafield="Drug" headertext="ddddrug"/>
          <asp:boundfield datafield="date" headertext="ddddate"/>
       </columns>
</asp:GridView>

Upvotes: 1

harshit
harshit

Reputation: 3846

Ignore the fact that you have more data than you need. Set AutoGenerateColumns to false. Create BoundColumns for the columns you need to show. Example :

BoundColumn nameColumn = new BoundColumn();
nameColumn.DataField = "Drug";
nameColumn.DataFormatString = "{0}";
nameColumn.HeaderText = "Drug";

Then add this BoundColumn to gridview.

GridView1.Columns.Add(nameColumn);
GridView1.AutoGenerateColumns = false;

Upvotes: 2

Related Questions