简体   繁体   English

如何为DataGridView的单元格分配值?

[英]How to assign a value to a cell of DataGridView?

I'm trying to set a second value to the cell of DataGridView , in particular as happean in the ComboBox like the DataBinding, for example: 我正在尝试设置第二个值的单元格DataGridView的,特别是作为happean ComboBox类的数据绑定,例如:

myComboBox.DisplayMember = "NAME"
myComboBox.ValueMember = "id_value"

how you can see I have the name displayed, but when I do myComboBox.SelectedValue I get id of value selected. 您如何看到我显示了名称,但是当我执行myComboBox.SelectedValue我获得了所选值的ID。 I want to know how I can achieve the same on the DataGridView. 我想知道如何在DataGridView上实现相同的目标。 Actually I'm able only to add a value like this: 实际上,我只能添加这样的值:

 myDataGrid.Rows.Add({"TEST"})

how I can assign to the row added a value as in the above example? 如上例所示,如何为行添加值?

One possibility is to load your data into a DataTable, add a BindingSource, set the data source for the BindingSource to the DataTable then use the BindingSource as the data source for the DataGridView. 一种可能是将数据加载到DataTable中,添加BindingSource,将BindingSource的数据源设置为DataTable,然后将BindingSource用作DataGridView的数据源。

From here we can not show a one or more columns where the hidden columns would be used as one might as with a ComboBox DisplayMember and ValueMember. 从这里开始,我们无法显示一个或多个列,其中隐藏列将像ComboBox DisplayMember和ValueMember一样被使用。 The example below is a simple demo to allow you to try this. 下面的示例是一个简单的演示,您可以尝试一下。 And note, there is little overhead moving from loading a DataGridView row by row to the method suggested below. 请注意,从逐行加载DataGridView到下面建议的方法几乎没有开销。

I intentionally kept things simple and all code as is relies on Framework 3.5 or higher, if using an earlier version than line continuation characters will be needed or move lines broken up to one line. 我故意使事情简单化,如果需要使用比行延续字符早的版本或将行拆分成一行,则所有代码都依赖于Framework 3.5或更高版本。

''' <summary>
''' Requires 1 DataGridView, 1 TextBox, 2 Buttons
''' </summary>
''' <remarks></remarks>
Public Class Form1
    Private bs As New BindingSource
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bs.DataSource = MimicLoadingData()
        DataGridView1.DataSource = bs
    End Sub
    ''' <summary>
    ''' Mimic loading data from a data source be it
    ''' a text file, excel or database etc.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' Set ColumnMapping to Hidden so they are 
    ''' not shown in the DataGridView
    ''' </remarks>
    Private Function MimicLoadingData() As DataTable
        Dim dt As New DataTable

        dt.Columns.Add(New DataColumn With
            {
                .ColumnName = "ID",
                .AutoIncrement = True,
                .DataType = GetType(Integer),
                .ColumnMapping = MappingType.Hidden
            }
        )
        dt.Columns.Add(New DataColumn With
            {
                .ColumnName = "Name",
                .DataType = GetType(String)
            }
        )
        dt.Columns.Add(New DataColumn With
            {
                .ColumnName = "IdName",
                .DataType = GetType(Integer),
                .ColumnMapping = MappingType.Hidden
            }
        )

        dt.Rows.Add(New Object() {Nothing, "Karen", 34})
        dt.Rows.Add(New Object() {Nothing, "Bob", 100})
        dt.Rows.Add(New Object() {Nothing, "Anne", 50})
        Return dt
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If bs.Current IsNot Nothing Then
            MessageBox.Show(
                CType(bs.Current, DataRowView).Row.Field(Of Integer)("IdName").ToString)
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If bs.Current IsNot Nothing Then
            Dim value As Integer = 0
            If Integer.TryParse(TextBox1.Text, value) Then
                CType(bs.Current, DataRowView).Row.SetField(Of Integer)("IdName", value)
            End If
        End If
    End Sub
End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM