DataGrid Column By Text

  • Thread starter Thread starter Mr.D
  • Start date Start date
M

Mr.D

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 ;)
 
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).GridColumnStyles
_(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.
 
Charlie said:
Code:
[/QUOTE]

I get my data from the Internet, So I dont have any control of the data
layout.

<code>
Dim dsPubs As New Data.DataSet
Dim xdcDOC As New Xml.XmlDocument

dsPubs.ReadXml(http://www......)
DataGrid1.DataMember = "Name"
DataGrid1.DataSource = dsPubs
</code>

So how do I get the columnIndex of the column with the text "Adress"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top