从带有vb.net中csv的数据的组合框中选择 [英] selecting from a combo box with data from csv in vb.net

查看:286
本文介绍了从带有vb.net中csv的数据的组合框中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv文件,我加载在数据网格视图中的组合框。我的csv是这样的:

I have a .csv file that I load in a combo box inside a data grid view. My csv is like this:

prodname,prodcode,amt
prodname1,prodcode1,amt1
prodname2,prodcode2,amt2

prodname

我想要的是当我选择 prodname1 prodcode1 amt1 将显示在数据网格视图的同一行中。

What i want is when I select prodname1, prodcode1 and amt1 will be displayed on the same row in the data grid view. I am just starting to fumble around in vb.net and will be grateful if someone help me.

这是我现在所拥有的:

    Dim cmb As New DataGridViewComboBoxColumn()
    cmb.HeaderText = "Product Name"
    cmb.Name = "cmb"
    DataGridView2.Columns.Add(cmb) 'code for a combobox column in datagridview

    Dim filename As String
    Dim dt As New DataTable
    filename = "/file/path/here"
    Dim sr As New IO.StreamReader(filename)

    Dim newline() As String = sr.ReadLine.Split(","c)
    dt.Columns.AddRange({New DataColumn(newline(0)), _
                         New DataColumn(newline(1))})
    While (Not sr.EndOfStream)
        newline = sr.ReadLine.Split(",")
        Dim newrow As DataRow = dt.NewRow
        cmb.Items.Add(newline(0))
        newrow.ItemArray = {newline(1)}
        dt.Rows.Add(newrow)
    End While
    DataGridView2.DataSource = dt






UPDATE:


UPDATE:

我根据Alex的回答更新了我的代码,但我仍然收到错误。

I updated my code as per Alex's answer but I still get an error.

    Dim cmb As New DataGridViewComboBoxColumn()
    cmb.HeaderText = "Product Name"
    cmb.Name = "cmb"
    DataGridView1.Columns.Add(cmb)

    AddHandler DataGridView1.CellValueChanged, AddressOf DataGridView1_OnCellValueChanged
    AddHandler DataGridView1.CurrentCellDirtyStateChanged, AddressOf DataGridView1_OnCurrentCellDirtyStateChanged

    Dim filename As String

    filename = "/path/to/file"
    Dim sr As New IO.StreamReader(filename)

    Dim newline() As String = sr.ReadLine.Split(","c)
    dt.Columns.AddRange({New DataColumn(newline(0)), _
    New DataColumn(newline(1))})

     While (Not sr.EndOfStream)
        newline = sr.ReadLine.Split(","c)
        Dim newrow As DataRow = dt.NewRow
        cmb.Items.Add(newline(0))
        'newrow.ItemArray = {newline(1)} 
        dt.Rows.Add(newrow)
        dicItems.Add(newline(0), newline.Skip(1))
    End While
    DataGridView1.DataSource = dt
End Sub

Private Sub DataGridView1_OnCellValueChanged(ByVal s As Object, ByVal e As DataGridViewCellEventArgs)
    If e.ColumnIndex = 0 Then
        Dim changedCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
        For i As Integer = 0 To dt.Columns.Count
            dt.Rows(e.RowIndex).Item(i) = dicItems(changedCell.Value.ToString)(i) **error here**
        Next
    End If
End Sub

Private Sub DataGridView1_OnCurrentCellDirtyStateChanged(ByVal s As Object, ByVal e As EventArgs)
    If DataGridView1.IsCurrentCellDirty Then
        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub




  • 他在他的样品上使用了分号,但我坚持我的逗号。 :)

  • 错误说找不到第2列 ,错误变为给定的键不存在于同一行上的字典中

    The error says Cannot find column 2 or when I tweak something, the error becomes The given key was not present in the dictionary on the same line

    推荐答案

    您需要两个新的事件处理程序:

    You need two new Event Handlers:

    AddHandler DataGridView1.CellValueChanged, AddressOf DataGridView1_OnCellValueChanged
    AddHandler DataGridView1.CurrentCellDirtyStateChanged, AddressOf DataGridView1_OnCurrentCellDirtyStateChanged
    

    第一个是识别组合框的单元格值是否已更改。
    当你点击一个组合框项目(默认行为是事件只在你离开单元格后触发)触发 OnCellValueChanged
    将该代码放在Forms构造函数或加载事件处理程序中。

    The first is to recognize if the cell value of your combobox has changed. The latter is to fire the OnCellValueChanged when you click on a combobox item (default behavior is that the event is only fired after you leave the cell). Put that code in the Forms constructor or Load event handler.

    prodname 具有哪些细节(例如 prodname1 => prodcode1,amt1 )。在我的例子中,我用 Dictionary< Key:string =>值:IEnumerable< string>

    Additionally you have to store which prodname has which details (e.g. prodname1 => prodcode1,amt1). In my example I did this with a Dictionary<Key: string => Values: IEnumerable<string>.

    Class Form1
       Private dicItems As New Dictionary(Of String, IEnumerable(Of String))
       Private dt As New DataTable
       ...
       Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         While (Not sr.EndOfStream)
            newline = sr.ReadLine.Split(","c)
            Dim newrow As DataRow = dt.NewRow
            cmb.Items.Add(newline(0))
            dt.Rows.Add(newrow)
            'Fill Dicitonary here. Key: newLine(0), values: newLine(1,2,...n)
            dicItems.Add(newline(0), newline.Skip(1))
         End While
       End Sub
    

    事件处理程序的代码非常简单:

    The code of the event handlers are pretty straight forward:

      Private sub DataGridView1_OnCurrentCellDirtyStateChanged(s As Object, e As EventArgs)
            If DataGridView1.IsCurrentCellDirty Then
                 DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
            End If
         End sub
    

    =>激活 CellValueChangedEvent via通过提交当前组合框项更改。

    => Fire the CellValueChangedEvent via Commiting the current combobox item change.

    Private sub DataGridView1_OnCellValueChanged(s As Object, e As DataGridViewCellEventArgs)
        If e.ColumnIndex = 0 Then
            Dim changedCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
            For i As Integer = 0 To dt.Columns.Count 
                dt.Rows(e.RowIndex).Item(i) = dicItems(changedCell.Value.ToString)(i)
            Next        
        End If
    End sub
    

    =>检查组合框是否已更改( ColumnIndex = 0 )并获取更改的单元格。

    在字典中查找,找到相应的 prodcode,amt 与给定的键( prodname )。

    DataTable 中设置新值(必须使 dt 一个类字段,而不是你的Load方法中的一个变量)。

    => Check if the combobox has changed (ColumnIndex = 0) and get the changed cell.
    Lookup in the dictionary to find the corresponding prodcode,amt with the given key (prodname).
    Set the new value in the DataTable (you have to make dt a class field instead of a variable in your Load method).

    这篇关于从带有vb.net中csv的数据的组合框中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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