Winforms Datagrid Tablestyle

Posts   
 
    
carmines
User
Posts: 15
Joined: 25-Oct-2006
# Posted on: 25-Oct-2006 00:26:34   

I am really struggling to get the winforms standard datagrid to display only certain columns from my collection. I have looked at all the related threads in the forums and tried all suggestions but my always shows all columns.

I am using selfservicing I set the tablestyle mapping name to the name of the collection class

Below is a code snippet showing my latest efforts Any help greatly appreciated

        ' get all customers
        _allEnquirySources = New Pt15EnquirySourceCollection
        _allEnquirySources.GetMulti(Nothing)

        Dim widgetTable As New DataGridTableStyle
        widgetTable.MappingName = "Pt15EnquirySourceCollection"

        ' Creates three column styles.
        Dim modelColumn As New DataGridTextBoxColumn
        modelColumn.MappingName = "Pt15Description"
        modelColumn.HeaderText = "Description"

        Dim closedColumn As New DataGridBoolColumn
        closedColumn.MappingName = "Pt15Closed"
        closedColumn.HeaderText = "Closed"

        ' Adds the column styles to the grid table style.
        widgetTable.GridColumnStyles.Add(modelColumn)
        widgetTable.GridColumnStyles.Add(closedColumn)

        ' Add the table style to the collection, but clear the 
        ' collection first.
        DataGrid1.TableStyles.Clear()
        DataGrid1.TableStyles.Add(widgetTable)
        DataGrid1.DataSource = _allEnquirySources
bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 25-Oct-2006 02:51:14   

Be sure to set the grid's AutoGenerateColumns property to false.

carmines
User
Posts: 15
Joined: 25-Oct-2006
# Posted on: 25-Oct-2006 10:05:31   

bclubb wrote:

Be sure to set the grid's AutoGenerateColumns property to false.

My datagrid does not have an AutoGenerateColumns property - I should have stated that I was using visual studio.net 2003 - is the AutoGenerateColumns property something new in 2005?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Oct-2006 17:29:11   

Did you try to open the form in Design Mode and define the needed columns in the DataGridView properties -> TableStyles -> Columns.

carmines
User
Posts: 15
Joined: 25-Oct-2006
# Posted on: 26-Oct-2006 08:20:43   

Walaa wrote:

Did you try to open the form in Design Mode and define the needed columns in the DataGridView properties -> TableStyles -> Columns.

I am not using a DataGridView control - I am using visual studio 2003 so I only have access to the Datagrid control. I have tried setting up the columns at design time and the correct column names do appear in the column mappingname combobox. However when I run the program the grid still shows all columns from the collection. The problem seems to be that the grid is not using the table style that I am creating. Any more ideas?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 26-Oct-2006 09:51:53   

It can be you're setting the tablestyle too late in the program. I.e.: if you set it in the constructor of the form and bind the data in the form load, it should work.

Frans Bouma | Lead developer LLBLGen Pro
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 26-Oct-2006 09:56:56   

That's strange,

Try to run the app in Debug mode and before the Form renders check to see if the TableStyles collection of the DataGrid contains any items. (count > 0)

(edit) Better as Frans suggested ...check the TableStyles collection before binding the data.

carmines
User
Posts: 15
Joined: 25-Oct-2006
# Posted on: 26-Oct-2006 16:56:49   

Otis wrote:

It can be you're setting the tablestyle too late in the program. I.e.: if you set it in the constructor of the form and bind the data in the form load, it should work.

The code snippet I included in my original post was all from the form constructor. So I am creating the tablestyle and the datasource in the constructor. Should I leave the datasource until the form load?

carmines
User
Posts: 15
Joined: 25-Oct-2006
# Posted on: 26-Oct-2006 19:38:55   

Otis wrote:

It can be you're setting the tablestyle too late in the program. I.e.: if you set it in the constructor of the form and bind the data in the form load, it should work.

Thanks for the suggestion but this has not made any difference - I have the tablestyle being set up in the constructor and the datasource being set up in the form load. I have also checked the tablestyles collection in debug mode and it is there.

Has anyone else actually succeeded in doing this? Just to clarify: I am using visual studio.net 2003 (net 1.1) My application is winforms using the standard datagrid I am using self servicing template

At design time if I drag a customer collection onto my form and try to set up a tablestyle the drop down list of mapping names only contains 'ts' which is the timestamp column from my table (I guess this shows up because a timestamp is interpreted as a collection of bits).

Any feedback is appreciated

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 27-Oct-2006 10:36:27   

