Hiding columns in GridView control

Hiding columns in GridView control

Post by c3Jpbml2YX » Fri, 04 Aug 2006 06:04:47


Hi,
I have binded a dataset to a gridview control at run time.
I'm fetching 5 columns from the database.
But while displaying it on the screen i want to show only 3 columns.
I tried with gridview.columns(i).visible property,but it did't work.

Thanks in advance
Srinivas
 
 
 

Hiding columns in GridView control

Post by puja » Sat, 05 Aug 2006 11:28:12

hi first of all, go to columns property of grid view and the checkbox called
"autogenerate fields should not be ticked.

if u see below example then although am fetching 5 columns in dataset, am
displaying only 3 namely property address, sales person and date requested.

hope this helps.

<asp:GridView ID="grdContract" runat="server" AllowSorting="True"
AutoGenerateColumns="False"

BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px"
DataKeyNames="File Number"

EmptyDataText="No Contracts Found" Height="189px"
OnRowCommand="GetMoreDetails"

OnRowCreated="HighlightRow" OnSorting="SortGrdContract" Width="100%"
CssClass="customGrid">

<Columns>

<asp:TemplateField HeaderText="Property Address" SortExpression="Property
Address">

<ItemTemplate>

<asp:Label ID="lblPropertyAddress" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Property Address") %>'>

</asp:Label>

</ItemTemplate>

<ControlStyle Width="180px" />

<ItemStyle HorizontalAlign="Left" />

</asp:TemplateField>

<asp:TemplateField HeaderText="Sales Person" SortExpression="Sales Person">

<ItemTemplate>

<asp:Label ID="lblSalesPerson" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Sales Person") %>'>

</asp:Label>

</ItemTemplate>

<ControlStyle Width="120px" />

</asp:TemplateField>

<asp:TemplateField HeaderText="Date Requested" SortExpression="Date
Requested">

<ItemTemplate>

<asp:Label ID="lblDateRequested" runat="server" Text='<%#
String.Format("{0:dd/MM/yy}", Convert.ToDateTime(DataBinder.Eval(Container,
"DataItem.Date Requested")))%>'>

</asp:Label>

</ItemTemplate>

</asp:TemplateField>

</asp:GridView>