kcwallace Posted May 5, 2006 Posted May 5, 2006 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. Quote Go Beavs!!!
tfowler Posted May 5, 2006 Posted May 5, 2006 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 Quote
kcwallace Posted May 5, 2006 Author Posted May 5, 2006 moving the Visible statement has no effect. The error still occurs. Quote Go Beavs!!!
tfowler Posted May 5, 2006 Posted May 5, 2006 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.