I’m adding a GridView & then showing data in it from a SQL Server database. The problem is that the GridView is not displaying in the browser with or without data.
Here is my code:
<asp:GridView ID="GridAllStore" runat="server" AutoGenerateColumns="False" Width="100%" ViewStateMode="Enabled">
public partial class AdminPanel : System.Web.UI.Page
{
storelocatorDataSetTableAdapters.storedbTableAdapter tastore = new storelocatorDataSetTableAdapters.storedbTableAdapter();
storelocatorDataSetTableAdapters.View_1TableAdapter taview = new storelocatorDataSetTableAdapters.View_1TableAdapter();
List<storelocatorDataSet.storedbRow> lststore = new List<storelocatorDataSet.storedbRow>();
List<storelocatorDataSet.View_1Row> lstview = new List<storelocatorDataSet.View_1Row>();
protected void Page_Load(object sender, EventArgs e)
{
lstview = taview.GetData().ToList();
GridAllStore.DataSource = lstview;
}
}
I think the problem is that you haven’t defined any columns to display. You have to explicitly define the columns when you set
AutoGenerateColumnsto false.To make sure that the basics are working set
AutoGenerateColumnsto true:With
AutoGenerateColumnsset to true, the datasource assigned, andDataBind()called, you should start seeing some data. Once you start seeing the data, you can define the specific columns you want to display.Since you only need to bind the grid on the first page load, utilize the
!Page.IsPostBackcondition: