Finally I got solution for this problem and here is more added for this solution now it is working perfect.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Collections.Generic
'''
Imports System.Object
Imports System.MarshalByRefObject
Imports System.ComponentModel.Component
Imports System.Windows.Forms.Control
Imports System.Windows.Forms.ListControl
Imports System.Windows.Forms.ListBox
Imports System.Windows.Forms.CheckedListBox
'''
''' <remarks>
Public Class CheckedListBoxWithinDatagridviewLatest
Public abc As New DataGridView
Dim colCheckbox As New WindowsApplication27.CheckedListBoxColumn ''DataGridViewCheckBoxColumn()
Private WithEvents CheckedListBox1 As WindowsApplication27.CheckedListBoxEditingControl
Private WithEvents txtFname As DataGridViewTextBoxEditingControl
Private WithEvents txtcategory As DataGridViewTextBoxEditingControl
Private WithEvents txtVehicle As DataGridViewTextBoxEditingControl
Public adap As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
Dim chkedlist As New CheckedListBox
Dim gboolload As Boolean
Private Sub AddTextboxRunTime_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
mstrsql = "select id,FName,Married,physicalHandicap,Local,MobileNo,Category,Vehicle from PrakashKaur .dbo.PersonDetail(nolock) order by id"
'mstrsql = "select top 2 code as 'Column2',Description as 'Column3' from dka.dbo.Languagemaster "
If (gfnintFillDataSet(mstrsql, "Detail", ds, Con)) > 0 Then
Dim strabc As String = ""
mstrsql = "select Description from PrakashKaur .dbo.StatusMaster(nolock) where Description is not null"
If gfnintFillDataSet(mstrsql, "status", ds, Con) > 0 Then
For inti = 0 To ds.Tables("status").Rows.Count - 1
strabc = strabc & ds.Tables("status").Rows(inti).item("Description") & ","
Next
End If
strabc = strabc.Substring(0, Len(strabc) - 1)
Dim str() As String = Split(strabc, ",")
Dim headerHeight As Integer = DataGridView1.ColumnHeadersHeight
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim colvehicle As New DataGridViewTextBoxColumn
colvehicle.DataPropertyName = "Vehicle"
colvehicle.DataPropertyName = "Vehicle"
colvehicle.HeaderText = "Vehicle"
colvehicle.Name = "Vehicle"
If DataGridView1.Columns.Contains("Vehicle") Then
DataGridView1.Columns.Remove("Vehicle")
DataGridView1.Columns.Add(colvehicle)
Else
DataGridView1.Columns.Add(colvehicle)
End If
colvehicle.DisplayIndex = 0
Dim colcategory As New DataGridViewTextBoxColumn
colcategory.DataPropertyName = "Category"
colcategory.HeaderText = "Category"
colcategory.Name = "Category"
If DataGridView1.Columns.Contains("Category") Then
DataGridView1.Columns.Remove("Category")
DataGridView1.Columns.Add(colcategory)
Else
DataGridView1.Columns.Add(colcategory)
End If
colcategory.DisplayIndex = 1
Dim colCheckedlistbox As New WindowsApplication27.CheckedListBoxColumn
' Size the column width so it is wide enough to display the header
colCheckedlistbox.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
colCheckedlistbox.DataPropertyName = "Column1"
colCheckedlistbox.HeaderText = "Column1"
colCheckedlistbox.Name = "Column1"
colCheckedlistbox.Width = 200
colCheckedlistbox.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
colCheckedlistbox.DisplayIndex = 3
If DataGridView1.Columns.Contains("Column1") Then
DataGridView1.Columns.Remove("Column1")
DataGridView1.Columns.Add(colCheckedlistbox)
Else
DataGridView1.Columns.Add(colCheckedlistbox)
End If
CheckedListBox1 = New WindowsApplication27.CheckedListBoxEditingControl
' abcd1 = New WindowsApplication27.CheckedListBoxEditingControl
For inti = 0 To UBound(str)
CheckedListBox1.Items.Add(str(inti))
' abcd1.Items.Add(str(inti))
Next
Dim colFname As New DataGridViewTextBoxColumn
colFname.DataPropertyName = "Column2"
colFname.HeaderText = "Column2"
colFname.Name = "Column2"
If DataGridView1.Columns.Contains("Column2") Then
DataGridView1.Columns.Remove("Column2")
DataGridView1.Columns.Add(colFname)
Else
DataGridView1.Columns.Add(colFname)
End If
colFname.DisplayIndex = 2
Dim colId As New DataGridViewTextBoxColumn
colId.DataPropertyName = "ID"
colId.HeaderText = "ID"
colId.Name = "ID"
If DataGridView1.Columns.Contains("ID") Then
DataGridView1.Columns.Remove("ID")
DataGridView1.Columns.Add(colId)
Else
DataGridView1.Columns.Add(colId)
End If
DataGridView1.Columns("ID").Visible = False
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For inti = 0 To ds.Tables("Detail").Rows.Count - 1
DataGridView1.Rows.Add()
' DataGridView1.Rows(Inti).Cells(0).Value = str
DataGridView1.Item("Vehicle", inti).value = ds.Tables("Detail").Rows(inti).item("Vehicle")
DataGridView1.Item("Category", inti).value = ds.Tables("Detail").Rows(inti).item("Category")
DataGridView1.Item("Column1", inti).value = str
DataGridView1.Item("Column2", inti).value = ds.Tables("Detail").Rows(inti).item("FName")
DataGridView1.Item("ID", inti).value = ds.Tables("Detail").Rows(inti).item("ID")
DataGridView1.Rows(Inti).Height = CheckedListBox1.Height
DataGridView1.ScrollBars = ScrollBars.Both
Next
End If
gboolload = False
DataGridView1.TabStop = False
If DataGridView1.Rows.Count > 1 Then
DataGridView1.Item("Vehicle", 0).Selected = True
End If
'///////////////////For single column
'Dim headerHeight As Integer = DataGridView1.ColumnHeadersHeight
'Dim str() As String = Split("abc,bcd,def,ghi", ",")
'DataGridView1.Rows.Add()
'DataGridView1.Rows(0).Cells(0).Value = str
'Dim chkedlist As New CheckedListBox
'For inti = 0 To UBound(str)
' chkedlist.Items.Add(str(inti))
'Next
'DataGridView1.Rows(0).Height = chkedlist.Height
'///////////////////For single column
End Sub
Dim rec As Rectangle
Private Sub DataGridView1_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
Try
Me.Cursor = Cursors.WaitCursor
Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).ErrorText = ""
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message, "DKA BALAJI -" & Me.Text, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If gboolload = True Then
Exit Sub
End If
intid = 0
rowIndex = DataGridView1.CurrentCell.RowIndex
intid = DataGridView1.Item("ID", DataGridView1.CurrentCell.RowIndex).Value
strfname = DataGridView1.Item("Column2", DataGridView1.CurrentCell.RowIndex).Value
strCategory = DataGridView1.Item("Category", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strVehicle = DataGridView1.Item("Vehicle", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
Dim com As New SqlCommand
com.Connection = Con
If Con.State = ConnectionState.Closed Then
Con.Open()
End If
If intid <> 0 And strfname <> "" Then
mstrsql = "select Married,physicalHandicap,Local,MobileNo from PrakashKaur .dbo.PersonDetail(nolock) where ID=" & intid & " and Fname='" & strfname & "' "
ElseIf intid <> 0 Then
mstrsql = "select Married,physicalHandicap,Local,MobileNo from PrakashKaur .dbo.PersonDetail(nolock) where ID=" & intid & " "
Else
Exit Sub
End If
If (gfnintFillDataSet(mstrsql, "TempStatus", ds, Con)) > 0 Then
'' Dim intval As Integer
For intii = 0 To ds.Tables("TempStatus").Columns.Count - 1
For inti = 0 To CheckedListBox1.Items.Count - 1
'''intval = ds.Tables("TempStatus").Rows(0).Item(intii)
stra = CheckedListBox1.GetItemText(CheckedListBox1.Items.Item(inti))
If stra = "Married" Then
If fn_check(IIf(IsDBNull(ds.Tables("TempStatus").Rows(0).Item("Married")), 0, ds.Tables("TempStatus").Rows(0).Item("Married"))) = True Then
CheckedListBox1.SetItemChecked(Inti, True)
Else
CheckedListBox1.SetItemChecked(Inti, False)
End If
ElseIf stra = "physicalHandicap" Then
If fn_check(IIf(IsDBNull(ds.Tables("TempStatus").Rows(0).Item("physicalHandicap")), 0, ds.Tables("TempStatus").Rows(0).Item("physicalHandicap"))) = True Then
CheckedListBox1.SetItemChecked(Inti, True)
Else
CheckedListBox1.SetItemChecked(Inti, False)
End If
ElseIf stra = "Local" Then
If fn_check(IIf(IsDBNull(ds.Tables("TempStatus").Rows(0).Item("Local")), 0, ds.Tables("TempStatus").Rows(0).Item("Local"))) = True Then
CheckedListBox1.SetItemChecked(Inti, True)
Else
CheckedListBox1.SetItemChecked(Inti, False)
End If
ElseIf stra = "MobileNo" Then
If fn_check(IIf(IsDBNull(ds.Tables("TempStatus").Rows(0).Item("MobileNo")), 0, ds.Tables("TempStatus").Rows(0).Item("MobileNo"))) = True Then
CheckedListBox1.SetItemChecked(Inti, True)
Else
CheckedListBox1.SetItemChecked(Inti, False)
End If
End If
intii = intii + 1
If intii > ds.Tables("TempStatus").Columns.Count - 1 Then
intii = intii - 1
End If
Next
Next
End If
' End If
End Sub
Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Try
Me.Cursor = Cursors.WaitCursor
rowIndex = DataGridView1.CurrentRow.Index
Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).ErrorText = ""
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message, "DKA BALAJI -" & Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub DataGridView1_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
Try
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name <> "Column2" Then
Exit Sub
End If
If ds.Tables("Newstatus").Rows.Count > 0 Then
intid = 0
intleaverowindex = DataGridView1.CurrentCell.RowIndex
intid = ds.Tables("Newstatus").Rows(intleaverowindex).Item("ID")
strfname = DataGridView1.Item("Column2", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strCategory = DataGridView1.Item("Category", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strVehicle = DataGridView1.Item("Vehicle", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
''Coding to check duplicate row
For inti = 0 To ds.Tables("Newstatus").Rows.Count - 1
If inti <> intleaverowindex Then
If strfname = ds.Tables("Newstatus").Rows(inti).Item("FName") Then
MessageBox.Show("Duplicate name not allowed.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
DataGridView1.Item("Column2", intleaverowindex).Value = ""
btnDel.Focus()
Exit Sub
Else
boolisduplicate = False
End If
End If
Next
''If user came from add button
If boolAdd = True Then
Exit Sub
End If
Dim com As New SqlCommand
com.Connection = Con
If Con.State = ConnectionState.Closed Then
Con.Open()
End If
If Len(strfname) <> 0 And Len(intid) = 0 Then
mstrsql = "select max(id) from PrakashKaur .dbo.PersonDetail (nolock)"
com.CommandText = mstrsql
Dim id As Integer = com.ExecuteScalar
id = id + 1
mstrsql = "insert into PrakashKaur .dbo.PersonDetail (ID,FName,Category,Vehicle) values(" & id & ",'" & strfname & "','" & IIf(IsDBNull(strCategory), "", strCategory) & "','" & IIf(IsDBNull(strVehicle), "", strVehicle) & "')"
com.CommandText = mstrsql
com.ExecuteNonQuery()
Me.DataGridView1.Item("ID", DataGridView1.CurrentRow.Index).Value = id
intid = 0
intid = DataGridView1.Item("ID", DataGridView1.CurrentRow.Index).Value
ElseIf Len(strfname) <> 0 And Len(intid) <> 0 Then
mstrsql = "if exists(select * from PrakashKaur .dbo.PersonDetail where ID=" & intid & ") begin update PrakashKaur .dbo.PersonDetail set Fname='" & strfname & "',Category='" & IIf(IsDBNull(strCategory), "", strCategory) & "',Vehicle ='" & IIf(IsDBNull(strVehicle), "", strVehicle) & "' where id=" & intid & " end"
com.CommandText = mstrsql
com.ExecuteNonQuery()
End If
End If
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message, "DKA BALAJI -" & Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
Try
Me.Cursor = Cursors.WaitCursor
e.Cancel = True
Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).ErrorText = e.Context.ToString() + ";" + e.Exception.Message
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message, "DKA BALAJI -" & Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "Column1" Then
CheckedListBox1 = DataGridView1.Columns("Column1").DataGridView.EditingControl
End If
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "Column2" Then
txtFname = DataGridView1.Columns("Column2").DataGridView.EditingControl
End If
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "Category" Then
txtcategory = DataGridView1.Columns("Category").DataGridView.EditingControl
End If
End Sub
Public intii As Integer
Dim stra As String, strfname As String, strCategory As String, strVehicle As String
Dim boolisduplicate As Boolean
Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
If ds.Tables("Newstatus").Rows.Count > 0 Then
intid = 0
intleaverowindex = DataGridView1.CurrentCell.RowIndex
intid = ds.Tables("Newstatus").Rows(intleaverowindex).Item("ID")
strfname = DataGridView1.Item("Column2", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strCategory = DataGridView1.Item("Category", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strVehicle = DataGridView1.Item("Vehicle", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
''If user came from add button
If boolAdd = True Then
Exit Sub
End If
Dim com As New SqlCommand
com.Connection = Con
If Con.State = ConnectionState.Closed Then
Con.Open()
End If
If Len(strfname) <> 0 And Len(intid) = 0 Then
mstrsql = "select max(id) from PrakashKaur .dbo.PersonDetail (nolock)"
com.CommandText = mstrsql
Dim id As Integer = com.ExecuteScalar
id = id + 1
mstrsql = "insert into PrakashKaur .dbo.PersonDetail (ID,FName,Category,Vehicle) values(" & id & ",'" & strfname & "','" & IIf(IsDBNull(strCategory), "", strCategory) & "','" & IIf(IsDBNull(strVehicle), "", strVehicle) & "')"
com.CommandText = mstrsql
com.ExecuteNonQuery()
Me.DataGridView1.Item("ID", DataGridView1.CurrentRow.Index).Value = id
intid = 0
intid = DataGridView1.Item("ID", DataGridView1.CurrentRow.Index).Value
ElseIf Len(strfname) <> 0 And Len(intid) <> 0 Then
mstrsql = "if exists(select * from PrakashKaur .dbo.PersonDetail where ID=" & intid & ") begin update PrakashKaur .dbo.PersonDetail set Fname='" & strfname & "',Category='" & IIf(IsDBNull(strCategory), "", strCategory) & "', Vehicle ='" & IIf(IsDBNull(strVehicle), "", strVehicle) & "' where id=" & intid & " end"
com.CommandText = mstrsql
com.ExecuteNonQuery()
End If
End If
gboolalready = True
End Sub
Dim mintCheckForBlankRowArr() As Int32
Dim boolAdd As Boolean = False
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Try
boolAdd = True
intid = 0
Dim strabc As String = ""
Dim inti As Integer = 0 '', intii As Integer
boolfromAdd = True
mstrsql = "select Description from PrakashKaur .dbo.StatusMaster(nolock) where Description is not null"
If gfnintFillDataSet(mstrsql, "status", ds, Con) > 0 Then
For inti = 0 To ds.Tables("status").Rows.Count - 1
strabc = strabc & ds.Tables("status").Rows(inti).Item("Description") & ","
Next
End If
strabc = strabc.Substring(0, Len(strabc) - 1)
Dim str() As String = Split(strabc, ",")
Dim com As New SqlCommand
com.Connection = Con
If Con.State = ConnectionState.Closed Then
Con.Open()
End If
DataGridView1.Refresh()
''////////////////////
If ds.Tables.Contains("Newstatus") = True Then
If ds.Tables("Newstatus").Rows.Count > 0 Then
rowIndex = DataGridView1.CurrentCell.RowIndex
intid = ds.Tables("Newstatus").Rows(rowindex).item("ID") ''DataGridView1.Item("ID", DataGridView1.CurrentCell.RowIndex).Value.ToString
strfname = ds.Tables("Newstatus").Rows(rowindex).item("FName") ''DataGridView1.Item("Column2", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strCategory = DataGridView1.Item("Category", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strVehicle = DataGridView1.Item("Vehicle", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
For inti = 0 To ds.Tables("Newstatus").Rows.Count - 1
If inti = rowindex Then
If inti < ds.Tables("Newstatus").Rows.Count - 1 And DataGridView1.Rows.Count > 1 Then
intid = 0
intid = ds.Tables("Newstatus").Rows(inti).Item("ID") + 1 '' DataGridView1.Item("ID", inti + 1).Value '''To take next value from current intid
mstrsql = "update PrakashKaur .dbo.PersonDetail set ID =ID + 1 " & " where ID>=" & intid & ""
com.CommandText = mstrsql
com.ExecuteNonQuery()
mstrsql = "insert into PrakashKaur .dbo.PersonDetail (id,FName,Married,physicalHandicap,Local,MobileNo,Category) values(" & intid & ",'',0,0,0,0,'')"
com.CommandText = mstrsql
com.ExecuteNonQuery()
DataGridView1.Rows.Insert(rowindex + 1)
rowIndex = DataGridView1.CurrentCell.RowIndex + 1
DataGridView1.Item("ID", rowIndex).Value = intid
DataGridView1.CurrentCell = DataGridView1.Item("Column2", RowIndex)
CheckedListBox1 = New WindowsApplication27.CheckedListBoxEditingControl
Exit For
ElseIf inti = ds.Tables("Newstatus").Rows.Count - 1 Or DataGridView1.Rows.Count = 1 Then
intid = 0
intid = ds.Tables("Newstatus").Rows(inti).Item("ID") + 1 ''DataGridView1.Item("ID", inti).Value
strfname = DataGridView1.Item("Column2", inti).Value
strCategory = DataGridView1.Item("Category", inti).EditedFormattedValue
strVehicle = DataGridView1.Item("Vehicle", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
mstrsql = "insert into PrakashKaur .dbo.PersonDetail (id,FName,Married,physicalHandicap,Local,MobileNo,Category) values(" & intid & ",'',0,0,0,0,'')"
com.CommandText = mstrsql
com.ExecuteNonQuery()
'DataGridView1.Rows.Add(DataGridView1.CurrentCell.RowIndex + 1)
rowIndex = DataGridView1.CurrentCell.RowIndex + 1
DataGridView1.Rows.Add()
DataGridView1.Item("ID", rowIndex).Value = intid
DataGridView1.CurrentCell = DataGridView1.Item("Column2", RowIndex)
txtFname.Text = DataGridView1.Item("Column2", DataGridView1.CurrentCell.RowIndex).Value
txtcategory.Text = DataGridView1.Item("Category", DataGridView1.CurrentCell.RowIndex).Value
CheckedListBox1 = New WindowsApplication27.CheckedListBoxEditingControl
txtFname.Focus()
Exit For
End If
End If
Next
Else
intid = 0
intid = 1
mstrsql = "insert into PrakashKaur .dbo.PersonDetail (id,FName,Married,physicalHandicap,Local,MobileNo,Category) values(" & intid & ",'',0,0,0,0,'')"
com.CommandText = mstrsql
com.ExecuteNonQuery()
CheckedListBox1 = New WindowsApplication27.CheckedListBoxEditingControl
DataGridView1.Rows.Add()
rowIndex = DataGridView1.CurrentCell.RowIndex
DataGridView1.CurrentCell = DataGridView1.Item("Column2", RowIndex)
End If
Else
intid = 0
intid = 1
mstrsql = "insert into PrakashKaur .dbo.PersonDetail (id,FName,Married,physicalHandicap,Local,MobileNo,Category) values(" & intid & ",'',0,0,0,0,'')"
com.CommandText = mstrsql
com.ExecuteNonQuery()
CheckedListBox1 = New WindowsApplication27.CheckedListBoxEditingControl
DataGridView1.Rows.Add()
rowIndex = DataGridView1.CurrentCell.RowIndex
DataGridView1.CurrentCell = DataGridView1.Item("Column2", RowIndex)
End If
'/////////////////////
rowIndex = DataGridView1.CurrentCell.RowIndex
DataGridView1.Columns("Column1").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Rows(rowIndex).Height = CheckedListBox1.Height
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
DataGridView1.Item("Column1", rowIndex).Value = str
DataGridView1.Item("Column2", rowIndex).Value = ""
DataGridView1.Item("Category", rowindex).value = ""
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
Try
DataGridView1.Refresh()
intid = 0
If ds.Tables.Contains("Newstatus") = False Or DataGridView1.Rows.Count = 0 Then
MessageBox.Show("There is no row to delete.", Me.Text, MessageBoxButtons.OK)
Exit Sub
ElseIf ds.Tables("Newstatus").Rows.Count > 0 Then
intid = 0
intleaverowindex = DataGridView1.CurrentCell.RowIndex
intid = ds.Tables("Newstatus").Rows(intleaverowindex).Item("ID")
strfname = ds.Tables("Newstatus").Rows(intleaverowindex).Item("FName") '' DataGridView1.Item("Column2", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strCategory = ds.Tables("Newstatus").Rows(intleaverowindex).Item("Category")
strVehicle = ds.Tables("Newstatus").Rows(intleaverowindex).Item("Vehicle")
If intid <> 0 Then
If Con.State = ConnectionState.Closed Then
Con.Open()
End If
mstrsql = "Delete from PrakashKaur .dbo.PersonDetail where ID=" & intid & " and FName='" & strfname & "'"
Dim com As New SqlCommand
com.Connection = New SqlConnection
com.Connection = Con
com.CommandText = mstrsql
com.ExecuteNonQuery()
ds.Tables("Newstatus").Rows.RemoveAt(intleaverowindex) ''
DataGridView1.Rows.RemoveAt(intleaverowindex)
Exit Sub
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub CheckedListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.DoubleClick
Try
If gboolload = True Then
Exit Sub
End If
intid = ds.Tables("Newstatus").Rows(DataGridView1.CurrentCell.RowIndex).Item("ID")
strfname = DataGridView1.Item("Column2", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strCategory = DataGridView1.Item("Category", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strVehicle = DataGridView1.Item("Vehicle", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
Dim com As New SqlCommand
com.Connection = Con
If Con.State = ConnectionState.Closed Then
Con.Open()
End If
mstrsql = "if exists(select * from PrakashKaur .dbo.PersonDetail where ID=" & intid & ") begin update PrakashKaur .dbo.PersonDetail set Fname='" & strfname & "', Category='" & IIf(IsDBNull(strCategory), "", strCategory) & "',Vehicle ='" & IIf(IsDBNull(strVehicle), "", strVehicle) & "' where id=" & intid & " end"
com.CommandText = mstrsql
com.ExecuteNonQuery()
If intid <> 0 And CheckedListBox1.SelectedItem Is Nothing = False Then
''Get selected item
If CheckedListBox1.GetItemCheckState(CheckedListBox1.Items.IndexOf(CheckedListBox1.SelectedItem)) = CheckState.Checked Then
stra = CheckedListBox1.SelectedItem ''CheckedListBox1.GetItemText(CheckedListBox1.Items.Item(inti))
mstrsql = "Update PrakashKaur.dbo.PersonDetail set " & stra & "=1 where ID='" & intid & "' and FName='" & strfname & "'"
com.CommandText = mstrsql
com.ExecuteNonQuery()
ElseIf CheckedListBox1.GetItemCheckState(CheckedListBox1.Items.IndexOf(CheckedListBox1.SelectedItem)) = CheckState.Unchecked Then
stra = CheckedListBox1.SelectedItem ''CheckedListBox1.GetItemText(CheckedListBox1.Items.Item(inti))
mstrsql = "Update PrakashKaur.dbo.PersonDetail set " & stra & "=0 where ID='" & intid & "' and FName='" & strfname & "'"
com.CommandText = mstrsql
com.ExecuteNonQuery()
End If
DataGridView1.Item("Column2", DataGridView1.CurrentCell.RowIndex).Selected = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub txtFname_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFname.Validated
Try
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name <> "Column2" Then
Exit Sub
End If
DataGridView1.Refresh()
intid = 0
intid = DataGridView1.Item("ID", DataGridView1.CurrentCell.RowIndex).Value
strfname = DataGridView1.Item("Column2", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strCategory = DataGridView1.Item("Category", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strVehicle = DataGridView1.Item("Vehicle", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub txtcategory_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtcategory.Validated
Try
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name <> "Category" Then
Exit Sub
End If
DataGridView1.Refresh()
DataGridView1.Refresh()
intid = 0
intid = DataGridView1.Item("ID", DataGridView1.CurrentCell.RowIndex).Value
strCategory = DataGridView1.Item("Category", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
strVehicle = DataGridView1.Item("Vehicle", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub txtVehicle_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVehicle.Validated
Try
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name <> "Vehicle" Then
Exit Sub
End If
DataGridView1.Refresh()
DataGridView1.Refresh()
intid = 0
intid = DataGridView1.Item("ID", DataGridView1.CurrentCell.RowIndex).Value
strVehicle = DataGridView1.Item("Vehicle", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class