datagridview中的清单列表框 [英] checkedlistbox in datagridview

查看:73
本文介绍了datagridview中的清单列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用vb2005在带有复选框列表的datagridview中显示和保存记录.

实际上,我已经为datagridviewcheckedlistbox创建了自定义控件,它显示了选中列表框的数据以及从sql表中检索到的其他数据.
但是问题是,当我检查此检查表框的项目并移动datagridview的另一行时,此检查状态会从所有以前的检查中删除.

这是我的代码.

how to display and save records within datagridview with checkedlist box using vb2005.

actually I had create custom control for datagridviewcheckedlistbox and it show data of checked list box with other data retrieve from sql table.
but problem is that when I check items of this checkedlist box and move other row of datagridview this check state removes from all previous checking.

here is my code.

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

  
    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
      
       

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

            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
   
    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_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

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

    Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
        If abcd.Items.Count > 0 Then
            If intleaverowindex = DataGridView1.CurrentRow.Index Then
                If abcd.Items.Count > 0 Then
                    For inti = 0 To abcd.Items.Count - 1

                        If abcd.GetItemCheckState(inti) = CheckState.Checked Then
                            CheckedListBox1.SetItemChecked(inti, True)
                        ElseIf abcd.GetItemCheckState(inti) = CheckState.Unchecked Then
                            CheckedListBox1.SetItemChecked(inti, False)
                        End If
                    Next
                End If
            End If
        End If
        
    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

    Private Sub DataGridView1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll

    End Sub
    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

        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  

End Class

推荐答案

使用一些技巧将您的CheckedListBoxDataGridView连接"在一起...
DataGridView1_RowLeaveCheckedListBox1_SelectedIndexChanged的正文中,您调用相同的代码部分:
Your CheckedListBox and DataGridView are "connected" together by using some trick...
Inside the body of DataGridView1_RowLeave and CheckedListBox1_SelectedIndexChanged you call the same part of code:
...
If CheckedListBox1.GetItemCheckState(inti) = CheckState.Checked Then
                (DataGridView)abcd.SetItemChecked(inti, True)
...


如果您多次使用同一部分代码,请编写自定义函数!

所以...如果将行从DataGridView移动(到无处?),您应该不会感到惊讶,因为DataGridView的行数与CheckedListBox项数不同.为了避免这种情况,两个控件中的项数必须相等.

阅读有关以下内容的更多信息: CheckedListBox的CheckedItems集合 [


If you use the same part of code more then 1 times, write custom function!

So... if you move row from DataGridView (to nowhere?), you should not be surprise, because the DataGridView count of rows differ of CheckedListBox count of items. To prevent this situation, the count of items in both controls must be equal.

Read more about: CheckedItems collection for CheckedListBox[^]

By the way, your code:

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


不是最佳的!看一下简单的把戏:


is not optimal! Take a look at simple trick:

Dim bChecked AS Boolean = TRUE
....
bChecked = (CheckedListBox1.GetItemCheckState(inti) = CheckState.Checked)
abcd.SetItemChecked(inti, bChecked)



抱歉,我的语言...



Sorry for my language...


这篇关于datagridview中的清单列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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