【问题标题】:How to return value from datagridview如何从datagridview返回值
【发布时间】:2012-08-25 16:17:59
【问题描述】:

我想从具有所选行的 datagridview 的表中返回一个值

TextBox8.Text = Me.DataGridView1.CurrentRow.Cells("Vend Price").ToString

vend price 代表需要的列名。

我收到错误消息:

Object reference not set to an instance of an object.

现在我做错了什么?

附言是否可以从数据网格的已删除列中返回一个值,或者我应该直接从表中返回?


更完整的代码如下:

Private Sub MainMenu_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load    
    Me.ProductsTableAdapter.Fill(Me.MisuDBDataSet.Products) 
End Sub 

Private Sub Button15_Click(sender As System.Object, e As System.EventArgs) Handles Button15.Click   
    If Me.DataGridView1.CurrentRow.Cells("Vend Price").Value.ToString Is Nothing Then 
        'Do nothing 
    Else 
        Label13.Text = Me.DataGridView1.CurrentRow.Cells("Vend Price").Value.ToString 
    End If 
End Sub

【问题讨论】:

  • 请提供您的 html 标记。
  • @Nudier datagridview 是一个winforms控件,不是asp.net

标签: datagridview vb.net-2010


【解决方案1】:

要从 DataGridView 单元格中获取值,您需要引用该单元格的 Value 属性:

TextBox8.Text = Me.DataGridView1.CurrentRow.Cells("Vend Price").Value.ToString

【讨论】:

  • 我已经给出了获取值的语法,虽然再看一遍,我怀疑你的代码会导致对象引用错误。当你说你“从数据网格中删除了一个列”时,你是什么意思?
  • 关于数据网格中的列,我创建了一个数据网格,它只显示 db 表中的几个列,我想知道网格实际上是否包含所有列信息或只有显示的列信息。
  • @MisuEgri “只显示”是什么意思?你如何阻止它显示列?但是,引用不正确的列名无论如何都会导致不同的错误。
  • @MisuEgri - 当前行是否为空?您需要将那条线分开并找出导致对象引用未设置错误的原因。
  • 嗨大卫,网格从 productsbindingsource 和 datagridview 任务中获取数据,我编辑列并删除了一些列,因为我不需要它们全部显示在这个数据网格中。
【解决方案2】:

要从 DataGridView 单元格中获取值,您需要引用该单元格的 Value 属性:

Dim ColumnIndex As Integer = 0 Dim RowIndex As Integer = 0
ColumnIndex = DataGridView.CurrentRow.Cells("DataGridViewTextBoxColumn3").ColumnIndex
RowIndex = DataGridView.CurrentRow.Cells("DataGridViewTextBoxColumn3").RowIndex
TextBox8.Text = DataGridView(ColumnIndex, RowIndex).Value.ToString()

'我的列名是DataGridViewTextBoxColumn3希望这有助于名称属性应该在代码中的某个地方设置检查表单属性的DataGridView名称并使用它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-18
    • 2015-02-08
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多