daidai
daidai

Reputation: 541

Cant see grid view in asp.net application

This is part of my .aspx page

<div>
    <asp:GridView ID="gvPredstave" runat="server" CssClass="gridview" AlternatingRowStyle-CssClass="even">
    </asp:GridView>
</div>

and this is my codebehind

OracleCommand cmd = new OracleCommand();
        OracleDataReader reader;
        cmd.Connection = conn;
        DataTable table = new DataTable();

        cmd.CommandText = "select ime_predstave from predstava";
        cmd.CommandType = CommandType.Text;
        reader = cmd.ExecuteReader();

        table.Load(reader);
        gvPredstave.DataSource = table;
        gvPredstave.DataBind();

When i start my App, i dont see gridview.Any help?

Upvotes: 0

Views: 1140

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34846

You need to set the AutoGenerateColumns property in your grid view markup, like this:

<asp:GridView ID="gvPredstave" runat="server" CssClass="gridview" AlternatingRowStyle-CssClass="even" AutoGenerateColumns="True">
</asp:GridView>

Note: No matter what is or is not coming back from the database will not show, because you have not told the grid view "what" to display. Setting the AutoGenerateColumns to True instructs the grid view to take whatever columns come back from the database and make them as columns in your grid, so you will get potentially less than appealing column headers like SYSTEM_ID for example.

To take control of which columns you want displayed in your grid view and what the names of those columns should be, then you need to declare the columns using the Columns property of the grid view, like this:

<asp:GridView ID="gvPredstave" runat="server" CssClass="gridview" AlternatingRowStyle-CssClass="even">
    <Columns>
        <asp:boundfield datafield="SYSTEM_ID" headertext="System ID"/>
    </Columns>
</asp:GridView>

Note: When explicitly declaring the Columns property, make sure to remove the AutoGenerateColumns property so you do not get both things rendering, one value does not trump the other; you will get both.

For more information about the GridView's Columns property read the MSDN documentation for GridView.Columns Property

Upvotes: 1

Related Questions