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

How to hide a field after auto generating data columns

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
barnesco
Asked:
barnesco
  • 13
  • 6
  • 2
2 Solutions
 
anoyesCommented:
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
 
samtran0331Commented:
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
 
barnescoAuthor Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
barnescoAuthor Commented:
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
 
barnescoAuthor Commented:
Actually, it's GRIDVIEW...
0
 
samtran0331Commented:
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
 
barnescoAuthor Commented:
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
 
samtran0331Commented:
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
 
barnescoAuthor Commented:
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
 
barnescoAuthor Commented:
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
 
barnescoAuthor Commented:
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
 
samtran0331Commented:
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
 
barnescoAuthor Commented:
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
 
samtran0331Commented:
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
 
barnescoAuthor Commented:
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
 
anoyesCommented:
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
 
barnescoAuthor Commented:
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
 
barnescoAuthor Commented:
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
 
samtran0331Commented:
>>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
 
barnescoAuthor Commented:
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
 
barnescoAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 13
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now