Hi, caple of general questions

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 17
Reputation: 1qaz2wsx7 is an unknown quantity at this point 
Solved Threads: 0
1qaz2wsx7 1qaz2wsx7 is offline Offline
Newbie Poster

Hi, caple of general questions

 
0
  #1
Jul 17th, 2007
Hi

I have some questions:

1) How can i get the size of the screen ? (not the forum, the
screen borders)
2) How can i resize all the columns in a DataGridView
to the length of their values ? (when using the built in
method, Autoresize, its only make it larger but dont make
it small if the value is shorter then the column)
3) How can i check if a specific KeyBoard key pressed ? (like Up or A buttons)
4) How can i change a specific Column Behavior in a DataGridView ? (like i want that
only the fields of this Column will be limited to 4 digits)

Thanks.
Last edited by 1qaz2wsx7; Jul 17th, 2007 at 9:30 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Hi, caple of general questions

 
0
  #2
Jul 17th, 2007
1) How can i get the size of the screen ? (not the forum, the
screen borders)
You can find that stuff in the SystemInformation class.

2) How can i resize all the columns in a DataGridView
to the length of their values ?
The only sure way I know is to find the longest column value for each column in all of the rows and fit the column width to it. I haven't done it with a DataGridView, but this is what I use for a DataGrid. Maybe it'll help you write your own.
  1. public static void AutoSizeColumns( DataGrid grid ) {
  2. System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd( grid.Handle );
  3. GridColumnStylesCollection cs = grid.TableStyles[0].GridColumnStyles;
  4. object ds = grid.DataSource;
  5.  
  6. for ( int style = 0; style < cs.Count; style++ ) {
  7. int rowCount = ( ds is DataView ) ? ( ds as DataView ).Count : ( ds as DataTable ).Rows.Count;
  8. string format = "{0}";
  9.  
  10. if ( cs[style] is DataGridTextBoxColumn ) {
  11. format = "{0:" + ( cs[style] as DataGridTextBoxColumn ).Format + "}";
  12. }
  13.  
  14. // The default minimum width is the header text
  15. System.Drawing.SizeF maxWidth = g.MeasureString( cs[style].HeaderText, grid.Font );
  16.  
  17. // Find the widest row in the grid for this column
  18. for ( int i = 0; i < rowCount; i++ ) {
  19. System.Drawing.SizeF cellWidth = g.MeasureString( string.Format( format, grid[i, style] ), grid.Font );
  20.  
  21. if ( cellWidth.Width > maxWidth.Width ) {
  22. maxWidth = cellWidth;
  23. }
  24. }
  25.  
  26. cs[style].Width = (int)maxWidth.Width + 8;
  27. }
  28.  
  29. g.Dispose();
  30. }
3) How can i check if a specific KeyBoard key pressed ? (like Up or A buttons)
Anything that derives from the Control class has a KeyDown event with event arguments that tell you what key was pressed.

4) How can i change a specific Column Behavior in a DataGridView ?
If you can't fiddle with the properties, you have to handle events and do it manually. I think the last time I did numeric input in a DataGridView text box column, I handled the KeyDown event and suppressed the keypress if the key value wasn't a digit. And you can set a property that limits the number of characters that the column allows.
Last edited by Hamrick; Jul 17th, 2007 at 9:40 am.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: TheGathering is an unknown quantity at this point 
Solved Threads: 10
TheGathering's Avatar
TheGathering TheGathering is offline Offline
Junior Poster

Re: Hi, caple of general questions

 
0
  #3
Jul 17th, 2007
http://msdn2.microsoft.com/en-us/library/default.aspx

^-- Has the answers to all your questions with code examples.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC