在分配具有数据的数据表后,Gridview是否为空数据?

时间:2013-01-18 19:52:04

标签: asp.net gridview

我有DataTable的数据。分配给gridview,但gridview在分配后有0行。

         Dim mKeywordSearch As New KeywordSearch
         Dim dtExcel As DataTable
         dtExcel = mKeywordSearch.DatabaseKeywordSearch(txtKeyword.Text)
         gvKeywordSearch.DataSource = dtExcel
         gvKeywordSearch.DataBind()
         Dim cnt As Integer = dtExcel.Rows.Count
         cnt = gvKeywordSearch.Rows.Count()

.aspx page

<asp:GridView ID="gvKeywordSearch" runat="server" Width="100%" 
AutoGenerateColumns="false" Visible="true"></asp:GridView>

1 个答案:

答案 0 :(得分:2)

将GridView更改为AutoGenerateColumns="true"或根据需要使用AutoGenerateColumns="false"

定义列
<asp:GridView ID="gvKeywordSearch" runat="server" Width="100%" 
AutoGenerateColumns="true" Visible="true"></asp:GridView>

AutoGenerateColumns = "false"

<asp:GridView ID="gvKeywordSearch" runat="server" Width="100%" 
    AutoGenerateColumns="false" Visible="true">
   <Columns>
      <asp:BoundField HeaderText="HeaderText1" DataField="FieldName1" ></asp:BoundField>
      <asp:BoundField HeaderText="HeaderText2" DataField="FieldName2" ></asp:BoundField>
      ...
   </Columns>
</asp:GridView>