Connecting Tech Pros Worldwide Help | Site Map

Resize DataGridView column populated from DataSource

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 9 '09
I have a DataGridView populated from a DataSource with the code:
Expand|Select|Wrap|Line Numbers
  1.         Dim dt As New DataTable
  2.         dt.Columns.Add("Name")
  3.         dt.Columns.Add("Result")
  4.         Dim dr As DataRow
  5.         For i = 1 To 12
  6.             dr = dt.NewRow()
  7.             dr(0) = "Name " & i.ToString()
  8.             dr(1) = "Result " & i.ToString()
  9.             dt.Rows.Add(dr)
  10.         Next
  11.         grd.DataSource = dt
  12.  
The grid is anchored so that it resized together with the form. I want to resize one of the columns when the grid resizes. I'm using the code:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub grd_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grd.Resize
  2.         colName.Width = grd.Width - colResult.Width - 18
  3.     End Sub
  4.  
This has no effect (column is not resizing).

I have tested with populating the grid without a DataSource, using the code:
Expand|Select|Wrap|Line Numbers
  1.         For i = 1 To 12
  2.             n = grd.Rows.Add()
  3.             grd.Rows(n).Cells(0).Value = "Name" & i.ToString()
  4.             grd.Rows(n).Cells(1).Value = "Result" & i.ToString()
  5.         Next
  6.  
In this case, resizing works fine. However, I need to use a DataSource.

Could someone help ?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#2: Oct 9 '09

re: Resize DataGridView column populated from DataSource


I always set my column autosizing to fit all cells (headers and regulars) and its always resized just fine?
Reply