Hiding columns in GridView control

S

srinivas

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
 
P

puja

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top