VB.NET(如何将checkedlistbox添加为datagridviewcolumn) [英] VB.NET(How to add checkedlistbox as datagridviewcolumn)

查看:106
本文介绍了VB.NET(如何将checkedlistbox添加为datagridviewcolumn)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

谁能告诉我如何使用vb(window application)在datagridview中将checkedlistbox添加为datagridviewcolumn.我有此代码,但无法正常工作.问题是,当我更改行索引时,所有先前的选择都已删除.怎么能阻止它.
#代码用于创建自定义控件,以将checkedlistbox创建为datagridview列.

Hello all,

can anyone tell me how can add checkedlistbox as datagridviewcolumn in datagridview using vb(window application). I have code for this but it is not working properly. Problem is that when I change row index all previous selection has removed. How can stop it.
# code for create custom control to create checkedlistbox as datagridview column.

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
'using System.Linq;
Imports System.Text
Imports System.Windows.Forms
Imports System.Collections
Imports System.Collections.ObjectModel

Namespace WindowsApplication27
    Partial Public Class CheckedListBoxColumn
        Inherits DataGridViewColumn
        Public Sub New()
            MyBase.New(New CheckedListBoxCell())
        End Sub
        Public Overrides Property CellTemplate() As DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As DataGridViewCell)
                If value IsNot Nothing AndAlso Not value.[GetType]().IsAssignableFrom(GetType(CheckedListBoxCell)) Then
                    Throw New InvalidCastException("Must be a CheckedListBoxCell")
                End If
                MyBase.CellTemplate = value
            End Set
        End Property
        Friend Sub show()
            Throw New NotImplementedException()
        End Sub

       
    End Class
    Public Class CheckedListBoxCell
        Inherits DataGridViewCell
        Public Sub New()
            MyBase.New()

        End Sub
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
            ' Set the value of the editing control to the current cell value.
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
            Dim ctl As CheckedListBoxEditingControl = TryCast(DataGridView.EditingControl, CheckedListBoxEditingControl)
            InitializeCheckedListBox(ctl, DirectCast(Me.FormattedValue, ICollection))
        End Sub
        Private Sub InitializeCheckedListBox(ByVal ctrl As CheckedListBox, ByVal value As ICollection)

            ctrl.Items.Clear()
            For Each obj As Object In value
                ' ctrl.Items.Add("fd");
                ctrl.Items.Add(obj.ToString())
            Next
            ctrl.Tag = Me.Value
        End Sub
        Public Overrides ReadOnly Property EditType() As Type
            Get
                Return GetType(CheckedListBoxEditingControl)
            End Get
        End Property
        Protected Overrides Function GetFormattedValue(ByVal value As Object, ByVal rowIndex As Integer, ByRef cellStyle As DataGridViewCellStyle, ByVal valueTypeConverter As System.ComponentModel.TypeConverter, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal context As DataGridViewDataErrorContexts) As Object


            If value Is Nothing Then
                Return New List(Of Object)()
            End If
            Return MyBase.GetFormattedValue(value, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)
        End Function
        Public Overrides ReadOnly Property FormattedValueType() As Type
            Get
                Return GetType(ICollection)
            End Get
        End Property
        Public Overrides Property ValueType() As Type
            Get
                Return GetType(ICollection)
            End Get
            Set(ByVal value As Type)

            End Set
        End Property

        Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, _
        ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
            MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, _
             formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
            graphics.FillRectangle(New SolidBrush(cellStyle.BackColor), cellBounds)
           

            If internalControl Is Nothing Then
                internalControl = New CheckedListBox()
            End If
            internalControl.Items.Clear()
            Dim collection As ICollection = TryCast(value, ICollection)
            If collection IsNot Nothing Then

               

                For Each obj As Object In collection
                    internalControl.Items.Add(obj)
                Next



                Dim bmp As New Bitmap(cellBounds.Width, cellBounds.Height)
                internalControl.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
                'stringLocation.X = cellBounds.X + contentBounds.Right + 2;

                'graphics.DrawString(ToolTipText, Control.DefaultFont, System.Drawing.Brushes.Black, stringLocation);

                graphics.DrawImage(bmp, cellBounds, New Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel)
                If abcd.Items.Count > 0 Then

                    For intii = intleaverowindex To intleaverowindex
                        If intleaverowindex >= 0 Then

                            '  internalControl.Items.Clear()
                            For inti = 0 To abcd.Items.Count - 1

                                If abcd.GetItemCheckState(inti) = CheckState.Checked Then
                                    'internalControl.Items.Add(abcd.Items.Item(inti), True)
                                    internalControl.SetItemChecked(inti, True)
                                ElseIf abcd.GetItemCheckState(inti) = CheckState.Unchecked Then
                                    'internalControl.Items.Add(abcd.Items.Item(inti), False)
                                    internalControl.SetItemChecked(inti, False)
                                End If
                            Next

                        End If
                    Next

                End If

            End If

          


        End Sub
        Protected Overrides Sub OnClick(ByVal e As DataGridViewCellEventArgs)
            Me.DataGridView.BeginEdit(False)
            MyBase.OnClick(e)
        End Sub
    End Class
    Class CheckedListBoxEditingControl
        Inherits CheckedListBox
        Implements IDataGridViewEditingControl
        Private dataGridView As DataGridView
        Private valueChanged As Boolean = False
        Public rowIndex As Integer
        Public Sub New()
        End Sub
        ' Implements the IDataGridViewEditingControl.EditingControlFormattedValue
        ' property.
        Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
            Get
                Return Me.Tag
            End Get
            ' this.Tag = value;
            Set(ByVal value As Object)
            End Set

        End Property
        ' Implements the
        ' IDataGridViewEditingControl.GetEditingControlFormattedValue method.
        Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue

           
            Return EditingControlFormattedValue
        End Function
        ' Implements the
        ' IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
            Me.Font = dataGridViewCellStyle.Font
            Me.ForeColor = dataGridViewCellStyle.ForeColor
            Me.BackColor = dataGridViewCellStyle.BackColor
        End Sub
        ' Implements the IDataGridViewEditingControl.EditingControlRowIndex
        ' property.
        Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
            Get
                Return rowIndex
            End Get
            Set(ByVal value As Integer)
                rowIndex = value
            End Set
        End Property
        ' Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
        ' method.
        Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
            ' Let the DateTimePicker handle the keys listed.
            Select Case key And Keys.KeyCode
                Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, Keys.Home, Keys.[End], _
                 Keys.PageDown, Keys.PageUp
                    Return True
                Case Else
                    Return Not dataGridViewWantsInputKey
            End Select
        End Function
        ' Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
        ' method.
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
            ' No preparation needs to be done.
        End Sub
        ' Implements the IDataGridViewEditingControl
        ' .RepositionEditingControlOnValueChange property.
        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
            Get
                Return False
            End Get
        End Property
        ' Implements the IDataGridViewEditingControl
        ' .EditingControlDataGridView property.
        Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
            Get
                Return dataGridView
            End Get
            Set(ByVal value As DataGridView)
                dataGridView = value
            End Set
        End Property
        ' Implements the IDataGridViewEditingControl
        ' .EditingControlValueChanged property.
        Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
            Get
                Return valueChanged
            End Get
            Set(ByVal value As Boolean)
                valueChanged = value
            End Set
        End Property
        ' Implements the IDataGridViewEditingControl
        ' .EditingPanelCursor property.
        Public ReadOnly Property EditingPanelCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
            Get
                Return MyBase.Cursor
            End Get
        End Property

    End Class
