Solved

How to hide a field after auto generating data columns

Posted on 2007-12-03
21
429 Views
Last Modified: 2012-08-13
How do you hide a datagrid column when its been auto generated? Specifically, I'm hiding the ID field from a SQL query that is dynamically generated.

I tried on page_load: grdPromo.Columns(1).Visible = False

But that doesn't work.

Thanks.
0
Comment
Question by:barnesco
  • 13
  • 6
  • 2
21 Comments
 
LVL 16

Expert Comment

by:anoyes
Comment Utility
You have to attach a handler to the DataGrid's OnItemDataBound event.  Give this a shot:
Protected Sub HideIDColumn(sender as object, e as datagriditemeventargs) handles datagrid.ItemDataBound
    e.Items.Cells(0).visible=false
End Sub

This assumes that the ID column is the first column.  For the second column use Cells(1) etc...
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
the most proper way to handle an ID field in a datagrid is to use the "DataKey" ...it stays a hidden value and is easier to get to the value in code:
http://aspnet.4guysfromrolla.com/articles/090402-1.aspx
Have a look at the "Determining the Clicked Row's Primary Key Field Value" section.
0
 

Author Comment

by:barnesco
Comment Utility
Neither applies, that I can tell. Since the data is not bound, there is not an "ItemDataBound" command, so I tried:

OnDataBound="HideIDColumn"

and

      Protected Sub HideIDColumn(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdPromo.DataBound
            e.Items.Cells(0).visible = False
      End Sub

But the DataBound doesn't have the same signature.
0
 

Author Comment

by:barnesco
Comment Utility
I'm sorry, I think I miss-wrote the question. I'm using a DATAVIEW not a DATAGRID, which explains why I can't use the ItemDataBound.
0
 

Author Comment

by:barnesco
Comment Utility
Actually, it's GRIDVIEW...
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
with gridviews, MS changed the ItemDatabound event to Rowdatabound.
however, key fields are still best handled using datakeys...
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx
0
 

Author Comment

by:barnesco
Comment Utility
I've looked at the link, but that shows how to find a specific key in a row.

I need to hide the entire column, with the header.
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
it's very common with a grid to need a primary key and not want to display it.
MS built the "datakey" for this purpose.
you don't need to hide it because it is not visible
0
 

Author Comment

by:barnesco
Comment Utility
Ok, I think I understand now. The SQL DataId doesn't need to delivered because the datakey will be what determines what row is selected, which inturn, passes the key to a details view.

Is that correct?
0
 

Author Comment

by:barnesco
Comment Utility
No, what I wrote above is not right. I still need to import the StoreID. The datakey reads the data value, which is the StoreID.

How would I select all of the data keys for all of the records so that I can set them to not be visible?

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:barnesco
Comment Utility
I have a master/detail scenario. Someone needs to show me how "you don't need to hide it because it is not visible" when I need the StoreID for the detail.

Thanks.
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
I guess I'm still not understanding something.
If your datakey is set to StoreID, it is not visible, but it is available for you to use.
You know which row it is because a grid is a zero-based index (first row is row 0, second row is row 1)
When you click on a row to show the "details" you have captured which row was clicked and then you get the datakey based on that row index.
Are you using ASP.Net 2.0 and a gridview or ASP.Net 1.1x and a datagrid?
0
 

Author Comment

by:barnesco
Comment Utility
Yes, the datakey is set to StoreID, and the grid is dynamic (auto generated), so it generates all the rows sent by the sqldatasource at runtime (see snippit). All columns are visibile, and I cannot set the visibility of any column--that what I need to do--set the StoreID column to not be visible when the grid is auto generated.

I am using a gridview in ASP.NET 2.0, and you're right when saying that when I click on a row, I capture which row was clicked and I get the datakey based on that row index.
<asp:GridView ID="grdPromo" runat="server" AllowPaging="True" AllowSorting="True" BackColor="LightGoldenrodYellow"

	BorderColor="Tan" BorderWidth="1px" CellPadding="2" CssClass="normal" DataKeyNames="StoreID" DataSourceID="PromoGrid"

	ForeColor="Black" GridLines="None" HorizontalAlign="Center" OnSelectedIndexChanged="grdPromo_SelectedIndexChange"

	PageSize="7" Width="100%">

	<FooterStyle BackColor="Tan" />

	<Columns>

		<asp:CommandField CausesValidation="False" InsertVisible="False"

			ShowSelectButton="True" />

	</Columns>

	<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

	<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" CssClass="normal" />

	<HeaderStyle BackColor="Tan" Font-Bold="True" />

	<AlternatingRowStyle BackColor="PaleGoldenrod" />

	<RowStyle HorizontalAlign="Center" />

</asp:GridView>

Open in new window

0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
barnesco,

My apologies, I completely missed the fact that you're using autogenerated columns (even though you said so in the title of the question!)...(although I maintain that "proper" grid coding would use the datakey)

anoyes actually had you on the right track, for 2.0, you need to use the rowdatabound event to hide the column

    Protected Sub grdPromo_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        e.Row.Cells(1).Visible = False
    End Sub
0
 

Author Comment

by:barnesco
Comment Utility
That looks right, but I'm receiving the error message:

Method 'Protected Sub grdPromo_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)' does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)

