数据网格视图组合框查询 [英] data Grid View Combo Box query

查看:68
本文介绍了数据网格视图组合框查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview有2个按钮ADD和VIEW。

点击VIEW按钮,它从.csv文件中获取数据。

现在我想改变在运行时进入组合框的列意味着如果我单击ADD按钮它将显示一个新行,在2列上它显示组合框中有一些下拉值(用户从下拉列表中选择值)。



以下代码能够将文件读入数据网格,但是我应该怎么做才能添加一个带有组合框的新行。

I have a datagridview having 2 buttons ADD and VIEW.
On click of VIEW button, it fetches the data from .csv file.
Now I want to change a column into combobox at run time means if I click on ADD button it will show a new row and on 2 columns it shows the combobox which is having some dropdown values in it(user selects the value from dropdown).

The below code is able to read a file into datagrid but what should I do to add a new row with combobox.

Public Class frmGrid
    Dim dt As DataTable = New DataTable
    Dim ds As DataSet = New DataSet
    Dim dv As DataView = New DataView
    ' Dim dgv2Cb As New DataGridViewComboBoxCell
    Dim ColumnCB As New DataGridViewComboBoxColumn
    Dim ChkBox As New DataGridViewCheckBoxColumn
    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim fName As String = ""
    Dim strline As String
    Dim charArray As Char = ","
    Dim strArray() As String

    OpenFileDialog1.FilterIndex = 2
    OpenFileDialog1.RestoreDirectory = True

    If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then

        fName = OpenFileDialog1.FileName
        Dim aFile As FileStream = New FileStream(fName, FileMode.Open)
        Dim sr As StreamReader = New StreamReader(aFile)

        strline = sr.ReadLine
        strArray = strline.Split(charArray)
        For i As Integer = 0 To strArray.GetUpperBound(0)
            dt.Columns.Add(strArray(i).Trim())
            dt.TableName = "BPSurv"
           
        Next
        Me.grdTest.DataSource = dt
        strline = sr.ReadLine()
        ChkBox.Name = "Select"
        ChkBox.Selected = False
        grdTest.Columns.Add(ChkBox)
        While strline <> Nothing
            strArray = strline.Split(charArray)
            Dim dr As DataRow = dt.NewRow
            'Dim dc As DataColumn
            For i As Integer = 0 To strArray.GetUpperBound(0)
                'Dim dgvc As DataGridViewComboBoxCell
                dr(i) = strArray(i).Trim
            Next
            dt.Rows.Add(dr)
            strline = sr.ReadLine()

        End While

    End If
    
End Sub

推荐答案

你可以做的一件事就是添加组合框在设计时,将其visible属性设置为false,然后在单击ADD按钮时在运行时将其设置为true。
One thing that you could do is add the combobox columns at design time, set their visible property to false, then set it to true at run-time when the ADD button is clicked.


这篇关于数据网格视图组合框查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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