Solved

How to hide a field after auto generating data columns

Posted on 2007-12-03
21
431 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
ID: 20399143
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
ID: 20399191
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
ID: 20399393
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:barnesco
ID: 20399670
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
ID: 20399676
Actually, it's GRIDVIEW...
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 20399900
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
ID: 20400263
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
ID: 20400638
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
ID: 20407991
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
ID: 20408264
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
 

Author Comment

by:barnesco
ID: 20408354
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
ID: 20410786
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
ID: 20411556
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
ID: 20411975
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
ID: 20416800
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
ID: 20417048
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
ID: 20417105
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
ID: 20417129
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
ID: 20419056
>>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
ID: 20420113
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
ID: 31412447
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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