2013-04-17 75 views
0

我有一個GridView,它工作正常。但是當我沒有結果時,標題消失。有沒有辦法顯示它沒有代碼隱藏?即使結果爲null時,也會顯示gridview的標題ASP.NET

我使用3.5

<asp:DropDownList ID="DropDownList1" 
runat="server" 
AutoPostBack="True" 
DataSourceID="SqlDataSource1" 
DataTextField="Categorie" 
DataValueField="Cat_ID" 
AppendDataBoundItems="True"> 

</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource1" 
runat="server" 
ConnectionString="<%$ ConnectionStrings:Goed %>" 
SelectCommand="SELECT * FROM [tbl_Cat]"> 
</asp:SqlDataSource> 
+1

請發表您的代碼 – nmat

+0

編輯它;)對不起忘了它 – DiederikEEn

回答

0

更新您的sql查詢以返回空值情況。

if not exists (SELECT * FROM [tbl_Cat]) 
select ' ' as Categorie,' ' as catid 
else 
select SELECT * FROM [tbl_Cat] 

將在所有框架工作VERSON

另一個選擇是重寫數據綁定方法,您可以檢查數據表。如果表格行數爲0,則可以手動插入空白值,然後進行數據綁定。

0

設置以下屬性,GridView中......

EmptyDataText="There are no crecords." 

或設置此模板

<EmptyDataTemplate> 
    No data found! 
</EmptyDataTemplate> 
+0

這只是顯示文本「沒有記錄」。它不會顯示頭文件 – DiederikEEn

+0

setdatasource as string.empty; –

0

使用屬性

ShowHeaderWhenEmpty="True" 

你代碼的變化

<asp:DropDownList ID="DropDownList1" 
runat="server" 
AutoPostBack="True" 
DataSourceID="SqlDataSource1" 
DataTextField="Categorie" 
DataValueField="Cat_ID" 
ShowHeaderWhenEmpty="True" 
AppendDataBoundItems="True"> 

3.5以下鏈接 http://www.aspdotnet-suresh.com/2010/12/v-behaviorurldefaultvmlo.html

+0

是不是4.0?因爲它不工作 – DiederikEEn

+0

是它的工作正常在4.0 –

+0

檢查此鏈接 - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.showheaderwhenempty.aspx –

0

對於.NET 3.5使用

<EmptyDataTemplate> 
<table> 
<tr> 
<th>Column 1</th> 
<th>Column 2</th> 
</tr> 
<tr> 
<td colspan="2">No Data found..</td> 
</tr>  
</table> 
</EmptyDataTemplate> 

就我而言,這將是最簡單的方法。

相關問題