PDA

View Full Version : Datagrid Column Display


Shaitan00
02-13-2005, 02:15 AM
Given a Datagrid [dgStatus] and an associated datasource [dataset ds] in C# [ASP.NEt]
dgStatus.DataSource = ds;
dgStatus.DataBind();

The Datasource [ds] contains 5 columns (Name, Pos, X, Y, Z) however I only want to display the first two columns in the Datagrid.

Therefore I did the following in my .aspx page:
<asp:datagrid id="dgStatus" runat="server">
<columns>
<asp:BoundColumn HeaderText="Device Name" DataField="Name"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Device Position" DataField="Pos"></asp:BoundColumn>
</columns>
</asp:datagrid><br>

However this does not deliver the desirable results.
Intead of 2 columns [with headers "Device Name" and "Device Position"] I get 7 columns [with headers "Device Name" and "Device Position" (correctly bound) and the 5 from the original Datasource [Name, Pos, X, Y, Z].

So all I want to see is the 2 databound columns with the appropriate headers and not the additional 5 columns. Any clues?

miranda
02-14-2005, 06:40 AM
You forgot to tell the datagrid NOT to autogenerate the columns. Default is for the columns to be automatically generated by all columns inside the database. So when you want to display a datagrid and limit which columns are displayed put this inside your datagrid control AutoGenerateColumns="False"

Like so:

<asp:datagrid id="dgStatus" AutoGenerateColumns="False" runat="server">
<columns>
<asp:BoundColumn HeaderText="Device Name" DataField="Name"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Device Position" DataField="Pos"></asp:BoundColumn>
</columns>
</asp:datagrid>