Solved

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

Posted on 2006-11-09
10
182 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
Comment Utility
Put it on the OnRowDataBound event of the grid and check for RowType = Header
0
 
LVL 1

Author Comment

by:samme
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:samme
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now