2012-09-07 31 views
0

如果gridview在自動生成的表格後面的代碼中綁定,我如何僅在網格視圖中顯示兩個柱面?現在它顯示六列,我只希望它顯示兩個?網格視圖顯示從數據表綁定後面的代碼中的2列

這是我的.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 
+1

在gridview上設置'AutoGenerateColumns ='False''。 – user1429080

+0

是默認使用'AutoGenerateColumns =「False」'它被設置爲true。 – Mourya

回答

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; 
+0

完美謝謝 –

1

默認情況下,網格將嘗試顯示數據源中的所有列(公共可瀏覽屬性)。如果你想顯示只有兩個,你應該關閉這個功能如下:

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

集的AutoGenerateColumns虛假和綁定字段爲:

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

相關問題