Link to home
Start Free TrialLog in
Avatar of mgmhicks
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("Verdana", 15.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, System.Byte))
            dgTransactions.HeaderFont = f

            dgTransactions.TableStyles.Clear()
            Dim ts1 As DataGridTableStyle

            'dgTransactions.TableStyles.Remove(ts1)
            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(ComboTextCol)

            ts1.PreferredRowHeight = (ComboTextCol.ColumnComboBox.Height + 3)
            ComboTextCol.ColumnComboBox.Items.Clear()

            Dim PayTypeIDCol As DataGridTextBoxColumn
            PayTypeIDCol = New DataGridTextBoxColumn
            With PayTypeIDCol
                .Width = 0
                .MappingName = "PayTypeID"

            End With
            ts1.GridColumnStyles.Add(PayTypeIDCol)

            ' 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(TextCol)
            ' 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.ToString

                '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(TextCol)

                'dgTransactions.TableStyles(0).GridColumnStyles(i).HeaderText = 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(TextCol)

            ' 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.ToString
                TextCol.HeaderText = mFirstDate & vbCrLf & mDayofWeek
                TextCol.Format = "##.##"
                TextCol.NullText = 0
                TextCol.Width = 70
                ts1.GridColumnStyles.Add(TextCol)

                'dgTransactions.TableStyles(0).GridColumnStyles(i).HeaderText = mFirstDate
                mFirstDate = DateAdd(DateInterval.Day, 1, mFirstDate)

            Next



            ' now fill the combobox

            mEmployeeID = cboEmployees.SelectedValue
            mPayPeriod = cboPayPeriod.SelectedValue


            daPayTypes = New SqlDataAdapter
            SqlSelectCommand3.Parameters(1).Value = mEmployeeID
            SqlSelectCommand3.Parameters(2).Value = mPayPeriod
            daPayTypes.SelectCommand = SqlSelectCommand3
            daPayTypes.SelectCommand.Connection = 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.ColumnComboBox.Items.Add(ds.Tables("paytypes").Rows(i).Item(1))

            Next
            ComboTextCol.ColumnComboBox.SelectedIndex = 0
            ' c) set the dropdown style of the combo...
            ComboTextCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
            'add the custom table style to the datagrid table styles
            dgTransactions.TableStyles.Add(ts1)

        End Function
Avatar of Mani Pazhana
Mani Pazhana
Flag of United States of America image

Sample VB.NET code:
-----------------------------------------------------------------------------------
 
Public Class MyDataGrid
 
      Inherits DataGrid
 
     Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
 
          Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
 
          If hti.Type = DataGrid.HitTestType.ColumnResize Then
 
               Return 'no baseclass call
 
          End If
 
          MyBase.OnMouseMove(e)
 
     End Sub
 
or try this:

[VB.NET]

---------------------------------------------------
 
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
 
     Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
 
     If hti.Type = DataGrid.HitTestType.ColumnResize Then
 
          Return 'no baseclass call
 
     End If
 
     MyBase.OnMouseDown(e)
 
End Sub
 
Avatar of mgmhicks
mgmhicks

ASKER

I'm erroring out on on this line

New Point(e.X,e.Y))

Cant be converted to system.windows.forms.datagridcell



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).GridColumnStyles
            AddHandler col.WidthChanged, AddressOf datagrid1_widthChanged
        Next

    End Sub

    Private Sub datagrid1_widthChanged(ByVal sender As Object, ByVal e As EventArgs)
        Select Case CType(sender, DataGridColumnStyle).MappingName
            Case "col1"
                CType(sender, DataGridColumnStyle).Width = 100
        End Select
    End Sub
Can you checkthis code:

Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
 
     Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))  'DataGrid should be your datagrid name
 
     If hti.Type = DataGrid.HitTestType.ColumnResize Then
 
          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.MouseEventArgs)
 
          Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
 
          If hti.Type = DataGrid.HitTestType.ColumnResize Then
 
               Return 'no baseclass call
 
          End If
 
          MyBase.OnMouseMove(e)
 
     End Sub
or try this:

---------------------------------------------------

Private Sub MasterScheduleDataGrid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MasterScheduleDataGrid.MouseDown

        'to get old periodID
        Dim pt = New Point(e.X, e.Y)

         Dim hti As DataGrid.HitTestInfo = MasterScheduleDataGrid.HitTest(pt)

         If hti.Type = DataGrid.HitTestType.ColumnResize Then
 
               Return 'no baseclass call
 
          End If
 
          MyBase.OnMouseMove(e)
 
     End Sub

ASKER CERTIFIED SOLUTION
Avatar of RobertRFreeman
RobertRFreeman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
RobertRFreeman --- Thanks
I provided two solutions.  The latter was an assist to mani-sai's solution.