LostRob
LostRob

Reputation: 463

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!

Upvotes: 0

Views: 1598

Answers (1)

senthilkumar2185
senthilkumar2185

Reputation: 2578

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

Upvotes: 1

Related Questions