Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

resizing datagrid columns (again!) 1

Status
Not open for further replies.

dougconran

Technical User
Sep 26, 2003
89
GB
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

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)
 
I can help you with that Doug.
Looks like you never added a MappingName. The MappingName is the name of the field in the dataset or table object that you are bound to.

tableStyle.MappingName = "Employees" <-- no good, this is supposed to be the field name.

Dim aColumnTextColumn As DataGridTextBoxColumn
aColumnTextColumn = New DataGridTextBoxColumn
aColumnTextColumn.Width = 10
aColumnTextColumn.HeaderText = "First Name"
aColumnTextColumn.MappingName = first_name <- field name from recordset.
tableStyle.GridColumnStyles.Add(aColumnTextColumn)
aColumnTextColumn.Width = 10
aColumnTextColumn.HeaderText = "Last Name"
aColumnTextColumn.MappingName = last_name <- field name from recordset.
tableStyle.GridColumnStyles.Add(aColumnTextColumn)
aColumnTextColumn.Width = 10

Hope this helps.
If it's not clear, please let me know.
 
Here is my code from a test datagrid.

Code:
' Setup connection
Dim myConnection As New SqlConnection(strMyConnectString)

strSQL = "SELECT agy_id,agy_type,agy_addr,agy_City,agy_last_dispatched FROM agency"

Dim myDataAdapter As New SqlDataAdapter(strSQL, myConnection)
Dim myDataSet As New DataSet

myDataAdapter.Fill(myDataSet, "agency")
'Dim myDataView As DataView = New DataView(myDataSet.Tables("agency"))
myDataView = New DataView(myDataSet.Tables("agency"))

myDataView.AllowNew = False

Dim myTableStyle As New DataGridTableStyle
myTableStyle.MappingName = "agency"

Dim myDataCol As DataGridTextBoxColumn

myDataCol = New DataGridTextBoxColumn
myDataCol.HeaderText = "Agency ID"
myDataCol.MappingName = "agy_id"
myDataCol.Width = 70
myTableStyle.GridColumnStyles.Add(myDataCol)

myDataCol = New DataGridTextBoxColumn
myDataCol.HeaderText = "Agency Type"
myDataCol.MappingName = "agy_type"
myDataCol.Width = 70
myTableStyle.GridColumnStyles.Add(myDataCol)

myDataCol = New DataGridTextBoxColumn
myDataCol.HeaderText = "Agency Address"
myDataCol.MappingName = "agy_addr"
myDataCol.Width = 100
myTableStyle.GridColumnStyles.Add(myDataCol)

myDataCol = New DataGridTextBoxColumn
myDataCol.HeaderText = "Agency City"
myDataCol.MappingName = "agy_City"
myDataCol.Width = 100
myTableStyle.GridColumnStyles.Add(myDataCol)

myDataCol = New DataGridTextBoxColumn
myDataCol.HeaderText = "Agy Last Dispatched"
myDataCol.MappingName = "agy_last_dispatched"
myDataCol.Width = 100
myTableStyle.GridColumnStyles.Add(myDataCol)

'Default text if the column data is null
myDataCol.NullText = "Null text"
myDataGrid.TableStyles.Add(myTableStyle)

myDataGrid.DataSource = myDataView

Or if you don't want to code them all by hand you could loop thru the fields in the dataset and get them link this:

Short piece of code to give you the idea if how to do it.
Code:
Dim aColumnTextColumn As DataGridTextBoxColumn
Dim i As Integer
i = 0

Do While (i < numCols)
aColumnTextColumn = New DataGridTextBoxColumn
'Get the column info from the dataset					    aColumnTextColumn.HeaderText = _dataSet.Tables("customers").Columns(i).ColumnName
					aColumnTextColumn.MappingName = _dataSet.Tables("customers").Columns(i).ColumnName
					tableStyle.GridColumnStyles.Add(aColumnTextColumn)
Loop

Hope this helps more.
 
Thanks bigfoot - have a star.

I now understand the function of the Mappingname (or, at least, how it should work) and it also solves another problem for me in that I can import my whole file into a table and then just display certain columns from the table. Before I was having to trawl through the file selecting just the required fields.

Many thanks

Doug
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top