none
DataGridView e DataGrid RRS feed

  • Pergunta

  •  

    Pessoal tenho um dataGrid formatado que peguei em um dos projetos que faço, mas com a evolução quero usar o DataGridView mas da seguinte maneira

     

    Code Snippet

     

    Public Class siagDataGrid

    Inherits System.Windows.Forms.DataGrid

    Private Const WM_SETCURSOR As Integer = 32

    Dim intRow As Integer

    Dim intColunaHand As Integer = -1

    Property ColunaHand() As Integer

    Get

    Return intColunaHand

    End Get

    Set(ByVal Value As Integer)

    intColunaHand = Value

    End Set

    End Property

    Public Event RowChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

    If msg.WParam.ToInt32() = CInt(Keys.Enter) Then

    SendKeys.Send("{Tab}")

    Return True

    End If

    Return MyBase.ProcessCmdKey(msg, keyData)

    End Function

    Protected Overloads Overrides Sub OnCurrentCellChanged(ByVal e As System.EventArgs)

    If intRow <> MyBase.CurrentCell.RowNumber Then

    RaiseEvent RowChanged(Me, e)

    intRow = MyBase.CurrentCell.RowNumber

    End If

    MyBase.OnCurrentCellChanged(e)

    End Sub

    Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)

    RaiseEvent RowChanged(Me, e)

    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    If m.Msg <> WM_SETCURSOR Or intColunaHand = -1 Then

    MyBase.WndProc(m)

    Else

    'se a for a coluna que contem o icone

    Dim pt As Point = Me.PointToClient(Control.MousePosition)

    Dim hti As DataGrid.HitTestInfo = Me.HitTest(pt.X, pt.Y)

    If hti.Column = intColunaHand Then

    Cursor.Current = System.Windows.Forms.Cursors.Hand

    'if not, call the baseclass

    Else

    MyBase.WndProc(m)

    End If

    End If

    End Sub

    End Class 'MyDataGrid

     

     

    E eu o chamo no form na parte que fica Form1.Designer.vb assim:

     

    Code Snippet

    Private Sub InitializeComponent()

    Me.grd = New ncCorp.ctr.siagDataGrid

    CType(Me.grd, System.ComponentModel.ISupportInitialize).BeginInit()

    Me.SuspendLayout()

    '

    'grd

    '

    Me.grd.CaptionVisible = False

    Me.grd.ColunaHand = -1

    Me.grd.DataMember = ""

    Me.grd.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!)

    Me.grd.HeaderForeColor = System.Drawing.SystemColors.ControlText

    Me.grd.Location = New System.Drawing.Point(12, 12)

    Me.grd.Name = "grd"

    Me.grd.ReadOnly = True

    Me.grd.RowHeaderWidth = 20

    Me.grd.Size = New System.Drawing.Size(268, 175)

    Me.grd.TabIndex = 153

    '

    'Form1

    '

    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

    Me.ClientSize = New System.Drawing.Size(292, 266)

    Me.Controls.Add(Me.grd)

    Me.Name = "Form1"

    Me.Text = "Form1"

    CType(Me.grd, System.ComponentModel.ISupportInitialize).EndInit()

    Me.ResumeLayout(False)

    End Sub

    Friend WithEvents grd As ncCorp.ctr.siagDataGrid

     

     

    E na chamada para preenchimento de código eh da seguinte maneira, onde eu trago os dados para o DataGrid:

     

    Code Snippet

    Private Sub ConfigDataGrid()

    Dim myGridStyle As DataGridTableStyle = New DataGridTableStyle

    myGridStyle.MappingName = "Cliente"

    myGridStyle.ColumnHeadersVisible = True

    myGridStyle.AllowSorting = False

    myGridStyle.RowHeadersVisible = True

    Dim col1 As DataGridColumnStyle = New DataGridColorTextBoxColumn

    col1.MappingName = "idptcestudante"

    col1.ReadOnly = True

    col1.HeaderText = "Código"

    col1.Width = 50

    col1.NullText = ""

    Dim col11 As DataGridColumnStyle = New DataGridColorTextBoxColumn

    col11.MappingName = "numerodes"

    col11.ReadOnly = True

    col11.HeaderText = "Número"

    col11.Width = 60

    col11.NullText = ""

    Dim col2 As DataGridColumnStyle = New DataGridColorTextBoxColumn

    col2.MappingName = "nomeestudante"

    col2.ReadOnly = True

    col2.HeaderText = "Nome"

    col2.Width = 210

    col2.NullText = ""

    Dim col3 As DataGridColumnStyle = New DataGridColorTextBoxColumn

    col3.MappingName = "nome"

    col3.ReadOnly = True

    col3.HeaderText = "Cliente"

    col3.Width = 110

    col3.NullText = ""

    myGridStyle.GridColumnStyles.Add(col1)

    myGridStyle.GridColumnStyles.Add(col2)

    myGridStyle.GridColumnStyles.Add(col3)

    grd.TableStyles.Clear()

    grd.TableStyles.Add(myGridStyle)

    grd.ReadOnly = True

    col1 = Nothing : col2 = Nothing

    End Sub

     

     

    Sendo que esse sub eu carrego no Evento Load do Form1, a facilidade q eu tenho aí eh que qndo quero trazer mais um dado basta acrescentar mais um bloco col trocando os dados para os novos, gostaria de fazer isso com o DataGridView, se alguém puder me ajudar, ficaria agradecido... Já tentei de tudo um pouco, soh faltou reza braba...
    sábado, 24 de janeiro de 2009 05:01

Todas as Respostas