Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am attempting write an Webform using ASP 2.0.

 

I need to use a DataGridView. However, I cannot seem to find any sample code to manipulate a column using code behind. Specifically, I need to programatically hide a field from the datasource.

 

I cannot remove the field from the datasource, because I need it to make decisions

 

I use the code below to build the datagridview. I get an error at the noted location. THe error states:

 

"{"Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index"}"

 

 

However, I know there are 6 fields in the dataset.

 

       With GridView1
           .DataMember = DS.Tables(0).TableName
           .DataSource = DS

           .AutoGenerateEditButton = True
           .BorderStyle = BorderStyle.Solid
           .BorderWidth = Unit.Pixel(10)
           .EmptyDataText = "No Data Added"
           .Columns(3).Visible = False 'Errors here.
           .DataBind()
       End With

 

I have the AutogenerateColumns set to TRUE. However, if I set it to FALSE, nothing shows up.

Go Beavs!!!
Posted

You probably need to move your ".Columns(3).Visible = False" after the .DataBind(). Before you bind the data to the grid, there are no columns.

 

Todd

Posted

So, if you comment out the ".Columns(3).Visible = False" statement, the GridView is displayed with 6 columns?

 

As a side note; I rarely set AutoGenerateColumns to TRUE. I always like to customize my column headers and formats, so I always manually create the columns.

 

It is definitely telling you that your DataSet does not have a 4th column. I've had some issues with the "Visible" command on columns before. I can't remember if it was with the old DataGrid, the DataGridView, or the GridView controls, but I ended up hiding the columns by setting their width = 0.

 

Todd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...