locked
Unable to bind Textboxes and picture box once cell is clicked on datagridview RRS feed

  • Question

  • Imports System.Data
    Imports System.Data.SqlClient

    Public Class frmAttendance_View

        Private StrCon As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Storage.mdf;Integrated Security=True"
        Private Connection As New SqlConnection
        Private Command As SqlCommand
        Private Sql As String
        Private Reader As SqlDataReader
        Private Adapter As SqlDataAdapter
        Private DataSt As DataSet
        Private BindingSrc As BindingSource
        Private Dtable As DataTable


        Dim title As String = "Attendance viewer"


        Private Sub frmAttendance_View_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Try
                With Connection
                    If .State = ConnectionState.Closed Then
                        .ConnectionString = StrCon
                        .Open()
                    End If
                End With
            Catch ex As Exception
                MsgBox(ex.Message.ToString, MsgBoxStyle.Exclamation, "Form Load")
            Finally
                Connection.Close()
            End Try

            With Me
                .Text = title
                .MaximizeBox = False
                .FormBorderStyle = FormBorderStyle.FixedDialog
            End With


            With txtFullname
                .BackColor = Color.Cornsilk
                .ForeColor = Color.Black
                .TextAlign = HorizontalAlignment.Center
            End With

            With txtTimeStamp
                .BackColor = Color.Cornsilk
                .ForeColor = Color.Black
                .TextAlign = HorizontalAlignment.Center
            End With

            With txtDateTime
                .TextAlign = HorizontalAlignment.Center
                .BackColor = Color.Cornsilk
                .ForeColor = Color.Black
            End With

            With txtRemarks
                .BackColor = Color.Cornsilk
                .ForeColor = Color.Black
            End With

            With txtSearch
                .BackColor = Color.Cornsilk
                .BackColor = Color.OrangeRed
                .TextAlign = HorizontalAlignment.Center
            End With



            Show_DataGrid()



        End Sub

        Private Sub Show_DataGrid()
            Try
                Dim dgv As DataGridView = DataGridView1
                BindingSrc = New BindingSource
                With dgv

                    Sql = "SELECT FullName as [Complete Name], RegistrationNumber as [Time Stamp], DateCaptured as [Date / Time] "
                    Sql &= "FROM  Attendance_Info "

                    Command = New SqlCommand(Sql, Connection)
                    Adapter = New SqlDataAdapter(Command)
                    Dtable = New DataTable
                    Adapter.Fill(Dtable)

                    dgv.DataSource = Dtable

                    BindingSrc.DataSource = Dtable

                    For Each ctrl As Control In Me.Controls
                        If TypeOf ctrl Is TextBox Then
                            ctrl.DataBindings.Clear()
                        End If
                    Next
                    PictureBox1.DataBindings.Clear()

                    txtFullname.DataBindings.Add("Text", BindingSrc, "FullName")
                    txtTimeStamp.DataBindings.Add("Text", BindingSrc, "RegistrationNumber")
                    txtDateTime.DataBindings.Add("Text", BindingSrc, "DateCaptured")
                    txtRemarks.DataBindings.Add("Text", BindingSrc, "Remarks")
                    PictureBox1.DataBindings.Add("image", BindingSrc, "ProfileImage", True)


                    .AllowUserToAddRows = False
                    .AllowUserToDeleteRows = False
                    .AllowUserToOrderColumns = True
                    .AllowUserToResizeColumns = True
                    .RowHeadersVisible = True
                    .SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
                    .MultiSelect = False
                    .RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing
                    .RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToFirstHeader
                    .ReadOnly = False

                    For i = 0 To dgv.Columns.Count - 1
                        .Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                        .Columns(i).SortMode = DataGridViewColumnSortMode.Programmatic
                    Next

                    .RowHeadersDefaultCellStyle.BackColor = Color.LavenderBlush
                    .AlternatingRowsDefaultCellStyle.BackColor = Color.MistyRose
                    .BackgroundColor = Color.LightPink
                    .DefaultCellStyle.BackColor = Color.LightSeaGreen
                End With


            Catch ex As Exception
                MsgBox(ex.Message.ToString, MsgBoxStyle.Exclamation, "Load data grid")
            Finally
                Connection.Close()
            End Try
        End Sub

    End Class
    Monday, April 8, 2019 4:53 AM

All replies

  • Hi,

    I see your code,you have bound Textboxes and picturebox using the code below.

                    txtFullname.DataBindings.Add("Text", BindingSrc, "FullName")
                    txtTimeStamp.DataBindings.Add("Text", BindingSrc, "RegistrationNumber")
                    txtDateTime.DataBindings.Add("Text", BindingSrc, "DateCaptured")
                    txtRemarks.DataBindings.Add("Text", BindingSrc, "Remarks")
                    PictureBox1.DataBindings.Add("image", BindingSrc, "ProfileImage", True)
    

    What is the relationship between cell is clicked on datagridview and bind Textboxes and picture box?Still need you to describe your issue in detail.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 8, 2019 5:52 AM
  • Hello,

    Going with the code shown, there are no indicators that there is a problem as per your title. What is concerning is how your code flows from form load, specifically clearing data bindings. If this is a main form there is zero reasons to clear data bindings. If this is a child form there is zero reasons to clear data bindings if you create the form with Dim frm As New frmAttendance_View each time it’s displayed. Bottom line is you have not given enough information to assist with this issue.

    Also, in the future place code in code blocks so it's easy for those who are here to assist to read your code.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, April 8, 2019 9:46 AM