End Namespace

# code to use this custom control.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Collections.Generic
''' <summary>
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
''' </summary>
''' <remarks></remarks>

Public Class AddTextboxRunTime
    Public abc As New DataGridView

    Dim colCheckbox As New WindowsApplication27.CheckedListBoxColumn ''DataGridViewCheckBoxColumn()
    Private WithEvents CheckedListBox1 As WindowsApplication27.CheckedListBoxEditingControl
    Public ds As New DataSet
    Public adap As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
    Dim chkedlist As New CheckedListBox

    Public Con As New SqlConnection("Server=192.168.0.232;database=DKA;UID=as;PWD=humtum")
    Private Sub AddTextboxRunTime_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim TextBox8 As New TextBox
        TextBox8.Size = New Size(200, 20)
        TextBox8.Name = "TextBox8"
        TextBox8.Location = New System.Drawing.Point(10, 10)
        Me.Controls.Add(TextBox8)
        Me.Controls.Item("TextBox8").Visible = True

        ' Dim abcd As New WindowsApplication27.CheckedListBoxColumn
        'RadioButton9
        '
        Dim RadioButton9 As New RadioButton
        RadioButton9.AutoSize = True
        RadioButton9.Checked = True
        RadioButton9.Location = New System.Drawing.Point(11, 40)
        RadioButton9.Name = "RadioButton9"
        RadioButton9.Size = New System.Drawing.Size(55, 17)
        RadioButton9.TabIndex = 48
        RadioButton9.TabStop = True
        RadioButton9.Text = "NEAR"
        RadioButton9.UseVisualStyleBackColor = True
        Me.Controls.Add(RadioButton9)

        'checkedlistbox'
        Dim chk As New CheckedListBox
        chk.BackColor = Color.Tan
        chk.Items.Add("a")
        chk.Items.Add("b")
        chk.Items.Add("c")
        chk.Location = New System.Drawing.Point(11, 60)
        Me.Controls.Add(chk)


        ' Fill in the data grid with a List

        Dim list = New List(Of Test)

        list.Add(New Test("Mac", 2200))
        list.Add(New Test("PC", 1100))
        abc.DataSource = list
        abc.Location = New System.Drawing.Point(11, 180)
        abc.Visible = True
        abc.Name = "DGV"
        Dim ChkBox As New DataGridViewCheckBoxColumn

        ChkBox.FlatStyle = FlatStyle.Standard
        abc.Columns.Insert(0, ChkBox)

        ' DataGridViewColumnSelector()

        Me.Controls.Add(abc)
        Me.Controls.Item("DGV").Visible = True

        mstrsql = "select top 2 code as 'Column2'  from  dka.dbo.Languagemaster "
        'mstrsql = "select top 2 code as 'Column2',Description as 'Column3'  from  dka.dbo.Languagemaster "
        If (gfnintFillDataSet(mstrsql, "Language", ds, Con)) > 0 Then

            DataGridView1.DataSource = DBNull.Value
            DataGridView1.DataSource = ds.Tables("Language")


            Dim str() As String = Split("abc,bcd,def,ghi", ",")

            CheckedListBox1 = New WindowsApplication27.CheckedListBoxEditingControl
            abcd = New WindowsApplication27.CheckedListBoxEditingControl

            For inti = 0 To UBound(str)
                CheckedListBox1.Items.Add(str(inti))
                abcd.Items.Add(str(inti))
            Next

            Dim headerHeight As Integer = DataGridView1.ColumnHeadersHeight

            For inti = 0 To DataGridView1.Rows.Count - 1
                'DataGridView1.Rows(Inti).Cells(0).Value = str
                DataGridView1.Rows(Inti).Cells(0).Value = str

                DataGridView1.Rows(Inti).Height = CheckedListBox1.Height
            Next
         
        
           
        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
            rowIndex = DataGridView1.CurrentRow.Index
            If abcd.Items.Count > 0 Then
                If intleaverowindex = rowIndex Then

                    '  internalControl.Items.Clear()
                    For inti = 0 To abcd.Items.Count - 1

                        If abcd.GetItemCheckState(inti) = CheckState.Checked Then
                            'internalControl.Items.Add(abcd.Items.Item(inti), True)
                            CheckedListBox1.SetItemChecked(inti, True)
                        ElseIf abcd.GetItemCheckState(inti) = CheckState.Unchecked Then
                            'internalControl.Items.Add(abcd.Items.Item(inti), False)
                            CheckedListBox1.SetItemChecked(inti, False)
                        End If
                    Next

                End If

            End If
            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_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

            If abcd.Items.Count > 0 Then
                If intleaverowindex = rowIndex Then

                    '  internalControl.Items.Clear()
                    For inti = 0 To abcd.Items.Count - 1

                        If abcd.GetItemCheckState(inti) = CheckState.Checked Then
                            'internalControl.Items.Add(abcd.Items.Item(inti), True)
                            CheckedListBox1.SetItemChecked(inti, True)
                        ElseIf abcd.GetItemCheckState(inti) = CheckState.Unchecked Then
                            'internalControl.Items.Add(abcd.Items.Item(inti), False)
                            CheckedListBox1.SetItemChecked(inti, False)
                        End If
                    Next

                End If

            End If
            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_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        rowIndex = DataGridView1.CurrentRow.Index
        If abcd.Items.Count > 0 Then
            If intleaverowindex = rowIndex Then

                '  internalControl.Items.Clear()
                For inti = 0 To abcd.Items.Count - 1

                    If abcd.GetItemCheckState(inti) = CheckState.Checked Then
                        'internalControl.Items.Add(abcd.Items.Item(inti), True)
                        CheckedListBox1.SetItemChecked(inti, True)
                    ElseIf abcd.GetItemCheckState(inti) = CheckState.Unchecked Then
                        'internalControl.Items.Add(abcd.Items.Item(inti), False)
                        CheckedListBox1.SetItemChecked(inti, False)
                    End If
                Next

            End If

        End If

    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
        CheckedListBox1 = DataGridView1.Columns(0).DataGridView.EditingControl
    End Sub

    Public intenterrowindex As Integer
    Public intii As Integer

    Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
       
        
    End Sub

    Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
        intleaverowindex = DataGridView1.CurrentRow.Index
        For inti = 0 To CheckedListBox1.Items.Count - 1

            If CheckedListBox1.GetItemCheckState(inti) = CheckState.Checked Then
                abcd.SetItemChecked(inti, True)
            ElseIf CheckedListBox1.GetItemCheckState(inti) = CheckState.Unchecked Then
                abcd.SetItemChecked(inti, False)

            End If
        Next
        gboolalready = True

    End Sub

    
    Public Function gfnintFillDataSet(ByVal strQuery As String, ByVal strTbl As String, ByVal dsTemp As DataSet, ByVal conTemp As SqlConnection) As Integer

        'This function is used to search the values according to Passed above SQL Query and Dataset Table Name
        'as Passed Parameter
        Try
            gfnintFillDataSet = 0
            dsClear(dsTemp, strTbl)

            'Passing SQL Query and SQL Connection into SQL DataAdapter
            adap = New SqlDataAdapter(strQuery, conTemp)
            adap.SelectCommand.CommandTimeout = 600
            'Retriving the Records into Dataset Table
            gfnintFillDataSet = adap.Fill(dsTemp, strTbl)
        Catch ex As Exception
            gfnintFillDataSet = 0
            MessageBox.Show(ex.Message, "DKA BALAJI- Module", MessageBoxButtons.OK, _
            MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

        End Try
    End Function
    Public Sub dsClear(ByVal ds As DataSet, ByVal tblName As String)
        Try
            If ds.Tables.Contains(tblName) Then
                ds.Tables(tblName).Clear()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Dim checked As Boolean
    Dim str1 As String

    Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
     
        If CheckedListBox1.GetItemCheckState(DataGridView1.CurrentRow.Index) = CheckState.Checked Then
            abcd.SetItemChecked(DataGridView1.CurrentRow.Index, True)

       End If
   End Sub

End Class



添加了代码标签-LOSMAC [/EDIT]



Code tags added - LOSMAC[/EDIT]

推荐答案

最后,我发现问题的答案在这里

我只是添加了一些这样的代码,现在可以正常工作了.
at last I found answer of my question is here

I just add some code like this and now working fine.
internalControl.Items.Clear()
           Dim collection As ICollection = TryCast(value, ICollection)

           If collection IsNot Nothing Then

               mstrsql = "select id,FName,Married,physicalHandicap,Local,MobileNo from  PrakashKaur .dbo.PersonDetail(nolock) "

               If gfnintFillDataSet(mstrsql, "Newstatus", ds, Con) > 0 Then

                   For inti = 0 To ds.Tables("Newstatus").Rows.Count - 1
                       If inti = rowIndex Then
                           For Each obj As Object In collection
                               If obj = "Married" Then
                                   If fn_check(IIf(IsDBNull(ds.Tables("Newstatus").Rows(inti).item("Married")), 0, ds.Tables("Newstatus").Rows(inti).item("Married"))) = True Then
                                       internalControl.Items.Add(obj, True)

                                   Else
                                       internalControl.Items.Add(obj, False)
                                   End If

                               ElseIf obj = "physicalHandicap" Then
                                   'If fn_check(ds.Tables("Newstatus").Rows(inti).item("physicalHandicap")) = True Then
                                   If fn_check(IIf(IsDBNull(ds.Tables("Newstatus").Rows(inti).item("physicalHandicap")), 0, ds.Tables("Newstatus").Rows(inti).item("physicalHandicap"))) = True Then

                                       internalControl.Items.Add(obj, True)

                                   Else
                                       internalControl.Items.Add(obj, False)
                                   End If

                               ElseIf obj = "Local" Then
                                   'If fn_check(ds.Tables("Newstatus").Rows(inti).item("Local")) = True Then
                                   If fn_check(IIf(IsDBNull(ds.Tables("Newstatus").Rows(inti).item("Local")), 0, ds.Tables("Newstatus").Rows(inti).item("Local"))) = True Then

                                       internalControl.Items.Add(obj, True)

                                   Else
                                       internalControl.Items.Add(obj, False)
                                   End If
                               ElseIf obj = "MobileNo" Then
                                   'If fn_check(ds.Tables("Newstatus").Rows(inti).item("MobileNo")) = True Then
                                   If fn_check(IIf(IsDBNull(ds.Tables("Newstatus").Rows(inti).item("MobileNo")), 0, ds.Tables("Newstatus").Rows(inti).item("MobileNo"))) = True Then

                                       internalControl.Items.Add(obj, True)

                                   Else
                                       internalControl.Items.Add(obj, False)
                                   End If
                               End If
                           Next
                       End If

                   Next
           End If


最后,我得到了解决方案这个问题,现在可以完美地解决此问题了.

导入系统
导入System.Drawing
导入System.Windows.Forms
导入System.Data
导入System.Data.SqlClient
导入System.Collections
导入System.Collections.Generic
''''< summary>
导入System.Object
导入System.MarshalByRefObject
导入System.ComponentModel.Component
导入System.Windows.Forms.Control
导入System.Windows.Forms.ListControl
导入System.Windows.Forms.ListBox
导入System.Windows.Forms.CheckedListBox
''''''
''''''<备注>

