lockgar
lockgar

Reputation: 79

Getting a specific value in a Datagrid in MSDN visual basic

So I have a simple question, but after so many Google searches I cant seem to find a good example.

I'm trying to get a very specific cell from a datagrid. I Know the datagrid is working, as it runs fine and I can see all of the values I want to see from it in my program. What I want to do is get a value from a specific cell the datagrid. I was hoping it would be something simple like this.

dataGrid1.DataSource = someDataSet

dataGrid.DataBind()

Label1.Text = dataGrid1.CellValue(rowValue, columnValue).ToString() 'this isn't valid

What I keep seeing is something with e.userclickshere or something else, which will be useful later on, but right now I just want to get a specific cell from a dg.

Upvotes: 0

Views: 648

Answers (2)

LosManos
LosManos

Reputation: 7692

FWIW: One way is to get it directly from your someDataSet. This way you won't mix data and UI.

Regarding how to get data out of the dataset - play with intellisense.
It is something like

Dim myValue as Object = myDataset.Tables(myTableIndexOftenZero).Rows(myRowIndex)(myFieldIndex)

But don't use untyped datasets unless you have to. Typed datasets gives you names and types for free. It is something like

Dim myValue as Integer = myDataset.MyTable(myRowIndex).MyProperty

BTW: IIRC you can feed your grid with a table like

myTable.DataSource = myDataSet.Tables(0)

BTW2: If you inherit from Dataset and implement your own stuff you can get to some nice protected events and properties to react to changes in the dataset/datatable. But I would today instead go for BTW3 as below.

BTW3: Today you might want to get rid of datasets at all and instead use lists of classes. Earlier there was some friction around this but today...

Upvotes: 1

Esselans
Esselans

Reputation: 1546

Something like

Dim dt as datatable = someDataSet.Tables(0) 'Or whatever table is

Label1.text = dt(rowValue)(columnValue).toString()

Upvotes: 1

Related Questions