dougconran
Technical User
I need to be able to take data from a file and display it in a datagrid. However, like others before me, I'm having problems with resizing some columns.
Although, using no tablestyle, I can get the data to display the columns are too narrow and so the data wraps onto a second line and becomes invisible.
I've looked through earlier postings and know that the answer is to set up a tablestyle. Unfortunately, when I do that I get a completely empty datagrid.
I'm sure that it is something to do with Mapping Names but can't establish the link between dataset/datatable and datagrid.
I'd be grateful for any help. My code is below
TIA
Doug
Although, using no tablestyle, I can get the data to display the columns are too narrow and so the data wraps onto a second line and becomes invisible.
I've looked through earlier postings and know that the answer is to set up a tablestyle. Unfortunately, when I do that I get a completely empty datagrid.
I'm sure that it is something to do with Mapping Names but can't establish the link between dataset/datatable and datagrid.
I'd be grateful for any help. My code is below
TIA
Doug
Code:
Dim darray(3)
Dim tarray(50)
Dim ds As DataSet
Dim dt As DataTable
ds = New DataSet
dt = New DataTable("Employees")
ds.Tables.Add(dt)
dt.Columns.Add("First Name")
dt.Columns.Add("Last Name")
dt.Columns.Add("Age")
dt.Columns.Add("Dept")
If File.Exists(employees) Then
fs = New FileStream(employees, FileMode.Open, FileAccess.Read)
sr = New StreamReader(fs, True)
vstr = sr.ReadLine
While Len(vstr) > 0
tarray = Split(vstr, vbTab)
darray(0) = tarray(0)
darray(1) = tarray(5)
darray(2) = tarray(7)
darray(3) = tarray(8)
dt.Rows.Add(darray)
vstr = sr.ReadLine
End While
fs.Close()
darray(0) = "Col1"
darray(1) = "Col2"
darray(2) = "Col3"
darray(3) = "Col4"
dt.Rows.Add(darray)
End If
DataGrid1.DataSource = dt
Dim tableStyle As DataGridTableStyle
tableStyle = New DataGridTableStyle
tableStyle.MappingName = "Employees"
Dim aColumnTextColumn As DataGridTextBoxColumn
aColumnTextColumn = New DataGridTextBoxColumn
aColumnTextColumn.Width = 10
aColumnTextColumn.HeaderText = "First Name"
tableStyle.GridColumnStyles.Add(aColumnTextColumn)
aColumnTextColumn.Width = 10
aColumnTextColumn.HeaderText = "Last Name"
tableStyle.GridColumnStyles.Add(aColumnTextColumn)
aColumnTextColumn.Width = 10
aColumnTextColumn.HeaderText = "Age"
tableStyle.GridColumnStyles.Add(aColumnTextColumn)
aColumnTextColumn.Width = 100
aColumnTextColumn.HeaderText = "Dept"
tableStyle.GridColumnStyles.Add(aColumnTextColumn)
DataGrid1.TableStyles.Add(tableStyle)