Important alert: (current site time 7/15/2013 8:48:15 AM EDT)
 

VB icon

_A simple datagrid (data grid) column resizer

Email
Submitted on: 8/4/2004 11:52:52 AM
By: Duane Warsham 
Level: Intermediate
User Rating: By 6 Users
Compatibility: VB.NET
Views: 26179
(About the author)
 
     This function call will allow you to resize your data grid column widths, as well as set other data grid properties using the DataGridTableStyle object.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: _A simple datagrid (data grid) column resizer
// Description:This function call will allow you to resize your data grid column widths, as well as set other data grid properties using the DataGridTableStyle object.
// By: Duane Warsham
//
// Inputs:DataSet object
//
// Returns:DataGridTableStyle object
//
// Assumes:works ok, only draw back is that for now you have to hard code the total # of columns you need to resize.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2639&lngWId=10//for details.//**************************************

' To use the object, call from your code 
'......
'......
'dataAdapter1.Fill(dataset1)
'
' datagrid1.DataSource = dataset1.Tables(0)
'
'Add new TableStyle to the DataGrid, you must clear the present one 1st, or will error
'datagrid1.TableStyles.Clear()
'datagrid1.TableStyles.Add(Database.myTableStyle(dataset1)) 'where Database is the class containing the myTableStyle function
'......
'......
'
'Now the actual code
'it must be shared in some cases
'
 Public Shared Function myTableStyle(ByVal ds As DataSet) As DataGridTableStyle
 'this will resize data grid column widths, and use it to set grid colors, column names, etc.
 'only draw back is having to hard code your style for the # of columns you have
 'Create Table Style.
 myTableStyle = New DataGridTableStyle
 'Set MappingName for TableStyle.
 myTableStyle.MappingName = ds.Tables(0).ToString
 myTableStyle.SelectionBackColor = Color.Yellow
 myTableStyle.SelectionForeColor = Color.DarkRed
 myTableStyle.HeaderForeColor = Color.DarkBlue
 myTableStyle.AlternatingBackColor = Color.LightYellow
 myTableStyle.BackColor = Color.AntiqueWhite
 'Create 1st column.
 Dim myCOL As New DataGridTextBoxColumn
 With myCOL
.MappingName = "WorkDate"
.HeaderText = "Work Date"
.Width = 100
 End With
 myTableStyle.GridColumnStyles.Add(myCOL)
 'Create 2nd column.
 myCOL = New DataGridTextBoxColumn
 With myCOL
.MappingName = "JobName"
.HeaderText = "Job Description"
.Width = 200
 End With
 myTableStyle.GridColumnStyles.Add(myCOL)
 'Create 3rd column.
 myCOL = New DataGridTextBoxColumn
 With myCOL
.MappingName = "Hours"
.HeaderText = "Hours"
.Width = 40
 End With
 myTableStyle.GridColumnStyles.Add(myCOL)
 'Create 4th column.
 myCOL = New DataGridTextBoxColumn
 With myCOL
.MappingName = "Comments"
.HeaderText = "Comments"
.Width = 397
 End With
 myTableStyle.GridColumnStyles.Add(myCOL)
 End Function


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
8/5/2004 1:51:59 AMJohn Robinson

how to set d Ist Fixed columns width? by default its showing too big. do u know any way???
mail me on sanju_ghelani@hotmail.com
Thanks...
(If this comment was disrespectful, please report it.)

 
12/14/2005 2:03:04 AMavis

i ve got error "Type 'DataGridTableStyle'
is not defined" , what should i do?
(If this comment was disrespectful, please report it.)

 
3/13/2006 11:06:01 AMDogs_Bollox

hi,
I have just rated this code with top marks, While it is nothing special in it self, it was a godsend to me. I am just starting to move from VB6 to VB2005 and this issue of column mapping was causing me some issues. Great work and keep it up, it's small nuggets of knowledge like this that programmers really need.
(If this comment was disrespectful, please report it.)

 
6/12/2007 1:22:27 AMdwi

Very good
(If this comment was disrespectful, please report it.)

 
7/22/2008 4:14:52 AMkhaled alsalama

hi
thank you it is very nice i am khaled from syria - der azor ,
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.