公共类CheckedListBoxWithinDatagridviewLatest
公开abc作为新的DataGridView

昏暗的colCheckbox作为新的WindowsApplication27.CheckedListBoxColumn''''DataGridViewCheckBoxColumn()
私有WithEvents CheckedListBox1作为WindowsApplication27.CheckedListBoxEditingControl

私有WithEvents txtFname作为DataGridViewTextBoxEditingControl
私有WithEvents txtcategory作为DataGridViewTextBoxEditingControl
私有WithEvents txtVehicle作为DataGridViewTextBoxEditingControl
公开adap为SqlClient.SqlDataAdapter =新的SqlClient.SqlDataAdapter
昏暗的chkedlist作为新的CheckedListBox
昏暗的gboolload为布尔值

Private Sub AddTextboxRunTime_Load(ByVal发送者作为对象,ByVal e作为System.EventArgs)处理Me.Load


mstrsql =从PrakashKaur中选择id,FName,已婚,physicalHandicap,本地,MobileNo,类别,车辆.id.bo.PersonDetail(nolock)按id排序"

''mstrsql =从dka.dbo.Languagemaster"
中选择前2个代码作为"Column2",描述作为"Column3" 如果(gfnintFillDataSet(mstrsql,"Detail",ds,Con))> 0然后


Dim strabc As String ="
mstrsql =从PrakashKaur .dbo.StatusMaster(nolock)中选择描述,其中描述不为空"

如果gfnintFillDataSet(mstrsql,"status",ds,Con)> 0然后

对于inti = 0到ds.Tables("status").Rows.Count-1
strabc = strabc& ds.Tables("status").Rows(inti).item("Description")& ,"
下一个

如果结束
strabc = strabc.Substring(0,Len(strabc)-1)
Dim str()As String = Split(strabc,,")

昏暗的headerHeight作为整数= DataGridView1.ColumnHeadersHeight


'''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''
作为新的DataGridViewTextBoxColumn昏暗的办公室
colvehicle.DataPropertyName =车辆"
colvehicle.DataPropertyName =车辆"
colvehicle.HeaderText =车辆"
colvehicle.Name =车辆"
如果DataGridView1.Columns.Contains("Vehicle")然后
DataGridView1.Columns.Remove("Vehicle")
DataGridView1.Columns.Add(colvehicle)
其他
DataGridView1.Columns.Add(colvehicle)
如果结束
colvehicle.DisplayIndex = 0


