none
How display fields in a Linq and databinding scenario RRS feed

  • Question

  • I have a table with users, a table with roles and a link table UserRoles which connects these two.

     

    I have created a Form where I can add and change users. Because of the relation between user, userroles and roles I see my datasource Users and I see UserRoles in this datasource and Roles as a child of userroles. To mange user roles a drag and drop the UserRoles to my form and see a datagridview based on the table UserRoles.

     

    How do I display the Name of the Role in this datagridview. There's a DataPropertyName Roles but this does not display the properties of the role but a blank field.

     

    My question is: How do I display the Rolename in the datagridview?

     

    Do I have to change the datasouce of the datagridview in a linq statement like  dgv.DataSource = from ur IN UserRoles JOIN r as Roles ON r.guid = ur.RolGuid SELECT ur, RoleName = r.Name WHERE ur.UserGuid = UserBindingSource.Guid ?

     

    But how will that work if I add records (roles to UserRoles).

     

     

    Wednesday, July 23, 2008 3:02 PM

Answers

  • I've got it fixed all the way without comboboxes and such.

     

    Just in the Linq diagram you have room to place your own code (like    Diagram.dbml has a Diagram.vb file)

     

    If my table is named SQL and has one field SQLTypeGuid which comes from SQLTypes table, this code exposes the SQLType name:

     

     

    Code Snippet

    Partial Class SQL

     

     Public ReadOnly Property SQLTypeName() As String

      Get

        Return Me.SQLTypes.Name

      End Get

     End Property

    End Class

     

     

    Now if you have a datagrid bound to a bindingsource and the binding source is like    db.SQLs

    You can edit the datagrid add a column and DataPropertyName : SQLTypeName

     

    You can even make multiple values and nested values and so on!

     

    Cool!

     

     

    Wednesday, August 20, 2008 8:46 AM

All replies

  • Yes, you can bind a query to the DataSource and display the results that way.

     

    I'm not sure I understand the problem with adding records. Can you please explain?

     

    Thanks,

     

    --Samir

     

    Thursday, July 24, 2008 12:47 AM
  • This is a many-to-many relationship between users and roles.

     

    In a databinding scenario I have a User Detail form. How would I display the roles of this user in a datagridview?

     

    And after that, how can I add roles to this user in this binding scenario.

     

    The problem looks simple, but when doing it, it's not simple at all.

     

    Here's the most basic datamodel

     

    Table Users:

     

    UserId (guid)

    UserName

    UserPassword

     

    Table Roles:

     

    RolId

    RoleName

     

    Table UserRoles:

     

    UserId

    RolId

     

     

    I want to make a UserDetail form, with a datagridview on it with UserRoles (displaying the roles names) and a way to add roles to this user.

     

    I'm programming for years and it's more than simple to make, but I am not used to do this in a DataBinding scenario, and I got stuck.

     

    Friday, July 25, 2008 7:38 AM
  • The winform DataGridView does not support parent-child relationships. You can use two grids and have the second grid's DataMember set to the parent binding source's child property accessor.

     

    In your case, the top grid would be Users and the second grid would be for UserRoles. You would then have a DataGridViewComboBoxColumn set up for your role to display the descriptive name from the roles table and bind the SelectedValue to the RoleId in the UserRoles object.

     

    Jim Wooley

    www.ThinqLinq.com

    Friday, July 25, 2008 7:22 PM
    Moderator
  • Thx, This is sort of the solution I now have implemented. ComboBox was a bit of a drag when adding new rows (roles). It does the job in this scenario.

     

    However in some other parts a have persons and relations they work for. If I just use the same solution I only have the relation name (it's an organisation) to display (in the PersonRelations). There a 100.000 organisations and that will be to much for the combo. How do I display more than one Column of the organisation table in PersonRelations (some construction as Users and Roles). Under the relations I have some search functionality to select an organisation to be added...

     

    When writing this I just think I made things to tough. I just make a linq query to display rows and have this extra functionality to add new rows... hmm I have to test the idea.. I was thinking to hard.

     

    Thx for getting me "en route".

     

     

    Saturday, July 26, 2008 3:23 PM
  • I've got it fixed all the way without comboboxes and such.

     

    Just in the Linq diagram you have room to place your own code (like    Diagram.dbml has a Diagram.vb file)

     

    If my table is named SQL and has one field SQLTypeGuid which comes from SQLTypes table, this code exposes the SQLType name:

     

     

    Code Snippet

    Partial Class SQL

     

     Public ReadOnly Property SQLTypeName() As String

      Get

        Return Me.SQLTypes.Name

      End Get

     End Property

    End Class

     

     

    Now if you have a datagrid bound to a bindingsource and the binding source is like    db.SQLs

    You can edit the datagrid add a column and DataPropertyName : SQLTypeName

     

    You can even make multiple values and nested values and so on!

     

    Cool!

     

     

    Wednesday, August 20, 2008 8:46 AM