简体   繁体   中英

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:

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. I want to know how I can achieve the same on the 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.

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. 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.

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.

''' <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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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