Solved

Gridview code done.... now where do i place it

Posted on 2006-11-09
10
186 Views
Last Modified: 2008-01-09
What i have done is created a search page that after going through and entering all the criteria it makes it the select command for the datasource (ex: sqldatasource.selectcommand = "Select...from...where...")

My gridview1 then uses this to populate the gridview. I now need it to make the id column invisible while also centering all of the infomation. I am pretty sure that this coding is correct.  

The problem is when i test it the value for gridview1.columns.count = 2 when i know there are more columns. so the big question is where do i place this code to return the correct column count?

For i = 0 To GridView1.Columns.Count - 1
            If GridView1.Columns(i).HeaderText = "id" Then
                GridView1.Columns(i).Visible = False
            End If
           
            GridView1.Columns(i).ItemStyle.HorizontalAlign = HorizontalAlign.Center
Next
 
0
Comment
Question by:samme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
10 Comments
 
LVL 9

Expert Comment

by:kraffay
ID: 17909706
Put it on the OnRowDataBound event of the grid and check for RowType = Header
0
 
LVL 1

Author Comment

by:samme
ID: 17909749
i actually tried putting it in the rowdatabound event with no luck so far.

What do i actually do with the RowType= Header?
0
 
LVL 9

Expert Comment

by:kraffay
ID: 17909817
OnRowDataBound fires each time a row is rendered, including the header and footer.  That is the time to grab a handle to any controls in row and set their properties.  But I think we are on the wrong track here...

Can you post the mark up?  

I think all you need to do here is turn off AutoGenerate columns and set the bound column visible property to false for the ID column.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:samme
ID: 17909902
If i try and turn off autogenerate columns nothing  shows up but select and edit since the columns are generated at runtime. Here is what i have for the gridview and data source. the selectcommand is done at runtime depending on what they selected. and since it is not done until runtime i can't go and set id to false from the edit columns within visual studio

<asp:SqlDataSource ID="SearchGridview" runat="server" ConnectionString="<%$ ConnectionStrings:LCSCMConnectionString %>" >
 <asp:SqlDataSource>

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SearchGridview" CellPadding="4" ForeColor="#333333" GridLines="None" Visible="False">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:CommandField ShowEditButton="True" />
         </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
        <EditRowStyle BackColor="#2461BF" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
0
 
LVL 9

Expert Comment

by:kraffay
ID: 17909961
You'll have to add your bound columns to the markup like this (I added the horizontal style to the GridView properties for centering):

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SearchGridview" CellPadding="4" ForeColor="#333333" GridLines="None" Visible="False" HorizontalAlign="Center">
        <Columns>
            <asp:BoundField DataField="Email" HeaderText="E-Mail Address" />
 
                 ... and so on and so on for each column ...

            <asp:CommandField ShowSelectButton="True" />
            <asp:CommandField ShowEditButton="True" />
         </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
        <EditRowStyle BackColor="#2461BF" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
0
 
LVL 1

Author Comment

by:samme
ID: 17910402
Not sure if you understand where i am going... the select statement is not decided until runtime
this part doen't exist at all since the columns are unknown until selected

<asp:BoundField DataField="Email" HeaderText="E-Mail Address" />
 
                 ... and so on and so on for each column ...


I don't know the columns that will be there until they are selected
so I can't put any columns in before this as you suggested
0
 
LVL 9

Accepted Solution

by:
kraffay earned 300 total points
ID: 17910661
This is a total hack, but if you know the ordinal of the field you want to hide (in this case the 8th col from my select), you can do this:

    Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound

        GridView1.HeaderRow.Cells(8).Visible = False

    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Me.GridView1.Controls(0).Controls(1).Controls(8).Visible = False
        End If

    End Sub
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question