PFranchise
PFranchise

Reputation: 6752

hiding columns of a datagrid (asp.net/c#)

Hey, I am trying to make my datagrid printable. To do this, I am trying to hide the final 4 columns. I have a printable button that I would like to when clicked, make those last 4 columns disappear. I have so far failed to make this work.

I have tried:

 ProductsGrid.Columns[6].ControlStyle.Width = -1;

and

 ProductsGrid.Columns[6].Visible = false;

Note: these columns do have data in them. Perhaps that is part of my issue. Also, I need the headers of the columns to disappear.

Thanks for any tips.

EDIT: I am making them invisible in my button click command. I am not using generated columns, so I think that is set to false. I got a bit fed up with this issue and left work, and won't be back till mid next week, so I might have to hold off finding the solution till then. Thanks for the comments everyone, I will look it over soon. Sorry, I can't give more feedback in a timely fashion.

Edit x2: Do have I have to handle it in some sort of postback or something?

Upvotes: 0

Views: 6059

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460018

If you have AutoGenerateColumns="True", then it does not work to make them invisible by simply set visible=False, because automatically generated bound column fields are not added to the Columns collection.

VB.Net, but i think you get the idea:

Private Sub setPrinterView()
  For Each tr As TableRow In DirectCast(Me.GridView1.Controls(0), Table).Rows
      For i As Int32 = 1 To 4
          If tr.Cells.Count - i < 0 Then Exit For
          tr.Cells(tr.Cells.Count - i).Visible = False
      Next
   Next
End Sub

If AutogenerateColumns is set to False you only need to make the Columns invisible without rebinding the Grid.

Upvotes: 2

Related Questions