如果该字段不为null,如何从数据库中获取组合数据块的值 [英] How to get value in comboboxcellof datagridview from database if that field is not null

查看:71
本文介绍了如果该字段不为null,如何从数据库中获取组合数据块的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据集填充datagridview(10列)。如果行的数据库表中的某些值(比如caste)为null,则我在运行时将第7列从文本框更改为comboboxcell。如果caste字段在数据库中不为null,那么comboboxcell应显示该特定行的值。下面是我尝试过的代码:

 对于 每个作为 DataGridViewRow  dgvUserDetails.Rows 
Dim cb As DataGridViewComboBoxCell = DataGridViewComboBoxCell
如果 row.Index> 0 然后
如果 dgvUserDetails .Rows(row.Index).Cells( 7 )。Value.ToString = IsDBNull(dgvUserDetails.Rows(row.Index) )。细胞( 7 )。值)然后
Dim con As OdbcConnection = OdbcConnection
sql = 从类别中选择描述,其中Catgry = 1
con.ConnectionString = connstring
如果 con.State = ConnectionState.Open 那么 con.Close()
con.Open()
Dim da As OdbcDataAdapter = OdbcDataAdapter(sql,con)
如果 ds.Tables.Contains( Caste然后
如果 ds.Tables( Caste)。Rows.Count> 0 然后
ds.Tables( Caste)。Rows.Clear()
结束 如果
结束 如果

da.Fill(ds, Caste
cb.DataSource = ds。表( Caste
cb.DisplayMember = 描述
dgvUserDetails.Rows(row.Index).Cells( 7 )= cb

其他
sql = 从类别中选择描述,其中ID =& dgvUserDetails.Rows(row.Index).Cells( 7 )。Value.ToString&
如果 rs .State = 1 然后 rs.Close()
rs.Open(sql,MainCon, 1 3
如果 rs.EOF 然后
gCaste = rs.Fields( 0 )。值
dgvUserDetails.Rows(row.Index).Cells( 7 )。Value = gCaste.ToString

结束 如果
结束 如果
结束 如果
下一步





我在数据库表中的第一条记录对caste字段有价值。但是在执行此行之后调试此代码

dgvUserDetails.Rows(row.Index).Cells(7).Value = gCaste.ToString

调试器进入dataError事件of datagridview.Can any1让我知道wat是这个代码中的问题?请帮帮我。急。

解决方案

如果我没记错你需要一个模板列和显示在数据网格行加载过程中每行需要哪个对象。



此网格是否有多行,每行可以有一个文本框或一个组合框,或者所有的行都有相同的东西?



我总是显示组合框,只需将组合框设置为列中我的数据值。 / blockquote>

然后当您将值返回到datagrid时,您需要通过执行查找值来设置值在组合框内。我手边没有具体的代码,但是如果你想设置一个特定值的组合框,你应该找到一些不错的样本。


 私有  Sub  FillCasteCol()
Dim con As OdbcConnection = OdbcConnection
对于 每个 As DataGridViewRow In dgvUserDetails.Rows
如果 row.Index< dgvUserDetails.Rows.Count - 1 然后
Dim cmbcell As DataGridViewComboBoxCell
dgvUserDetails.Rows(row.Index)。单元格( 7 )= cmbcell
sql = 从数据文件中选择种姓,其中Part_No =& dgvUserDetails.Item( Part_No,row.Index).Value& 和SLNOINPART =& dgvUserDetails.Item( SLNOINPART,row.Index).Value& 和fullname ='& dgvUserDetails.Item( Name,row.Index).Value& '
如果 rs.State = 1 然后 rs.Close()
rs.Open(sql,MainCon , 1 3
如果 rs.EOF IsDBNull(rs.Fields( 0 )。值)然后
gCaste = rs.Fields( 0 )。值
cmbcell.Value = gCaste
sql = 从类别中选择描述,其中Catgry = 1和描述<>'& gCaste& '
con.ConnectionString = connstring
如果 con.State = ConnectionState.Open 那么 con.Close()
con.Open()
< span class =code-keyword> Dim da As OdbcDataAdapter = OdbcDataAdapter(sql ,con)
如果 ds.Tables.Contains( Caste然后
如果 ds.Tables( Caste)。Rows.Count> 0 然后
ds.Tables( Caste)。Rows.Clear()
结束 如果
结束 如果

da.Fill(ds, Caste
cmbcell.Items.Add( gCaste)
cmbcell.DataSource = ds.Tables( Caste
cmbcell.DisplayMember = ds.Tables( Caste)。列( 0 )。ColumnName.ToString
con.Close()
Else
sql = 从类别中选择描述,其中Catgry = 1
con .ConnectionString = connstring
如果 con.State = ConnectionState.Open 那么 con.Close()
con.Open()
Dim da As OdbcDataAdapter = OdbcDataAdapter(sql,con)
如果 ds.Tables.Contains( Caste然后
如果 ds.Tables( Caste)。Rows.Count> 0 然后
ds.Tables( Caste)。Rows.Clear()
结束 如果
结束 如果

da.Fill(ds, Caste
cmbcell.DataSource = ds。表( Caste
cmbcell.DisplayMember = ds.Tables( Caste)。列( 0 )。ColumnName。 ToString
结束 如果

结束 如果
con.Close()
下一步
结束 Sub


I am populating datagridview(10 columns) with dataset.I am changing 7th column from textbox to comboboxcell at run time if some value(say caste) in database table for row is null. If caste field is not null in database, then comboboxcell should display that value for that particular row.Below is the code I have tried:

For Each row As DataGridViewRow In dgvUserDetails.Rows
            Dim cb As DataGridViewComboBoxCell = New DataGridViewComboBoxCell
            If row.Index > 0 Then
                If dgvUserDetails.Rows(row.Index).Cells(7).Value.ToString = "" Or Not IsDBNull(dgvUserDetails.Rows(row.Index).Cells(7).Value) Then
                    Dim con As OdbcConnection = New OdbcConnection
                    sql = "Select Description from Category where Catgry = 1"
                    con.ConnectionString = connstring
                    If con.State = ConnectionState.Open Then con.Close()
                    con.Open()
                    Dim da As OdbcDataAdapter = New OdbcDataAdapter(sql, con)
                    If ds.Tables.Contains("Caste") Then
                        If ds.Tables("Caste").Rows.Count > 0 Then
                            ds.Tables("Caste").Rows.Clear()
                        End If
                    End If
                    
                    da.Fill(ds, "Caste")
                    cb.DataSource = ds.Tables("Caste")
                    cb.DisplayMember = "Description"
                    dgvUserDetails.Rows(row.Index).Cells(7) = cb

                Else
                    sql = "Select Description from Category where ID = " & dgvUserDetails.Rows(row.Index).Cells(7).Value.ToString & ""
                    If rs.State = 1 Then rs.Close()
                    rs.Open(sql, MainCon, 1, 3)
                    If Not rs.EOF Then
                        gCaste = rs.Fields(0).Value
                        dgvUserDetails.Rows(row.Index).Cells(7).Value = gCaste.ToString

                    End If
                End If
            End If
        Next



My first record in database table have value for caste field.But debugging this code,after executing this line
dgvUserDetails.Rows(row.Index).Cells(7).Value = gCaste.ToString
debugger goes to dataError event of datagridview.Can any1 let me know wat is the problem in this code?Please help me.its urgent.

解决方案

If I remember right you need to have a template column and show which object is needed for each row during the datagrid row loading process.

Does this grid have more than one row and can each row have either a textbox or a combobox, or will all the rows have the same thing?

I always showed the combobox and just set the combobox to the value of my data in the column.


Then when you return the value to the datagrid you need to do a set the value by doing a find value within the combobox. I don''t have the specific code on hand but if you look for "set a combo box to a specific value" you should find some decent samples.


Private Sub FillCasteCol()
        Dim con As OdbcConnection = New OdbcConnection
        For Each row As DataGridViewRow In dgvUserDetails.Rows
            If row.Index < dgvUserDetails.Rows.Count - 1 Then
                Dim cmbcell As New DataGridViewComboBoxCell
                dgvUserDetails.Rows(row.Index).Cells(7) = cmbcell
                sql = "Select caste from datafile where Part_No = " & dgvUserDetails.Item("Part_No", row.Index).Value & " and SLNOINPART = " & dgvUserDetails.Item("SLNOINPART", row.Index).Value & " and fullname = '" & dgvUserDetails.Item("Name", row.Index).Value & "'"
                If rs.State = 1 Then rs.Close()
                rs.Open(sql, MainCon, 1, 3)
                If Not rs.EOF And Not IsDBNull(rs.Fields(0).Value) Then
                    gCaste = rs.Fields(0).Value
                    cmbcell.Value = gCaste
                    sql = "Select Description from Category where Catgry = 1 and Description <> '" & gCaste & "'"
                    con.ConnectionString = connstring
                    If con.State = ConnectionState.Open Then con.Close()
                    con.Open()
                    Dim da As OdbcDataAdapter = New OdbcDataAdapter(sql, con)
                    If ds.Tables.Contains("Caste") Then
                        If ds.Tables("Caste").Rows.Count > 0 Then
                            ds.Tables("Caste").Rows.Clear()
                        End If
                    End If

                    da.Fill(ds, "Caste")
                    cmbcell.Items.Add(gCaste)
                    cmbcell.DataSource = ds.Tables("Caste")
                    cmbcell.DisplayMember = ds.Tables("Caste").Columns(0).ColumnName.ToString
                    con.Close()
                Else
                    sql = "Select Description from Category where Catgry = 1"
                    con.ConnectionString = connstring
                    If con.State = ConnectionState.Open Then con.Close()
                    con.Open()
                    Dim da As OdbcDataAdapter = New OdbcDataAdapter(sql, con)
                    If ds.Tables.Contains("Caste") Then
                        If ds.Tables("Caste").Rows.Count > 0 Then
                            ds.Tables("Caste").Rows.Clear()
                        End If
                    End If

                    da.Fill(ds, "Caste")
                    cmbcell.DataSource = ds.Tables("Caste")
                    cmbcell.DisplayMember = ds.Tables("Caste").Columns(0).ColumnName.ToString
                End If

            End If
            con.Close()
        Next
    End Sub


这篇关于如果该字段不为null,如何从数据库中获取组合数据块的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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