Hi all,
I am working on a project affiliated to customer's database and i am required to insert, update, delete the data through a window's form program. The database i am using is MS SQL server 2005 and one of the columns i had is "Selection" which its datatype is set to "bits" which is used as a checkbox control in the datagridview in my program. The checkbox allows me to choose the specific row of customer's data, which i append to textboxes, but there is one problem, i am able to multiple-select the checkboxes. I want to restrict the datagridview checkbox column to only be able to select only one checkbox/one row at a time. The follow is part of the codes I've done so far:-
Any help would be greatly appreciated.
Thank you.
I am working on a project affiliated to customer's database and i am required to insert, update, delete the data through a window's form program. The database i am using is MS SQL server 2005 and one of the columns i had is "Selection" which its datatype is set to "bits" which is used as a checkbox control in the datagridview in my program. The checkbox allows me to choose the specific row of customer's data, which i append to textboxes, but there is one problem, i am able to multiple-select the checkboxes. I want to restrict the datagridview checkbox column to only be able to select only one checkbox/one row at a time. The follow is part of the codes I've done so far:-
Any help would be greatly appreciated.
Thank you.
Imports System.Data
Imports System.Data.SqlClient
Imports System.Threading
Imports System.Text.RegularExpressions
Imports System.Text
Public Class Form3
Dim da As SqlDataAdapter
Dim conn As SqlConnection
Dim bsource As BindingSource = New BindingSource()
Dim ds As DataSet
Dim firstname As String
Dim sql As String
Dim pnum As String
Dim msg As String
Dim MaxRows As Integer
Dim i As Integer
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnUpdate.Visible = False
Dim myCommand As New SqlCommand(sql, conn)
Dim connectionString As String = "Data Source=ITCU-23\SQLEXPRESS;Initial Catalog=compare;" & "Integrated Security=SSPI;"
conn = New SqlConnection(connectionString)
sql = "SELECT * FROM Alarms"
da = New SqlDataAdapter(sql, conn)
ds = New DataSet()
Dim commandBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
da.Fill(ds, "Alarms")
DataGridView1.DataSource = ds.Tables("Alarms")
MaxRows = ds.Tables("Alarms").Rows.Count
MaxRows = MaxRows - 1
' MsgBox(ds.Tables("Alarms").Rows(MaxRows).Item(0))
btnEdit.Enabled = False
btnDel.Enabled = False
Dim i As Integer
Dim dr As DataTable = CType(DataGridView1.DataSource, DataTable)
For i = 0 To (dr.Rows.Count - 1)
DataGridView1.Rows(i).Cells("Selection").Value = DBNull.Value
Next i
'MsgBox(ds.Tables("Alarms").Rows(MaxRows).Item(0))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
Dim name As String = txtName.Text
Dim blk As String = txtBlock.Text
Dim eNo As String = txtNum.Text
Dim unit As String = txtUnit.Text
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("Alarms").NewRow()
dsNewRow.Item("CustID") = ds.Tables("Alarms").Rows(MaxRows).Item(0) + 1
Dim signal As String = "Unit " & ds.Tables("Alarms").Rows(MaxRows).Item(0) + 1 & " on fire"
dsNewRow.Item("Signal") = signal
If (String.IsNullOrEmpty(name) Or IsNumeric(name) = True) Then
MsgBox("Please enter a proper name")
Exit Sub
End If
If (String.IsNullOrEmpty(blk) = True) Then
MsgBox("Please enter Block Name")
Exit Sub
End If
If (String.IsNullOrEmpty(unit) = True) Then
MsgBox("Please enter a proper unit number")
Exit Sub
End If
If (IsNumeric(eNo) = False Or String.IsNullOrEmpty(eNo)) Then
MsgBox("Please enter a proper emergency number")
Exit Sub
End If
Try
dsNewRow.Item("CustName") = name
dsNewRow.Item("UnitNum") = unit
dsNewRow.Item("BlkName") = blk
dsNewRow.Item("EmerNum") = eNo
ds.Tables("Alarms").Rows.Add(dsNewRow)
conn.Open()
da.Update(ds, "Alarms")
conn.Close()
MsgBox("Your Personal Information have been updated")
txtName.Text = ""
txtBlock.Text = ""
txtUnit.Text = ""
txtNum.Text = ""
btnEdit.Enabled = False
Catch ex As Exception
End Try
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtName.Clear()
txtBlock.Clear()
txtUnit.Clear()
txtNum.Clear()
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
txtName.ReadOnly = False
txtBlock.ReadOnly = False
txtUnit.ReadOnly = False
txtNum.ReadOnly = False
btnEdit.Visible = False
btnClear.Enabled = True
btnUpdate.Visible = True
btnDel.Enabled = False
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Try
Dim i As Integer
For i = 0 To (ds.Tables("Alarms").Rows.Count - 1)
If DataGridView1.Rows(i).Cells("Selection").Value IsNot System.DBNull.Value Then
If DataGridView1.Rows(i).Cells("Selection").Value = True Then
conn.Open()
Dim nameCommand As SqlCommand = conn.CreateCommand()
nameCommand.CommandText = _
"UPDATE Alarms SET CustName = '" & txtName.Text & "', BlkName= '" & txtBlock.Text & "', UnitNum = '" & txtUnit.Text & "', EmerNum= '" & txtNum.Text & "' WHERE CustID = '" & TextBox1.Text & "'"
nameCommand.ExecuteNonQuery()
ds.AcceptChanges()
da.Update(ds, "Alarms")
conn.Close()
MsgBox("Customer Information updated")
btnUpdate.Visible = False
btnInsert.Enabled = True
btnClear.Enabled = True
btnEdit.Visible = True
btnEdit.Enabled = False
txtName.Text = ""
txtBlock.Text = ""
txtUnit.Text = ""
txtNum.Text = ""
DataGridView1.Rows(i).Cells("Selection").Value = DBNull.Value
End If
End If
Next i
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
Dim i As Integer
'Change to txtbox format
Dim ds As DataTable = CType(DataGridView1.DataSource, DataTable)
txtName.Text = ""
txtBlock.Text = ""
txtUnit.Text = ""
txtNum.Text = ""
For i = 0 To (ds.Rows.Count - 1)
If DataGridView1.Rows(i).Cells("Selection").Value IsNot System.DBNull.Value Then
If DataGridView1.Rows(i).Cells("Selection").Value = True Then
txtName.Text += DataGridView1.Rows(i).Cells("CustName").Value.ToString()
txtBlock.Text += DataGridView1.Rows(i).Cells("BlkName").Value.ToString()
txtUnit.Text += DataGridView1.Rows(i).Cells("UnitNum").Value.ToString()
txtNum.Text += DataGridView1.Rows(i).Cells("EmerNum").Value.ToString()
TextBox1.Text += DataGridView1.Rows(i).Cells("CustID").Value.ToString()
txtName.ReadOnly = True
txtBlock.ReadOnly = True
txtUnit.ReadOnly = True
txtNum.ReadOnly = True
btnInsert.Enabled = False
btnClear.Enabled = False
btnEdit.Enabled = True
btnDel.Enabled = True
End If
End If
Next i
End Sub
End Class