Yes, it is doable and working, though we know there are problems with this, not due to llblgen pro code, but because table styles are added too late to the grid to be considered (I've no idea why this is).

For example, we've had reports where tablestyles created and added in code didn't work, and tablestyles designed in the vs.net designer did work (and which looked exactly the same in code)...

I'll see if I can reproduce it using the steps you provided.

Frans Bouma | Lead developer LLBLGen Pro
carmines
User
Posts: 15
Joined: 25-Oct-2006
# Posted on: 27-Oct-2006 14:16:19   

Otis wrote:

Yes, it is doable and working, though we know there are problems with this, not due to llblgen pro code, but because table styles are added too late to the grid to be considered (I've no idea why this is).

For example, we've had reports where tablestyles created and added in code didn't work, and tablestyles designed in the vs.net designer did work (and which looked exactly the same in code)...

I'll see if I can reproduce it using the steps you provided.

Thanks very much for your efforts - I eagerly await the results of your test. I have replicated my problem on the northwind example project by adding a tablestyle to the customer selection grid - I could email this to you if that would help. Thanks again

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 27-Oct-2006 14:35:13   

I've a project for these kind of things, it's not necessary to mail us this.

At the moment a complex issue with relations in the runtime is being fixed, so you're next, but still scheduled for this afternoon simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 27-Oct-2006 17:57:28   

As you didn't specify which version of llblgen pro you're using, I assumed v1.0.2005.1.

My testcode/form:


// inside the form class with the grid.
private System.Windows.Forms.DataGrid _theGrid;
private System.ComponentModel.Container components = null;

private DataGridTextBoxColumn column;
private DataGridTableStyle style;

public BindingTester()
{
    InitializeComponent();

    column = new DataGridTextBoxColumn();
    column.MappingName = "CompanyName";
    column.HeaderText = "CompanyName";
    style = new DataGridTableStyle();
    style.MappingName = "CustomerCollection";
    style.GridColumnStyles.Add(column);
    _theGrid.TableStyles.Add(style);
}


public void BindCollection(IEntityCollection toBind)
{
    _theGrid.DataSource = toBind;
}

my testcode:


[Test]
public void TableStyleTest()
{
    CustomerCollection customers = new CustomerCollection();
    customers.GetMulti(null);
    BindingTester viewer = new BindingTester();
    viewer.BindCollection(customers);
    viewer.ShowDialog(null);
}

Be absolutely sure the casing of the typename you specify as mappingname is correct. If you don't do that, no binding takes place.

Frans Bouma | Lead developer LLBLGen Pro
carmines
User
Posts: 15
Joined: 25-Oct-2006
# Posted on: 27-Oct-2006 19:10:27   

I am very sorry for not posting the version of llblgen I am using. I know this must be important information and I even read the forum guidelines that ask for this to be included. My sincere apologies for this. I am actually using the demo version of llblgen 2.0 The code sample you have posted does not really look significantly different to my code (apart from mine being in VB and yours is in C#). Could it be a problem with the demo version 2.0?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 27-Oct-2006 20:18:48   

No, I don't think it is, though the code significant for binding is very different in llblgen pro v2.0, so I'll do a new test in v2.0 code. My code illustrates what you described: setting up the tablestyle in the constructor of the form and then binding the collection to teh grid. So it should look similar.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 30-Oct-2006 08:56:49   

I can reproduce it with the exact same unittest I wrote above in .net 1.1 code using llblgen pro v2.0.

Strange. I'll check it out simple_smile

(edit). The answer is inside the 'Migrating your code' section.

The list name of an entitycollection bound to a grid has been changed to the following: the LLBLGenProEntityName of an instance created by the factory set for the collection + "Collection". So if the factory is set to CustomerEntityFactory, the name is "CustomerEntityCollection". In adapter, this is a change, as in adapter previously the list name was always "". This now allows you to define gridlayouts and bind them to various sets of data at runtime.

This isn't very clear for new users as new users don't read that section obviously. I'll add a note to the Databinding section in the manual.

Frans Bouma | Lead developer LLBLGen Pro
carmines
User
Posts: 15
Joined: 25-Oct-2006
# Posted on: 30-Oct-2006 19:25:13   

Fantastic simple_smile That has absolutely worked. You are right about the 'migrating your code' section - I haven't read it as version 2 was my first experience of llblgen. I am coming to the end of my 30 day evaluation period but with support like this I will definitely be making a purchase. Thanks again for your support over the last few days and thanks for developing such a time saving application.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 31-Oct-2006 15:19:56   

carmines wrote:

Fantastic simple_smile That has absolutely worked. You are right about the 'migrating your code' section - I haven't read it as version 2 was my first experience of llblgen. I am coming to the end of my 30 day evaluation period but with support like this I will definitely be making a purchase. Thanks again for your support over the last few days and thanks for developing such a time saving application.

You're welcome simple_smile And thanks for the compliments simple_smile

Frans Bouma | Lead developer LLBLGen Pro