昏暗的类别作为新的DataGridViewTextBoxColumn
colcategory.DataPropertyName =类别"
colcategory.HeaderText =类别"
colcategory.Name =类别"
如果DataGridView1.Columns.Contains("Category")然后
DataGridView1.Columns.Remove("Category")
DataGridView1.Columns.Add(colcategory)
其他
DataGridView1.Columns.Add(colcategory)
如果结束
colcategory.DisplayIndex = 1

昏暗的colCheckedlistbox作为新的WindowsApplication27.CheckedListBoxColumn
''调整列宽的大小以使其足以显示标题
colCheckedlistbox.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader

colCheckedlistbox.DataPropertyName ="Column1"
colCheckedlistbox.HeaderText ="Column1"
colCheckedlistbox.Name ="Column1"
colCheckedlistbox.Width = 200
colCheckedlistbox.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
colCheckedlistbox.DisplayIndex = 3



如果DataGridView1.Columns.Contains("Column1")然后
DataGridView1.Columns.Remove("Column1")
DataGridView1.Columns.Add(colCheckedlistbox)
其他
DataGridView1.Columns.Add(colCheckedlistbox)
如果结束


CheckedListBox1 = New WindowsApplication27.CheckedListBoxEditingControl
'' abcd1 = New WindowsApplication27.CheckedListBoxEditingControl

