locked
Browse Button [ Datagridview - VB.NET 2010 ] RRS feed

  • Pertanyaan

  • bagaimana cara membuat kolom text dgn tombol browse pada datagridview, tetapi tombol browse muncul saat cell focus

    Senin, 22 Oktober 2012 04.12

Jawaban

  • Halo Ahmad,

    Terima kasih untuk balasan anda.

    VB.Net tidak menggunakan += dalam event handler melainkan menggunakan Addhandler.

    Untuk mengetahui lebih jauh tentang konsep Addhandler dalam VB.Net, silahkan merujuk pada artikel berikut:

    RaiseEvent Statement

    AddHandler Statement

    Silahkan mereply kembali pada thread ini jika masih mengalami kendala.


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Senin, 29 Oktober 2012 09.55
    Moderator

Semua Balasan

  • Halo Ahmad,

    Terima kasih atas partisipasi anda dalam forum MSDN Indonesia.

    Kami pernah membahas masalah serupa pada forum kami di link

     Browse buttonControls in Windows Forms DataGridView

    Bila masih mengalami kendala, silahkan mereply kembali dalam forum ini.

    Semoga membantu.


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Jumat, 26 Oktober 2012 07.24
    Moderator
  • terima kasih atas bantuannya, tetapi masih ada masalah pada codenya, codenya sudah saya convert ke VB.NET dan hasilnya

    Public Class Form1
        Inherits Form
        Private txtbtnControl As TextBoxButtonControl
        Private Code As DataGridViewTextBoxColumn, Description As DataGridViewTextBoxColumn
    
        Public Sub New()
            InitializeComponent()
    
            Me.Code = New DataGridViewTextBoxColumn()
            Me.Code.Name = "FilePath"
            Me.Code.Width = 300
            Me.Code.Resizable = DataGridViewTriState.[False]
            Me.DataGridView1.Columns.Add(Code)
    
            Me.Description = New DataGridViewTextBoxColumn()
            Me.Description.Name = "Description"
            Me.Description.Width = 150
            Me.DataGridView1.Columns.Add(Description)
    
            'adding textboxbutton control at code column...'
            Me.txtbtnControl = New TextBoxButtonControl()
            Me.txtbtnControl.Visible = False
            Me.DataGridView1.Controls.Add(Me.txtbtnControl)
    
            Me.txtbtnControl.btnCode.Click += New EventHandler(AddressOf btnCode_Click)
            Me.txtbtnControl.Leave += New EventHandler(AddressOf txtbtnControl_Leave)
            Me.txtbtnControl.txtCode.TextChanged += New EventHandler(AddressOf txtCode_TextChanged)
    
            Me.DataGridView1.CellClick += New DataGridViewCellEventHandler(AddressOf dataGridView1_CellClick)
            Me.DataGridView1.ColumnHeaderMouseClick += New DataGridViewCellMouseEventHandler(AddressOf dataGridView1_ColumnHeaderMouseClick)
            Me.DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
        End Sub
    
        Private Sub dataGridView1_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs)
            Me.txtbtnControl.Visible = False
        End Sub
    
        Private Sub dataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
            If e.ColumnIndex = Code.Index Then
                Try
                    Dim rect As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
                    Me.txtbtnControl.Location = rect.Location
                    Me.txtbtnControl.Size = rect.Size
                    Me.txtbtnControl.btnCode.Text = ".."
                    Me.txtbtnControl.Visible = True
                    Me.txtbtnControl.Focus()
                    If Not String.IsNullOrEmpty(Me.DataGridView1.CurrentRow.Cells(Code.Name).Value.ToString()) Then
                        Me.txtbtnControl.txtCode.Text = Me.DataGridView1.CurrentRow.Cells(Code.Name).Value.ToString()
                    Else
                        Me.txtbtnControl.txtCode.Text = ""
                    End If
                Catch generatedExceptionName As Exception
                End Try
            End If
        End Sub
    
        Private Sub txtCode_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
            If Me.txtbtnControl.txtCode.Modified Then
                Me.DataGridView1.CurrentRow.Cells(Code.Name).Value = Me.txtbtnControl.txtCode.Text
            End If
        End Sub
    
        Private Sub txtbtnControl_Leave(ByVal sender As Object, ByVal e As EventArgs)
            Try
                Me.txtbtnControl.Visible = False
                Me.txtbtnControl.txtCode.Text = ""
            Catch generatedExceptionName As Exception
            End Try
        End Sub
    
        Private Sub btnCode_Click(ByVal sender As Object, ByVal e As EventArgs)
            'Form2 form2 = new Form2();'
            'form2.ShowDialog();'
            Dim dialog As New OpenFileDialog()
            If dialog.ShowDialog() = DialogResult.OK Then
                Try
                    DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value = dialog.FileName.ToString()
                    txtbtnControl.txtCode.Text = dialog.FileName.ToString()
                Catch generatedExceptionName As Exception
                End Try
            End If
        End Sub
    End Class
    
    Class TextBoxButtonControl
        Inherits UserControl
        Public txtCode As TextBox
        Public btnCode As Button
    
        Public Sub New()
            Me.txtCode = New TextBox()
            Me.Controls.Add(Me.txtCode)
            Me.btnCode = New Button()
            Me.Controls.Add(Me.btnCode)
            Me.renderControl()
        End Sub
        Public Sub renderControl()
            Me.txtCode.Location = New Point(0, 0)
            'this.txtCode.Width = 2 * this.Width / 3 - 17;'
            Me.txtCode.Width = Me.Width + 115
            Me.txtCode.Height = Me.Height
            Me.btnCode.Location = New Point(Me.Width + 115, 0)
            Me.btnCode.Width = 32
            Me.btnCode.Height = 21
        End Sub
    End Class

    error:

    'Public Event Click(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    Me.txtbtnControl.btnCode.Click += New EventHandler(AddressOf btnCode_Click)

    'Public Event Leave(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    Me.txtbtnControl.Leave += New EventHandler(AddressOf txtbtnControl_Leave)

    'Public Event TextChanged(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    Me.txtbtnControl.txtCode.TextChanged += New EventHandler(AddressOf txtCode_TextChanged)

    'Public Event CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    Me.DataGridView1.CellClick += New DataGridViewCellEventHandler(AddressOf dataGridView1_CellClick)

    'Public Event ColumnHeaderMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    Me.DataGridView1.ColumnHeaderMouseClick += New DataGridViewCellMouseEventHandler(AddressOf dataGridView1_ColumnHeaderMouseClick)

    solusinya gimana ya??

    Sabtu, 27 Oktober 2012 02.31
  • Halo Ahmad,

    Terima kasih untuk balasan anda.

    VB.Net tidak menggunakan += dalam event handler melainkan menggunakan Addhandler.

    Untuk mengetahui lebih jauh tentang konsep Addhandler dalam VB.Net, silahkan merujuk pada artikel berikut:

    RaiseEvent Statement

    AddHandler Statement

    Silahkan mereply kembali pada thread ini jika masih mengalami kendala.


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Senin, 29 Oktober 2012 09.55
    Moderator
  • saya masih bingung mengenai codenya, tapi saya coba cari contoh programnya dan dapat contoh programnya tetapi contoh programnya menambahkan DateTimePicker ke datagrid, saya masih bingung bagaimana cara menambahkan kolom text dengan tombol browse

    mungkin rekan-rekan bisa membantu, dan ini saya sertakan link program yang saya upload

    http://www.4shared.com/zip/sQUjF2G_/datagrid_column.html

    Selasa, 30 Oktober 2012 06.24