formatting a datagrid

Ok - I have a datagrid bound to a SqlCeDataAdapter

grid.DataSource = myDataSet.Tables(0)


I want to apply a dataGridTableStyle to format the grid nicely

ts.MappingName = "procedureHistory"

Dim procedureDate As New DataGridTextBoxColumn
procedureDate.MappingName = "procedureDate"
procedureDate.HeaderText = "Date"
ts.GridColumnStyles.Add(procedureDate)

etc etc for the other colums

grid.TableStyles.Add(ts)

But it doesn't work - the grid still has the default formatting -  I guess it's cause the datasource.tables property just references an index, and this doesn't correspond to the ts.mappingName perhaps

To go back a step, what I want to do is return a record set via stored proc from a SQL database, bind the grid to it and then format the grid nicely !

I'm using VB.net 2005 beta 2.

thanks



Answer this question

formatting a datagrid

  • Marisa Seal

    Thanks for the timely reply.

    My code actually in that order & it isn't happening - the only thing which I am wondering about is the mapping name for the table style

    My query / 'table' is the result of a join on a number of tables. So I'm unsure as to what the relevance of the table name is in this instance & how it is linked to the returned data....

    selectStatement = 'Select blah blah blah...'

    Dim
    cmd As New SqlCeCommand(selectStatement, conn)
    Dim myAdapter As New
    SqlCeDataAdapter
    myAdapter.SelectCommand = cmd
    Dim myDataSet As New
    System.Data.DataSet
    myAdapter.Fill(myDataSet)
    grid.DataSource = myDataSet.Tables(0)

    If there is a better way / more efficient way of doing this I'd love to know.

    thanks
    Bruce


  • Christoff

    Fantastic - what do you know - it's called 'Table'!

    Thanks very much for your help - would never have found that one.

    Bruce

  • daveeberly

    The MappingName for your DataGridTableStyle must be equal to the name of the DataTable which is the DataSource of your DataGrid.
    The FAQ here http://www.syncfusion.com/faq/windowsforms/Search/931.aspx
    gives some code snippet you can use to determine the mapping name required by your datasource.

    HTH


  • Sheeja Anil

    Hi Bruce,
    You will need to flip the order of the steps you are taking to get your desired style applied to the grid.
    1. Create a DataGridTableStyle and set its MappingName property to point to the name of your DataTable which is the DataSource for the DataGrid.
    2. Next, add this DataGridTableStyle to the DataGrid.TableStyles property.
    3. Finally, set the DataGrid.DataSource property to the DataTable

    NOTE: You need to add the DataGridTableStyle to the DataGrid before you set the DataGrid.DataSource property.

    Doing things in this order guarantees that the DataGridTableStyle.GridColumnStyles collection will be fully populated showing all the DataTable columns in the the DataGrid.

    For your code above, do the following

    ts.MappingName = "procedureHistory"

    Dim procedureDate As New
    DataGridTextBoxColumn
    procedureDate.MappingName = "procedureDate"
    procedureDate.HeaderText = "Date"
    ts.GridColumnStyles.Add(procedureDate)

    etc etc for the other colums

    grid.TableStyles.Add(ts)

    'Set the DataSource after you have added the Grid Table Style
    grid.DataSource = myDataSet.Tables(0)



    HTH,
    Mark.


  • formatting a datagrid