For inti = 0 To UBound(str)
CheckedListBox1.Items.Add(str(inti))
'' abcd1.Items.Add(str(inti))
下一个

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)
其他
DataGridView1.Columns.Add(colFname)
如果结束
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)
其他
DataGridView1.Columns.Add(colId)
如果结束
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
下一个

如果结束
gboolload = False
DataGridView1.TabStop = False
If DataGridView1.Rows.Count > 1然后

DataGridView1.Item("Vehicle", 0).Selected = True
如果结束

''///////////////////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
结束子

Dim rec As Rectangle

Private Sub DataGridView1_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
试试
Me.Cursor = Cursors.WaitCursor
Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).ErrorText = ""

异常捕获
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message, "DKA BALAJI -" & Me.Text, _
MessageBoxButtons.OK, MessageBoxIcon.Error)

终于
Me.Cursor = Cursors.Default

结束尝试
结束子

私有子DataGridView1_CellClick(ByVal发送者作为对象,ByVal e作为System.Windows.Forms.DataGridViewCellEventArgs)处理DataGridView1.CellClick
If gboolload = True Then
退出子
如果结束

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()
如果结束
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然后
mstrsql = "select Married,physicalHandicap,Local,MobileNo from PrakashKaur .dbo.PersonDetail(nolock) where ID=" & intid & " "
其他
退出子
如果结束

