【问题标题】:grid view display 2 columns from code behind datatable binding网格视图显示来自数据表绑定后面代码的 2 列
【发布时间】:2012-09-07 13:10:10
【问题描述】:

如果 gridview 被自动生成的表格绑定在后面的代码中,如何在我的 grid view 中只显示两个列?现在它显示六列,而我只希望它显示两列?

这是我的 .aspx 页面代码:

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

这是我的代码背后的代码:

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

【问题讨论】:

  • 在gridview上设置AutoGenerateColumns="False"
  • 是的,使用AutoGenerateColumns="False",默认设置为true。

标签: c# asp.net vb.net


【解决方案1】:

忽略您拥有的数据比您需要的多的事实。将 AutoGenerateColumns 设置为 false。为需要显示的列创建 BoundColumns。示例:

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

然后将此BoundColumn添加到gridview。

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

【讨论】:

    【解决方案2】:

    默认情况下,网格将尝试显示数据源中的所有列(公共可浏览属性)。如果你只想显示两个,你应该按如下方式关闭它:

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

    【讨论】:

      【解决方案3】:

      将 AutoGenerateColumns 设置为 false 并绑定字段为:

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

      如果您愿意,您还可以在标题中应用 CssClass。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-21
        • 1970-01-01
        • 1970-01-01
        • 2018-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多