How to programmatically set cell value in DataGridView?

前端 未结 14 842
無奈伤痛
無奈伤痛 2020-11-30 02:05

I have a DataGridView. Some of the cells receive their data from a serial port: I want to shove the data into the cell, and have it update the underlying bound object.

相关标签:
14条回答
  • 2020-11-30 03:01

    If the DataGridView has been populated by DataSource = x (i.e. is databound) then you need to change the bound data, not the DataGridView cells themselves.

    One way of getting to that data from a known row or column is thus:

    (YourRow.DataBoundItem as DataRowView).Row['YourColumn'] = NewValue;
    
    0 讨论(0)
  • 2020-11-30 03:05

    in VB you can use this one

    Dim selectedRow As DataRowView
    selectedRow = dg.Rows(dg.CurrentCell.RowIndex).DataBoundItem
    selectedRow("MyProp") = "myValue"
    dg.NotifyCurrentCellDirty(True)
    

    thanks to saeed serpooshan for last row

    0 讨论(0)
提交回复
热议问题