2013-06-26 33 views
0

我有一個網格視圖。如何在頁面中沒有任何數據時看到列標題?
我試試這個:如何顯示空的gid視圖?

<Empty Data Template> 
     no data. 
</Empty Data Template> 

但這不是我想要的。我想顯示我的網格視圖的列,即使沒有數據。請幫幫我!

+0

你有headertemplate嗎? –

+0

是的,我有。 – Angel

回答

1

試試這個

1)添加ShowHeaderWhenEmpty="True"到你的GridView

<asp:GridView ShowHeaderWhenEmpty="true" runat="server" /> 

2)你的GridView必須綁定到數據源(至少,一個空的集合)爲空模板顯示

GridView1.DataSource = [Some DataSource Here]; 
    GridView1.DataBind(); 
+0

「添加ShowHeaderWhenEmpty =」真「到您的網格視圖」!怎麼做? – Angel

+1

'''' – codingbiz

1

如果您的目標是c#4.0+,您可以使用ShowHeaderWhenEmpty屬性,否則,您需要添加一些自定義代碼。

0

在這裏你會找到你需要的東西:

http://msdn.microsoft.com/pt-br/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx

我試過了,它的工作原理!

<asp:gridview id="CustomersGridView" 
    datasourceid="CustomersSqlDataSource" 
    autogeneratecolumns="true" 
    runat="server"> 

    <emptydatarowstyle backcolor="LightBlue" 
     forecolor="Red"/> 

    <emptydatatemplate> 

     <asp:image id="NoDataImage" 
     imageurl="~/images/Image.jpg" 
     alternatetext="No Image" 
     runat="server"/> 

     No Data Found. 

    </emptydatatemplate> 

    </asp:gridview> 
+0

我以前是這樣做的,但這不是我想要的。我將在網格視圖中顯示我的列的標題。 – Angel

0

你有2種方法來做到這一點:

1,通過模擬輸入字段內

第一個單元格 第二單元 第三個單元 2-Is創建空DataSet並將其綁定到GirdView。

如果ds.Tables(0).Rows.Count> 0然後 grd_codes.DataSource = DS grd_codes.DataMember = ds.Tables(0).TableName

 grd_codes.DataBind() 

    Else 
     Try 
      If ds.Tables(0).Rows.Count = 0 Then 

       ds.Tables(0).Rows.Add(ds.Tables(0).NewRow()) 
       grd_codes.DataSource = ds 
       grd_codes.DataBind() 
       Dim columnCount As Integer = grd_codes.Rows(0).Cells.Count 
       grd_codes.Rows(0).Cells.Clear() 
       grd_codes.Rows(0).Cells.Add(New TableCell) 
       grd_codes.Rows(0).Cells(0).ColumnSpan = columnCount 
       grd_codes.Rows(0).Cells(0).Text = "No Records Found." 

      End If 

我更喜歡第一方式,因爲綁定空的DataSet有一些問題。