• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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

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
samme
Asked:
samme
  • 4
  • 3
1 Solution
 
kraffayCommented:
Put it on the OnRowDataBound event of the grid and check for RowType = Header
0
 
sammeAuthor Commented:
i actually tried putting it in the rowdatabound event with no luck so far.

What do i actually do with the RowType= Header?
0
 
kraffayCommented:
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
Industry Leaders: 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!

 
sammeAuthor Commented:
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
 
kraffayCommented:
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
 
sammeAuthor Commented:
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
 
kraffayCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now