Datagrids in .net seem kind of convoluted at first.
Here is some code that shows how the grid is built from the ground up, as a
set of objects:
Put a datagrid on a form.
This goes in the forms load event:
Dim DT As New DataTable()
DT.Columns.Add(New DataColumn("Fieldname1"))
DT.Columns.Add(New DataColumn("Fieldname2"))
Dim DR As DataRow = DT.NewRow
DR.Item(0) = "John"
DR.Item(1) = "Doe"
DT.Rows.Add(DR)
Dim ts As New DataGridTableStyle()
Dim dgc(1) As DataGridColumnStyle
dgc(0) = New DataGridTextBoxColumn()
dgc(0).HeaderText = "Col 1"
dgc(0).MappingName = "Fieldname1"
dgc(1) = New DataGridTextBoxColumn()
dgc(1).HeaderText = "Col 2"
dgc(1).MappingName = "Fieldname2"
ts.GridColumnStyles.AddRange(dgc)
Me.DataGrid1.TableStyles.Add(ts)
Me.DataGrid1.DataSource = DT
Then add a Datagrid CurrentCellChanged event handler:
Private Sub DataGrid1_CellChanged(ByVal sender As System.Object, _
ByVal e As EventArgs) Handles DataGrid1.CurrentCellChanged
MessageBox.Show(DataGrid1.TableStyles(0).GridColum nStyles
_(DataGrid1.CurrentCell.ColumnNumber).HeaderText)
End Sub
Note:
Dim dgc(1) As DataGridColumnStyle--
This declaration is made of the base class, which is abstract. This allows
you to instantiate each column as a text column, or a boolean column, or as
another style that you derive from the base class.
"Mr.D" wrote:
I know that it is possible to do this: MsgBox(DataGrid1.Item(rowIndex,
columnIndex))
However my data has several columns wich I dont controll.
Ex: Name, Adress, Favorite Food in different orders.
So how do I select column by Text instead of number?
EX: DataGrid1.Item(rowIndex, DataGrid1.Column("Adress")
Hope that I made myself understandable ;)
----
Tim