mgmhicks
asked on
How to keep columns for being adjustable
I have setup a grid using the following code. Cant figure out how to keep the columns from having there width changed by the user. I want to keep the fixed width size and not allow users to change it. Thanks in advance.
Private Function SetupGridHeader()
Dim mFirstDate As Date
Dim mDayofWeek As String
Dim mColumnString As String
mFirstDate = cboPayPeriod.Text
Dim i As Int16
Dim mEmployeeID As Integer
Dim mPayPeriod As Integer
Dim f As New System.Drawing.Font("Verda na", 15.25F, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, System.Byte))
dgTransactions.HeaderFont = f
dgTransactions.TableStyles .Clear()
Dim ts1 As DataGridTableStyle
'dgTransactions.TableStyle s.Remove(t s1)
ts1 = New DataGridTableStyle
ts1.MappingName = "Transactions"
' Set other properties.
ts1.AlternatingBackColor = Color.LightBlue
ts1.AllowSorting = False
'
' Add 1 combo box to column 1
Dim ComboTextCol As DataGridComboBoxColumn
ComboTextCol = New DataGridComboBoxColumn
ComboTextCol.MappingName = "PayType"
ComboTextCol.HeaderText = "PayType"
ComboTextCol.Width = 100
ts1.GridColumnStyles.Add(C omboTextCo l)
ts1.PreferredRowHeight = (ComboTextCol.ColumnComboB ox.Height + 3)
ComboTextCol.ColumnComboBo x.Items.Cl ear()
Dim PayTypeIDCol As DataGridTextBoxColumn
PayTypeIDCol = New DataGridTextBoxColumn
With PayTypeIDCol
.Width = 0
.MappingName = "PayTypeID"
End With
ts1.GridColumnStyles.Add(P ayTypeIDCo l)
' add first AutoOvertime
Dim TextCol As DataGridTextBoxColumn
TextCol = New DataGridTextBoxColumn
TextCol.MappingName = "AutoOT1"
TextCol.HeaderText = "Auto OT"
TextCol.NullText = 0
TextCol.Format = "##.##"
TextCol.Width = 40
ts1.GridColumnStyles.Add(T extCol)
' Add remaining text columns with textbox column style
Dim mName As String
For i = 1 To 7
'Dim TextCol As DataGridTextBoxColumn
TextCol = New DataGridTextBoxColumn
mName = "Day" & CInt(i)
TextCol.MappingName = mName
mDayofWeek = mFirstDate.DayOfWeek.ToStr ing
'mColumnString = mFirstDate & vbNewLine & mDayofWeek
TextCol.TextBox.Multiline = False
TextCol.HeaderText = mFirstDate & vbCrLf & mDayofWeek
'TextCol.HeaderText = TextCol.HeaderText & mDayofWeek
TextCol.Format = "##.##"
TextCol.NullText = 0
TextCol.Width = 70
ts1.GridColumnStyles.Add(T extCol)
'dgTransactions.TableStyle s(0).GridC olumnStyle s(i).Heade rText = mFirstDate
mFirstDate = DateAdd(DateInterval.Day, 1, mFirstDate)
Next
' put the 2nd Auto Overtime column in
TextCol = New DataGridTextBoxColumn
TextCol.MappingName = "AutoOT2"
TextCol.HeaderText = "Auto OT"
TextCol.NullText = 0
TextCol.Width = 40
TextCol.Format = "##.##"
ts1.GridColumnStyles.Add(T extCol)
' now finish the day fields
For i = 8 To 14
'Dim TextCol As DataGridTextBoxColumn
TextCol = New DataGridTextBoxColumn
mName = "Day" & CInt(i)
TextCol.MappingName = mName
mDayofWeek = mFirstDate.DayOfWeek.ToStr ing
TextCol.HeaderText = mFirstDate & vbCrLf & mDayofWeek
TextCol.Format = "##.##"
TextCol.NullText = 0
TextCol.Width = 70
ts1.GridColumnStyles.Add(T extCol)
'dgTransactions.TableStyle s(0).GridC olumnStyle s(i).Heade rText = mFirstDate
mFirstDate = DateAdd(DateInterval.Day, 1, mFirstDate)
Next
' now fill the combobox
mEmployeeID = cboEmployees.SelectedValue
mPayPeriod = cboPayPeriod.SelectedValue
daPayTypes = New SqlDataAdapter
SqlSelectCommand3.Paramete rs(1).Valu e = mEmployeeID
SqlSelectCommand3.Paramete rs(2).Valu e = mPayPeriod
daPayTypes.SelectCommand = SqlSelectCommand3
daPayTypes.SelectCommand.C onnection = MyConn
' OLD STATEMENT IN PROCEDURE GET PAYTYPES. NOW JUST PICK ALL PAYTYPES
' SELECT ID, Name FROM time_PayTypes WHERE (Name NOT IN (SELECT paytype FROM time_curtrans WHERE empid = @EmpID AND payperiod = @PayPeriod)) ORDER BY ID
' go through ds to see if paytypes already exists. If so get rid of it.
' 12/22/04 removed to allow multiple transaction types
'For i = 0 To ds.Tables.Count - 1
' If ds.Tables(i).TableName = "PayTypes" Then
' ds.Tables(i).Clear()
' End If
'Next
Try
daPayTypes.Fill(ds, "PayTypes")
Catch ex As Exception
MsgBox(ex.Message)
End Try
For i = 0 To ds.Tables("PayTypes").Rows .Count - 1
ComboTextCol.ColumnComboBo x.Items.Ad d(ds.Table s("paytype s").Rows(i ).Item(1))
Next
ComboTextCol.ColumnComboBo x.Selected Index = 0
' c) set the dropdown style of the combo...
ComboTextCol.ColumnComboBo x.DropDown Style = ComboBoxStyle.DropDownList
'add the custom table style to the datagrid table styles
dgTransactions.TableStyles .Add(ts1)
End Function
Private Function SetupGridHeader()
Dim mFirstDate As Date
Dim mDayofWeek As String
Dim mColumnString As String
mFirstDate = cboPayPeriod.Text
Dim i As Int16
Dim mEmployeeID As Integer
Dim mPayPeriod As Integer
Dim f As New System.Drawing.Font("Verda
dgTransactions.HeaderFont = f
dgTransactions.TableStyles
Dim ts1 As DataGridTableStyle
'dgTransactions.TableStyle
ts1 = New DataGridTableStyle
ts1.MappingName = "Transactions"
' Set other properties.
ts1.AlternatingBackColor = Color.LightBlue
ts1.AllowSorting = False
'
' Add 1 combo box to column 1
Dim ComboTextCol As DataGridComboBoxColumn
ComboTextCol = New DataGridComboBoxColumn
ComboTextCol.MappingName = "PayType"
ComboTextCol.HeaderText = "PayType"
ComboTextCol.Width = 100
ts1.GridColumnStyles.Add(C
ts1.PreferredRowHeight = (ComboTextCol.ColumnComboB
ComboTextCol.ColumnComboBo
Dim PayTypeIDCol As DataGridTextBoxColumn
PayTypeIDCol = New DataGridTextBoxColumn
With PayTypeIDCol
.Width = 0
.MappingName = "PayTypeID"
End With
ts1.GridColumnStyles.Add(P
' add first AutoOvertime
Dim TextCol As DataGridTextBoxColumn
TextCol = New DataGridTextBoxColumn
TextCol.MappingName = "AutoOT1"
TextCol.HeaderText = "Auto OT"
TextCol.NullText = 0
TextCol.Format = "##.##"
TextCol.Width = 40
ts1.GridColumnStyles.Add(T
' Add remaining text columns with textbox column style
Dim mName As String
For i = 1 To 7
'Dim TextCol As DataGridTextBoxColumn
TextCol = New DataGridTextBoxColumn
mName = "Day" & CInt(i)
TextCol.MappingName = mName
mDayofWeek = mFirstDate.DayOfWeek.ToStr
'mColumnString = mFirstDate & vbNewLine & mDayofWeek
TextCol.TextBox.Multiline = False
TextCol.HeaderText = mFirstDate & vbCrLf & mDayofWeek
'TextCol.HeaderText = TextCol.HeaderText & mDayofWeek
TextCol.Format = "##.##"
TextCol.NullText = 0
TextCol.Width = 70
ts1.GridColumnStyles.Add(T
'dgTransactions.TableStyle
mFirstDate = DateAdd(DateInterval.Day, 1, mFirstDate)
Next
' put the 2nd Auto Overtime column in
TextCol = New DataGridTextBoxColumn
TextCol.MappingName = "AutoOT2"
TextCol.HeaderText = "Auto OT"
TextCol.NullText = 0
TextCol.Width = 40
TextCol.Format = "##.##"
ts1.GridColumnStyles.Add(T
' now finish the day fields
For i = 8 To 14
'Dim TextCol As DataGridTextBoxColumn
TextCol = New DataGridTextBoxColumn
mName = "Day" & CInt(i)
TextCol.MappingName = mName
mDayofWeek = mFirstDate.DayOfWeek.ToStr
TextCol.HeaderText = mFirstDate & vbCrLf & mDayofWeek
TextCol.Format = "##.##"
TextCol.NullText = 0
TextCol.Width = 70
ts1.GridColumnStyles.Add(T
'dgTransactions.TableStyle
mFirstDate = DateAdd(DateInterval.Day, 1, mFirstDate)
Next
' now fill the combobox
mEmployeeID = cboEmployees.SelectedValue
mPayPeriod = cboPayPeriod.SelectedValue
daPayTypes = New SqlDataAdapter
SqlSelectCommand3.Paramete
SqlSelectCommand3.Paramete
daPayTypes.SelectCommand = SqlSelectCommand3
daPayTypes.SelectCommand.C
' OLD STATEMENT IN PROCEDURE GET PAYTYPES. NOW JUST PICK ALL PAYTYPES
' SELECT ID, Name FROM time_PayTypes WHERE (Name NOT IN (SELECT paytype FROM time_curtrans WHERE empid = @EmpID AND payperiod = @PayPeriod)) ORDER BY ID
' go through ds to see if paytypes already exists. If so get rid of it.
' 12/22/04 removed to allow multiple transaction types
'For i = 0 To ds.Tables.Count - 1
' If ds.Tables(i).TableName = "PayTypes" Then
' ds.Tables(i).Clear()
' End If
'Next
Try
daPayTypes.Fill(ds, "PayTypes")
Catch ex As Exception
MsgBox(ex.Message)
End Try
For i = 0 To ds.Tables("PayTypes").Rows
ComboTextCol.ColumnComboBo
Next
ComboTextCol.ColumnComboBo
' c) set the dropdown style of the combo...
ComboTextCol.ColumnComboBo
'add the custom table style to the datagrid table styles
dgTransactions.TableStyles
End Function
or try this:
[VB.NET]
-------------------------- ---------- ---------- -----
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.Mouse EventArgs)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
If hti.Type = DataGrid.HitTestType.Colum nResize Then
Return 'no baseclass call
End If
MyBase.OnMouseDown(e)
End Sub
[VB.NET]
--------------------------
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.Mouse
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
If hti.Type = DataGrid.HitTestType.Colum
Return 'no baseclass call
End If
MyBase.OnMouseDown(e)
End Sub
ASKER
I'm erroring out on on this line
New Point(e.X,e.Y))
Cant be converted to system.windows.forms.datag ridcell
New Point(e.X,e.Y))
Cant be converted to system.windows.forms.datag
Here is a method for doing it column by column. Using column table and column styles.
(You have to set up table and column styles explicitly on the grid)
Private Sub myInitializer()
Dim col As DataGridColumnStyle
For Each col In Me.DataGrid1.TableStyles(0 ).GridColu mnStyles
AddHandler col.WidthChanged, AddressOf datagrid1_widthChanged
Next
End Sub
Private Sub datagrid1_widthChanged(ByV al sender As Object, ByVal e As EventArgs)
Select Case CType(sender, DataGridColumnStyle).Mappi ngName
Case "col1"
CType(sender, DataGridColumnStyle).Width = 100
End Select
End Sub
(You have to set up table and column styles explicitly on the grid)
Private Sub myInitializer()
Dim col As DataGridColumnStyle
For Each col In Me.DataGrid1.TableStyles(0
AddHandler col.WidthChanged, AddressOf datagrid1_widthChanged
Next
End Sub
Private Sub datagrid1_widthChanged(ByV
Select Case CType(sender, DataGridColumnStyle).Mappi
Case "col1"
CType(sender, DataGridColumnStyle).Width
End Select
End Sub
Can you checkthis code:
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.Mouse EventArgs)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y)) 'DataGrid should be your datagrid name
If hti.Type = DataGrid.HitTestType.Colum nResize Then
Return 'no baseclass call
End If
MyBase.OnMouseDown(e)
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.Mouse
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y)) 'DataGrid should be your datagrid name
If hti.Type = DataGrid.HitTestType.Colum
Return 'no baseclass call
End If
MyBase.OnMouseDown(e)
End Sub
Note, if you are using the below lines of code:
Inherits DataGrid should be Inherits YourDataGrid.
-------------------------- ---------- ---------- ---------- ---------- ---------- -----
Public Class MyDataGrid
Inherits DataGrid
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.Mouse EventArgs)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
If hti.Type = DataGrid.HitTestType.Colum nResize Then
Return 'no baseclass call
End If
MyBase.OnMouseMove(e)
End Sub
Inherits DataGrid should be Inherits YourDataGrid.
--------------------------
Public Class MyDataGrid
Inherits DataGrid
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.Mouse
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
If hti.Type = DataGrid.HitTestType.Colum
Return 'no baseclass call
End If
MyBase.OnMouseMove(e)
End Sub
or try this:
-------------------------- ---------- ---------- -----
Private Sub MasterScheduleDataGrid_Mou seDown(ByV al sender As Object, ByVal e As System.Windows.Forms.Mouse EventArgs) Handles MasterScheduleDataGrid.Mou seDown
'to get old periodID
Dim pt = New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = MasterScheduleDataGrid.Hit Test(pt)
If hti.Type = DataGrid.HitTestType.Colum nResize Then
Return 'no baseclass call
End If
MyBase.OnMouseMove(e)
End Sub
--------------------------
Private Sub MasterScheduleDataGrid_Mou
'to get old periodID
Dim pt = New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = MasterScheduleDataGrid.Hit
If hti.Type = DataGrid.HitTestType.Colum
Return 'no baseclass call
End If
MyBase.OnMouseMove(e)
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you include this class, then you would need to redo 2 lines in the windows generated code as follows:
Friend WithEvents DataGrid1 As dataGridNoResize
and
Me.DataGrid1 = New dataGridNoResize
Friend WithEvents DataGrid1 As dataGridNoResize
and
Me.DataGrid1 = New dataGridNoResize
RobertRFreeman --- Thanks
I provided two solutions. The latter was an assist to mani-sai's solution.
--------------------------
Public Class MyDataGrid
Inherits DataGrid
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.Mouse
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
If hti.Type = DataGrid.HitTestType.Colum
Return 'no baseclass call
End If
MyBase.OnMouseMove(e)
End Sub