OnDataBound="grdPromo_RowDataBound"
 

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

		e.Row.Cells(1).Visible = False

	End Sub

Open in new window

0
 
LVL 16

Assisted Solution

by:anoyes
anoyes earned 100 total points
Comment Utility
The problem is that OnDataBound="grdPromo_RowDataBound" needs to be OnRowDataBound="grdPromo_RowDataBound", but you actually don't need that.  The Handles grdPromo.RowDataBound part at the end of your sub automatically wires up to that RowDataBound event, so you don't have to specify it in the control as well.
0
 

Author Comment

by:barnesco
Comment Utility
Anoyes, thanks. Now I'm receiving the error:

Specified argument was out of the range of valid values. Parameter name: index

If I set the cells(x) to 0, the "Select" row disappears. Any other value causes the above to appear.
0
 

Author Comment

by:barnesco
Comment Utility
However, you might be on to something. If I set code to

e.Row.Visible = False

All the rows disappear. If there's a way to get the ordinal column to disappear, we're there.
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 400 total points
Comment Utility
>>If I set the cells(x) to 0, the "Select" row disappears. Any other value causes the above to appear.

that would only be true if you have only one column(?)...what I posted, i did test against Northwind...
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" DataKeyNames="EmployeeID"
            DataSourceID="AccessDataSource1" EmptyDataText="There are no data records to display."  OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:CommandField CausesValidation="False" InsertVisible="False" ShowSelectButton="True" />
            </Columns>
        </asp:GridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="App_Data\Nwind.mdb"
            SelectCommand="SELECT `EmployeeID`, `LastName`, `FirstName`, `Title`, `TitleOfCourtesy`, `BirthDate`, `HireDate`, `Address`, `City`, `Region`, `PostalCode`, `Country`, `HomePhone`, `Extension`, `Photo`, `Notes`, `ReportsTo` FROM `Employees`">
        </asp:AccessDataSource>

code:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        e.Row.Cells(1).Visible = False
    End Sub
0
 

Author Comment

by:barnesco
Comment Utility
Found the problem: It's in the paging. The above works, but add AllowPaging="True" and the I receive an "index out of range" error.

What's the fix for that?
0
 

Author Closing Comment

by:barnesco
Comment Utility
I think we bird-dogged this the best we can. Thank you for being persistent. The question was answered about how to have a row disappear when it's auto generated. The fact that an error is thrown when paging is involved is another question.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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 video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

9 Experts available now in Live!

Get 1:1 Help Now