Is there a way to get the GridView object to show, even though there's no data?
Is your problem that the empty data row with text "No data available." does not appear although EmptyDataText="No data available."
is set?
Note that the empty row and headers will not appear unless DataBind() is called with something other than null. I guess your code does not include it.
Shown below is a sample which can show the empty data row and headers when there is no data:
.aspx.cs
using System;
using System.Collections.Generic;
namespace WebForms1
{
public partial class WebForm56 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Note that the empty row and headers will not appear
// unless DataBind() is called with something other than null.
GridView1.DataSource = new List<string>();
GridView1.DataBind();
}
}
}
.aspx
<%@ Page Title="" Language="C#"
MasterPageFile="~/Site.Master"
AutoEventWireup="true"
CodeBehind="WebForm56.aspx.cs"
Inherits="WebForms1.WebForm56" %>
<%@ Register Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1"
ContentPlaceHolderID="HeaderContent"
runat="server">
<style type="text/css">
.modalBackground {
background-color: Gray;
filter: alpha(opacity=70);
opacity: 0.7;
}
.modalPopup {
background-color: White;
}
</style>
</asp:Content>
<asp:Content ID="Content2"
ContentPlaceHolderID="MainContent"
runat="server">
<asp:ModalPopupExtender ID="ModalPopupExtender1"
runat="server"
TargetControlID="Button1"
PopupControlID="Panel1"
BackgroundCssClass="modalBackground"
OkControlID="Button2"
CancelControlID="Button3">
</asp:ModalPopupExtender>
<asp:Button ID="Button1"
runat="server" Text="Show ModalPopup" />
<asp:Panel ID="Panel1"
runat="server"
CssClass="modalPopup">
<%-- ShowHeaderWhenEmpty property available on .NET 4+ --%>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
EmptyDataText="No data available."
ShowHeaderWhenEmpty="true">
<Columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
</Columns>
<EmptyDataRowStyle HorizontalAlign="Center" />
</asp:GridView>
<p style="text-align: center;">
<asp:Button ID="Button2" runat="server" Text="OK" />
<asp:Button ID="Button3" runat="server" Text="Cancel" />
</p>
</asp:Panel>
</asp:Content>
Result