简体   繁体   中英

How do I get a value from a specific column in a vb.net datagridview?

I have created a datatable as the source for my datagridview as follows:

    Dim dt As New DataTable
    dt.Clear()
    DataGridView1.DataSource = dt
    dt.Columns.Add("Customer")
    dt.Columns.Add("Dtime")
    dt.Columns.Add("Title")
    dt.Columns.Add("EventID")

The EventID is numeric and will be passed to a new form to tell it which record to open (that bit works I use it on another form).

I have tried:

event = DataGridView1.SelectedRows(e.RowIndex).Cells(3).Value.ToString
event = Convert.ToInt32(DataGridView1.SelectedRows(0).Cells(3).Value.ToString)
event = Convert.ToInt32(DataGridView1.Item("EventID", DataGridView1.CurrentRow.Index).Value.ToString)

and probably a dozen other ways I've found on this site and others but I keep getting the same error:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

It's driving me insane, what am I doing wrong? Please help!

event = DataGridView1.Rows(e.RowIndex).Cells(3).Value.ToString()

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