如何根据另一个DataGridviewComboboxColumn的选择填充DataGridviewComboboxColumn [英] How to Fill DataGridviewComboboxColumn based on Selection of another DataGridviewComboboxColumn

查看:57
本文介绍了如何根据另一个DataGridviewComboboxColumn的选择填充DataGridviewComboboxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生,



在我使用Vb.net的Windows应用程序中,在datagridview中我有两个不同的DataGridViewComboBoxColumn



我已将数据集附加到第一个DataGridViewComboBoxColumn ..

现在根据此组合中的选择项我想将数据库附加到第二个DataGridViewComboBoxColumn





例如:

以下是DataGridView中的2个DatagridviewCombobox列

 CountryCombobox CityCombobox(所选项目)是'印度'从
第一个组合是
'CountryCombobox')
--------------- ---------- --------
美国德里
英国孟买
印度Kolkota
Banglore





请帮帮我...



谢谢......

解决方案

使用 dataGridView_CurrentCellDirtyStateChanged 事件来实现这个目的/>
检查currentcell的列是你的第一个组合列

然后根据当前行的第二个组合框单元的值更改数据源,



例如



 私人  Sub  dgv1_CurrentCellDirtyStateChanged( ByVal  sender  As  对象 ByVal  e  As  System.EventArgs)句柄 dgv1.CurrentCellDirtyStateChanged 
尝试
如果(dgv1 .IsCurrentCellDirty)然后
dgv1.Com mitEdit(DataGridViewDataErrorContexts.Commit)
如果 dgv1.CurrentCell.ColumnIndex = 1 然后
如果 dgv1.CurrentCell.Value<> 0 然后
Dim t = DirectCast (dgv1.Rows(dgv1.CurrentCell.RowIndex).Cells( 3 ),DataGridViewComboBoxCell)
t.DataSource = DBGetData( 从tbl1中选择col1,col2,其中col3 =& dgv1.CurrentCell.Value)
t.DisplayMember = col1
t .ValueMember = col2
t.Value = 0
结束 如果
结束 如果
结束 如果
Catch
结束 尝试
结束 Sub





快乐编码!

:)


您需要重新选择数据,否则两个控件将同步。您可以复制数据源而不是查询数据库两次。


如果您使用的是Dirtied方法,那么最好使用ColumIndex而不是单元格,因为如果要添加新行的单元格值没有任何应用程序将抛出错误。以下是基于解决方案2的修改后的解决方案

 私人  Sub  dgTrans_CurrentCellDirtyStateChanged( ByVal  sender  As   Object  ByVal  e  As  System.EventArgs) Handles  dgTrans.CurrentCellDirtyStateChanged 
尝试
如果(dgTrans.IsCurrentCellDirty) 然后
dgTrans.CommitEdit(DataGridViewDataErrorContexts.Commit)
如果 dgTrans.CurrentCell。 ColumnIndex = 1 然后
如果 dgTrans.CurrentCell.Value<> ; 0 然后
Dim t As DataGridViewComboBoxColumn = dgTrans.Columns( 3
t.DataSource = DBGetData( 从tbl1中选择col1,col2,其中col3 =& dgTrans.CurrentCell.Value)
t。 DisplayMember = col1
t.ValueMember = col2
t.Value = 0
< span class =code-keyword>结束 如果
结束 如果
结束 如果
Catch
结束 尝试
结束


Respected Sir,

In my Windows Application using Vb.net, In the datagridview i have two different DataGridViewComboBoxColumn

I have attached dataset to 1st DataGridViewComboBoxColumn..
now as per selection item from this combo i want to attach database to 2nd DataGridViewComboBoxColumn


example:
Following are 2 DatagridviewCombobox columns in DataGridView

CountryCombobox         CityCombobox (Selected Item Is 'India' From                                        
                                                       1st combo that is
                                                       'CountryCombobox')
---------------        ------------------  
US                     Delhi
UK                     Mumbai  
India                  Kolkota
                       Banglore



Please help me...

Thank you...

解决方案

use dataGridView_CurrentCellDirtyStateChanged event for achieve this
check currentcell's column is your first combo's column
then as per value change datasource of second combobox cell of that current row,

e.g.

Private Sub dgv1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv1.CurrentCellDirtyStateChanged
        Try
            If (dgv1.IsCurrentCellDirty) Then
                dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit)
                If dgv1.CurrentCell.ColumnIndex = 1 Then
                    If dgv1.CurrentCell.Value <> 0 Then
                        Dim t = DirectCast(dgv1.Rows(dgv1.CurrentCell.RowIndex).Cells(3), DataGridViewComboBoxCell)
                        t.DataSource = DBGetData("select col1,col2 from tbl1 where col3=" & dgv1.CurrentCell.Value)
                        t.DisplayMember = "col1"
                        t.ValueMember = "col2"
                        t.Value = 0
                    End If
                End If
            End If
        Catch
        End Try
    End Sub



Happy Coding!
:)


You need to reselect the data, otherwise the two controls will synchronise. You can make a copy of the data source rather than query the DB twice.


If you are using the Dirtied method then its better to use the ColumIndex rather that the cell because if you are adding new Rows the cell value is nothing has the application will throw an error. Here is my Modified solution based on Solution 2

Private Sub dgTrans_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgTrans.CurrentCellDirtyStateChanged
       Try
           If (dgTrans.IsCurrentCellDirty) Then
               dgTrans.CommitEdit(DataGridViewDataErrorContexts.Commit)
               If dgTrans.CurrentCell.ColumnIndex = 1 Then
                   If dgTrans.CurrentCell.Value <> 0 Then
                       Dim t As DataGridViewComboBoxColumn = dgTrans.Columns(3)
                       t.DataSource = DBGetData("select col1,col2 from tbl1 where col3=" & dgTrans.CurrentCell.Value)
                       t.DisplayMember = "col1"
                       t.ValueMember = "col2"
                       t.Value = 0
                   End If
               End If
           End If
       Catch
       End Try
   End Sub


这篇关于如何根据另一个DataGridviewComboboxColumn的选择填充DataGridviewComboboxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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