If (gfnintFillDataSet(mstrsql, "TempStatus", ds, Con)) > 0然后
'''' 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)

其他
CheckedListBox1.SetItemChecked(Inti, False)

如果结束

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)

其他
CheckedListBox1.SetItemChecked(Inti, False)

如果结束

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)

其他
CheckedListBox1.SetItemChecked(Inti, False)

如果结束
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)

其他
CheckedListBox1.SetItemChecked(Inti, False)

如果结束
如果结束
intii = intii + 1
If intii > ds.Tables("TempStatus").Columns.Count - 1 Then
intii = intii - 1
如果结束
下一个
下一个
如果结束


'' End If
结束子


Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

试试
Me.Cursor = Cursors.WaitCursor
rowIndex = DataGridView1.CurrentRow.Index

Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).ErrorText = ""

异常捕获
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message, "DKA BALAJI -" & Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)

终于
Me.Cursor = Cursors.Default

结束尝试
结束子

Private Sub DataGridView1_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
试试

If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name <> "Column2" Then
退出子
如果结束
If ds.Tables("Newstatus").Rows.Count > 0然后

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()
退出子
其他
boolisduplicate = False
如果结束
如果结束
下一个

''''If user came from add button
If boolAdd = True Then
退出子
如果结束
Dim com As New SqlCommand
com.Connection = Con
If Con.State = ConnectionState.Closed Then
Con.Open()
如果结束


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然后

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()
如果结束
如果结束

