Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Datagrid Column at Runtime

Status
Not open for further replies.

Isadore

Technical User
Feb 3, 2002
2,167
US
After much searching here at the ASP.NET forum I found only partial solutions to removing Datagrid columns at runtime. The most common solution would remove the column, causing a frame shift, or leaving the Column Header intact with the cells below it inivisible.

The following solution worked well and I thought I would post this for those who might be trying to accomplish the same task but having difficulties.

To remove both the header and underlying column of your Datagrid at runtime (set to Autogenerate=false) use the following code in the ItemDataBound event handle:
Code:
Sub dgChemData_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then 
 If yourcondtion Then
  e.Item.Cells.Remove(e.Item.Cells(0))
 End If
End If

..followed by turning off the Header for the same column:

If e.Item.ItemType = ListItemType.Header
 e.Item.Cells.Remove(e.Item.Cells(0)) 
End If

End Sub

The Datagrid used the nominal <asp:BoundColumn.. format.
 
Of course the Header code should also contain the IF..THEN conditional statement (forgot to add that).
 
Probably should have completely finished the problem before posting - one final note. While removing these columns and headers an interesting phenomenom occurred.

As you remove the first column and header the second column and header take on a new zero (0) based column number resulting from and as a result of the previous deletion.

For example, if Column X is Column(7) prior to the first deletion (if you are deleting more than one column) the second deletion would have to be coded as Column(6) as new Column numbers are generated for each deletion event. FYI
 
One final phenomenon to take care of. I found that after I deleted columns that internal css statements such as "width" were lost and hard to recover in the remaining columns. For example, if the 8th Column of the Datagrid had the following:
Code:
<asp:BoundColumn DataField="ContactNames" ItemStyle-width="150px" HeaderText="Monitors"/>
..a deletion of a column would cause this width statement to be "lost" and no longer applicable - not surprisingly. A simple solution, and nothing new here I am sure, is to do ascribe a css class and apply during the Itembound event.
Code:
<Head>
<style type="text/css">
 .cellClass{ width: 150;}
</style>
</Head>
..followed by reference in the ItemDataBound event:
Code:
..
Dim tblCell As TableCell = DirectCast(e.Item.Cells(9), TableCell)
tblCell.CssClass = "cellClass"
..
In this case I knew after deleting 2 Columns from the Grid, that the width of the 10th Column had to be set at a certain value; and so this value can be referenced to the Cell number and not directly inside the object itself.
 
Actually the title of this thread should have been "deleting a Datagrid column" -- not many here would search on the word "remove" -- so I add this last comment so that in a search it may be found using "delete datagrid column" which would be expected.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top