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

  • Thread starter Thread starter jepoyman
  • Start date Start date
J

jepoyman

Guest
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

Continue reading...
 
Back
Top