0

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?

4
  • ProductsGrid.Columns[6].Visible = false; should be working. I'm not 100% sure, but I think you have to call DataBind() again to update it. Unless you re-bind the data, the controls are not updated on the page. (I'm setting this as a comment rather than an answr instead of an answer because I'm not 100% sure, and not at my Dev PC to test it.) Commented Nov 5, 2010 at 22:55
  • 1
    Where and when do you hide your columns? Making them invisible should work(without rebinding). Are you autogenerating the columns? Commented Nov 5, 2010 at 23:01
  • If that fails, it shouldn't be too difficult to just produce a second page for printing by copying and pasting code, then remove the columns from the print page in the markup, and link to it with a "Print-friendly version " link. Or you could set the the header and cells of the table to a css class called "noprint" and have a css for display and one for printing, setting visible to false in the second css... Commented Nov 5, 2010 at 23:04
  • Yeah, I agree with Tim. You really shouldn't need to rebind, and the code you're using should work. Good thought on the AutoGenerateColumns property... Commented Nov 5, 2010 at 23:07

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.