从组合框中选择来自vb.net中csv的数据

时间:2016-07-18 06:12:13

标签: vb.net csv datagridview

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

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

prodname是我在datagridview中的组合框中显示的内容。

我想要的是当我选择prodname1时,prodcode1amt1将显示在数据网格视图的同一行中。我刚刚开始在vb.net中摸索,如果有人帮助我,我将不胜感激。

这就是我现在所拥有的:

    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

更新:

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

    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
  • 他在他的样本上使用了分号,但我坚持使用逗号。 :)

错误显示Cannot find column 2或当我调整某些内容时,错误在同一行显示为The given key was not present in the dictionary

Here is my csv file done in Excel

1 个答案:

答案 0 :(得分:0)

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

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

首先要识别组合框的细胞值是否已经改变。 后者是在单击组合框项目时触发OnCellValueChanged(默认行为是事件仅在您离开单元格后触发)。 将该代码放在Forms构造函数或Load事件处理程序中。

此外,您必须存储哪个prodname具有哪些详细信息(例如prodname1 =&gt; prodcode1,amt1)。在我的示例中,我使用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

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

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

=&GT;通过提交当前组合框项目更改来触发CellValueChangedEvent

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

=&GT;检查组合框是否已更改(ColumnIndex = 0)并获取更改的单元格 在字典中查找以找到具有给定键(prodcode,amt)的相应prodnameDataTable中设置新值(您必须使dt成为类字段而不是Load方法中的变量。)