在datagridview中添加多个Datagridviewcombobox列 [英] Adding Multiple Datagridviewcomboboxcolumn in datagridview

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

问题描述

我试图在win应用程序的datagrid中添加多个Datagridviewcomboboxcolumn,并从值中更新这些列.


我通过将不同的数据表设置为数据源来添加了组合框列,但是当我更改一个组合框的值时,所有组合框值都会更改

I am trying to add multiple Datagridviewcomboboxcolumn in datagrid in win application and to update those columns also from the value.


I have added the combobox column by setting different datatable as datasource but when i change the value of one combobox then all the combobox value changes

does anyone one has the code or application to help in this.

推荐答案

Public Class Form1
    Dim mcn As MySqlConnection
    Dim mcm As MySqlCommand
    Dim mda As New MySqlDataAdapter
    Dim mdt As New DataTable
    Dim mds As New DataSet
    Dim str As String
    Dim value As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        str = "server=localhost;uid=root;pwd=bss;database=cdetail"
        mcn = New MySqlConnection
        mcn.ConnectionString = str
        mcn.Open()
        FillGridView()

        End Sub

    Private Sub FillGridView()

        '--- Adding the values into the combo box through the List---
        Dim cmbOpt1 As DataGridViewComboBoxColumn = CType(DataGridView1.Columns("loc"), DataGridViewComboBoxColumn)
        Dim cmbOpt2 As DataGridViewComboBoxColumn = CType(DataGridView1.Columns("PRODUCT"), DataGridViewComboBoxColumn)
        Try

            str = "select Name,fullname from others order by Name"
            mda = New MySqlDataAdapter(str, mcn)
            Dim mdt1 As New DataTable
            mda.Fill(mdt1)
            'If loc.DataSource Is Nothing Then
            cmbOpt1.DataSource = mdt1
            cmbOpt1.DisplayMember = "Name"
            cmbOpt1.ValueMember = "fullname"

            Dim str2 As String
            Dim mda1 As New MySqlDataAdapter
            Dim mdt2 As New DataTable
            str2 = "select productname,fullname from product order by productname"
            mda = New MySqlDataAdapter(str2, mcn)
            mda.Fill(mdt2)
            cmbOpt2.DataSource = mdt2
            cmbOpt2.DisplayMember = "productname"
            cmbOpt2.ValueMember = "fullname"

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Class


Public SqlCon As New System.Data.SqlClient.SqlConnection("server=??? Servername or . for local??;user id=???UserName???;pwd=???Password???;data source=???Server Name or .???;database=?? Database name ???")
Dim tbl As New DataTable
Dim dvw As DataView
Dim b As Boolean
Dim cmd As New SqlClient.SqlCommand("SELECT * FROM Personalinfo", sqlcon)
sqlcon.Open()
Dim sdr As SqlClient.SqlDataReader = cmd.ExecuteReader
Dim fc As Integer
While (sdr.Read)
    'populating columns
    If Not b Then
        For fc = 0 To sdr.FieldCount - 1
            tbl.Columns.Add(sdr.GetName(fc))
        Next
        b = True
    End If
    'populating rows
    Dim row As DataRow = tbl.NewRow
    For fc = 0 To sdr.FieldCount - 1
        row(fc) = sdr(fc)
    Next
    tbl.Rows.Add(row)
End While
dvw = New DataView(tbl)
Me.dgrPersonalInfo.DataSource = dvw
sqlcon.Close()


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

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