1

I'm using a list of objects as the data source of my GridView and when I set columns to not be visible the updates fail because the value of those columns is changed to null (and the column doesn't allow nulls). The values do exist when the columns are visible but I really don't want to display these columns because, for the most part, they are ID columns that the user doesn't really need to see.

EDIT: I've tried the hidden field option but it still sets the value to null. I've looked at the page source and the hidden field exists with the appropriate value...

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139

5 Answers5

3

I found this solution to simulate hidden columns in .Net 2.0:

Implement the GridView.RowCreated event.

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[1].Visible = false;
    e.Row.Cells[2].Visible = false;
}

Here's the link: http://www.beansoftware.com/ASP.NET-Tutorials/GridView-Hidden-Column.aspx

I guess in 2.0 when a column is not visible the databinding fails for that column but this method hides after the link has been established so it tricks the system (?).

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
1

Microsoft recommends using the DataKeyNames property of the GridView control.

Instead of using code-behind to hide certain columns, you can just remove the bound fields from the GridView and specify them in the DataKeyNames property:

<asp:GridView ID="GridView1" runat="server" 
        DataKeyNames="SalesOrderID,SalesOrderDetailID"
        DataSourceID="LinqDataSource1">

This way the fields will not show to the user but the GridView knows to keep the values around for updating, etc.

YeahStu
  • 4,032
  • 5
  • 48
  • 69
0

When a field inside GridView is made invisible, its cell values are not more accessible or these are null or empty.

In order to solve this issue, you just have to assign column names (Hidden Fields) to DataKeyNames property of GridView by doing DataKeyNames="colName1,colName2,colName3".

Then access their cell values as cellValue = GridView1.DataKeys[0]["ID"].ToString();

I have written a simple post demonstrating the solution to your problem at here.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Awais Hafeez
  • 1,070
  • 5
  • 9
0

If you aren't doing so already, I'd consider using Template columns for your data and do "manual" data binding (either "inline" or in the code behind page using the RowDataBound event). That way you can test for DBNull and simply ignore putting a value in the column if the value is NULL. This will also allow for the columns to be properly hid.

Dillie-O
  • 29,277
  • 14
  • 101
  • 140
  • Are you saying that if a column is invisible, its value by default is null? – Austin Salonen Jan 06 '09 at 15:20
  • No, I'm saying that the "auto binding" is expecting a value and if it encounters DBNull, it'll give you the problem you're having. It sounds like the databinding is attempting to occur even if the column is invisible. Having the "manual" binding will still allow the binding to occur, just w/no data. – Dillie-O Jan 06 '09 at 15:26
  • The data is not DBNull but yet when it goes to update, the values are null when they are not visible (it's doing the same with Hidden fields). – Austin Salonen Jan 06 '09 at 17:00
0

You could do it with hidden fields for the values you don't want to display. that way you can still use the same databinding and other functions as you do today.

Richard L
  • 1,211
  • 7
  • 10