异常捕获
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message, "DKA BALAJI -" & Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
终于
Me.Cursor = Cursors.Default

结束尝试
结束子

Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
试试
Me.Cursor = Cursors.WaitCursor
e.Cancel = True
Me.DataGridView1.Rows(Me.DataGridView1.CurrentCell.RowIndex).ErrorText = e.Context.ToString() + ";" + e.Exception.Message

异常捕获
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message, "DKA BALAJI -" & Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)

终于
Me.Cursor = Cursors.Default

结束尝试
结束子

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
如果结束
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "Column2" Then
txtFname = DataGridView1.Columns("Column2").DataGridView.EditingControl
如果结束
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "Category" Then
txtcategory = DataGridView1.Columns("Category").DataGridView.EditingControl
如果结束
结束子


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然后

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
退出子
如果结束
Dim com As New SqlCommand
com.Connection = Con
If Con.State = ConnectionState.Closed Then
Con.Open()
如果结束


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然后

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()
如果结束
如果结束

gboolalready = True
结束子
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
试试
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然后

For inti = 0 To ds.Tables("status").Rows.Count - 1
strabc = strabc & ds.Tables("status").Rows(inti).Item("Description") & ,"
下一个
如果结束

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()
如果结束
DataGridView1.Refresh()



''''////////////////////
If ds.Tables.Contains("Newstatus") = True Then

If ds.Tables("Newstatus").Rows.Count > 0然后

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然后
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
如果结束
如果结束
下一个
其他
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)

如果结束

其他
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)
如果结束

''/////////////////////
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 = ""

异常捕获
MessageBox.Show(ex.Message)
结束尝试

结束子

Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
试试
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)
退出子

ElseIf ds.Tables("Newstatus").Rows.Count > 0然后

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然后

If Con.State = ConnectionState.Closed Then
Con.Open()
如果结束
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)
退出子

如果结束
如果结束


异常捕获
MessageBox.Show(ex.Message)
结束尝试
结束子


Private Sub CheckedListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.DoubleClick
试试


If gboolload = True Then
退出子
如果结束
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()
如果结束
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()
如果结束
DataGridView1.Item("Column2", DataGridView1.CurrentCell.RowIndex).Selected = True
如果结束

异常捕获
MessageBox.Show(ex.Message)
结束尝试
结束子


Private Sub txtFname_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFname.Validated
试试
If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name <> "Column2" Then
退出子
如果结束

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


异常捕获
MessageBox.Show(ex.Message)
结束尝试
结束子




Private Sub txtcategory_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtcategory.Validated
试试


If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name <> "Category" Then
退出子
如果结束


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

异常捕获
MessageBox.Show(ex.Message)
结束尝试
结束子

Private Sub txtVehicle_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVehicle.Validated
试试


If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name <> "Vehicle" Then
退出子
如果结束


DataGridView1.Refresh()
DataGridView1.Refresh()
intid = 0
intid = DataGridView1.Item("ID", DataGridView1.CurrentCell.RowIndex).Value
strVehicle = DataGridView1.Item("Vehicle", DataGridView1.CurrentCell.RowIndex).EditedFormattedValue

异常捕获
MessageBox.Show(ex.Message)
结束尝试

结束子
结束类
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
'''''' <summary>
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


这篇关于VB.NET(如何将checkedlistbox添加为datagridviewcolumn)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