none
Problems loading a datagrid at runtime onto a mobile

    Question

  • Hi,  I'm developing for a mobile 6 platform and trying to get the datagrid to work.  I want to load it manually in code, but the following does not work (assume gridTruckLoad is a valid datagrid on my form):

     

    Dim mo15Table As New DataTable("15LitreTable")

     

    Dim oCol As New DataColumn

    oCol.ColumnName =

    "Customer"

    oCol.DataType =

    GetType(String)

    mo15Table.Columns.Add(oCol)

     

    Dim moDataSet As DataSet = New DataSet

    moDataSet.Tables.Add(mo15Table)

    gridTruckLoad.DataSource = moDataSet

     

    Dim oDataTableStyle As DataGridTableStyle = New DataGridTableStyle

    oDataTableStyle.MappingName =

    "15LitreTable"

     

    Dim oTableCol As DataGridTextBoxColumn = New DataGridTextBoxColumn

    oTableCol.MappingName =

    "Customer"

    oTableCol.HeaderText =

    "Customer Location"

    oTableCol.Width = 200

    oDataTableStyle.GridColumnStyles.Add(oTableCol)

    gridTruckLoad.TableStyles.Add(oDataTableStyle)

     

    Dim oRow As DataRow = mo15Table.NewRow()

    oRow(

    "Customer") = "Customer 1"

    mo15Table.Rows.Add(oRow)

    mo15Table.AcceptChanges()

    gridTruckLoad.ColumnHeadersVisible =

    True

    gridTruckLoad.Refresh()

    =========================================

    When I run this - I get an empty grid - no header, no data.

    Any help much appreciated.

    Tuesday, September 22, 2009 5:14 AM

Answers

  • Hi MobileLPlater,




    We should bind datagrid with datatable variable, not dataset variable. So code should be modified as below:
    gridTruckLoad.DataSource = mo15Table

     Please check it. Here is my sample code to the same work, it works fine in my local PC:

    private void Form1_Load(object sender, EventArgs e)

            {

                //************table style 1 ************************************//

                DataGridTableStyle tabStyle = new DataGridTableStyle();

                tabStyle.MappingName = "Person";

     

                DataGridTextBoxColumn col = new DataGridTextBoxColumn();

                col.MappingName = "id";

                col.HeaderText = "UID";

                col.Width =100;

                tabStyle.GridColumnStyles.Add(col);

     

                DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();

                col1.MappingName = "name";

                col1.HeaderText = "UserName";

                col1.Width = 100;

                tabStyle.GridColumnStyles.Add(col1);

     

     

     

                this.dataGrid1.TableStyles.Clear();

                this.dataGrid1.TableStyles.Add(tabStyle);

     

     

                DataTable dt = new DataTable("Person");

                dt.Columns.Add("id");

                dt.Columns.Add("name");

     

                DataRow row = dt.NewRow();

                row["id"] = "1";

                row["name"] = "David";

     

                dt.Rows.Add(row);

                dt.AcceptChanges();

     

                DataSet ds = new DataSet();

                ds.Tables.Add(dt);

     

     

                this.dataGrid1.DataSource = dt;

            }







    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by SydneyWaterBoy Monday, September 28, 2009 12:21 AM
    Thursday, September 24, 2009 3:45 AM
    Moderator

All replies

  • Hi,

    The project Hide DataGrid Column Tester may help you as it uses a DataTable and Table Style and populates some data on a Data Grid :-

    http://www.smartmobiledevice.co.uk/Projects

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Tuesday, September 22, 2009 5:52 AM
  • Hi Paul,

    thanks for your lead. Unfortunately I had troubles opening the project - I unzipped it all to a single directory, but it told me files were missing.  I think it's also in C sharp, rather than VB.

    cheers

    Anton
    Wednesday, September 23, 2009 12:27 AM
  • Hi,

    If you can open up the Form1.cs file in a Text Editor or Visual Studio then you should be able to see the code. Even though it is written in C#, it may provide the pointer you require to get your VB code working.

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Wednesday, September 23, 2009 6:45 AM
  • Hi MobileLPlater,




    We should bind datagrid with datatable variable, not dataset variable. So code should be modified as below:
    gridTruckLoad.DataSource = mo15Table

     Please check it. Here is my sample code to the same work, it works fine in my local PC:

    private void Form1_Load(object sender, EventArgs e)

            {

                //************table style 1 ************************************//

                DataGridTableStyle tabStyle = new DataGridTableStyle();

                tabStyle.MappingName = "Person";

     

                DataGridTextBoxColumn col = new DataGridTextBoxColumn();

                col.MappingName = "id";

                col.HeaderText = "UID";

                col.Width =100;

                tabStyle.GridColumnStyles.Add(col);

     

                DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();

                col1.MappingName = "name";

                col1.HeaderText = "UserName";

                col1.Width = 100;

                tabStyle.GridColumnStyles.Add(col1);

     

     

     

                this.dataGrid1.TableStyles.Clear();

                this.dataGrid1.TableStyles.Add(tabStyle);

     

     

                DataTable dt = new DataTable("Person");

                dt.Columns.Add("id");

                dt.Columns.Add("name");

     

                DataRow row = dt.NewRow();

                row["id"] = "1";

                row["name"] = "David";

     

                dt.Rows.Add(row);

                dt.AcceptChanges();

     

                DataSet ds = new DataSet();

                ds.Tables.Add(dt);

     

     

                this.dataGrid1.DataSource = dt;

            }







    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by SydneyWaterBoy Monday, September 28, 2009 12:21 AM
    Thursday, September 24, 2009 3:45 AM
    Moderator
  • Hi Guang-Ming  - thanks for taking the time to answer my query.  You were right, a simple thing, but once I changed from a DataSet to DataTable, it all worked.  Many thanks.

    MobileLPlater
    Monday, September 28, 2009 12:21 AM