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
Solved

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

Posted on 2006-11-09
10
185 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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