Solved

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

Posted on 2006-11-09
10
187 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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