master detail, datagrid mappingname question

master detail, datagrid mappingname question

Post by rob warnin » Fri, 03 May 2002 19:26:32



Hy

In a master detail form I like to show the details in a datagrid. When I set
the datagrid.dataSource to the dataset and the datagrid.dataMember to the
table, a default datagrid table style is aperenty made and shows ok! Also
showing the table key column and the referential column witch is not what I
want.
So I try to make my own table style; but what ever mapping name I use for my
own datagridtablestyle the default is still shown in my grid. also when I
set the datagrid.datasource to the dataset-master table and the datagrid
dataMember to the refering detail table it still dus not work.

How this should be done???

many thanks in advance.

Rob Warning

 
 
 

master detail, datagrid mappingname question

Post by Ken Robison[M » Fri, 03 May 2002 22:57:37


Hi Rob,

You will need to work with the DataGridTableStyle and GridColumnStyle
collection in order to accomplish you goal.
When you set the Datasource and Datamember of the datagrid you will see all
the columns.
You can sample set the columnwidth = 0 for the columns in question or you
can add the desired formatting via the DataGridTableStyle collection and
GridColumnStyle collection setting the header and binding as desired.

EXAMPLE:
Private Sub ButtonAddDataGridTableStyle_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ButtonAddDataGridTableStyle.Click
        If Not MyDataGrid.TableStyles.Contains("items") Then
            AddCustomDataTableStyle()
        Else
            MyDataGrid.TableStyles.Clear()
        End If

    End Sub
    Private Sub AddCustomDataTableStyle()
        ' Create a new DataGridTableStyle and set
        ' its MappingName to the TableName of a DataTable.
        Dim ts1 As New DataGridTableStyle()
        ts1.MappingName = "Items"
        ts1.AlternatingBackColor = Color.LightBlue
        ' Add a GridColumnStyle and set its MappingName
        ' to the name of a DataColumn in the DataTable.
        ' Set the HeaderText and Width properties.
        ' Add a second column style.
        Dim TextCol As New DataGridTextBoxColumn()
        TextCol.MappingName = "id"
        TextCol.HeaderText = "Identity"
        TextCol.TextBox.BackColor = System.Drawing.Color.Azure
        TextCol.Width = 150
        ts1.GridColumnStyles.Add(TextCol)
' Add the DataGridTableStyle objects to the collection.
        MyDataGrid.TableStyles.Add(ts1)

    End Sub 'AddCustomDataTableStyle

The collection of DataGridColumnStyle objects (the
GridColumnStylesCollection) is accessed through the
System.Windows.Forms.DataGrid control's TableStyles property.

The System.Windows.Forms.DataGrid control automatically creates a
collection of DataGridColumnStyle objects for you when you set the
DataSource property to an appropriate data source. The objects created
actually are instantiations of one of the following classes that inherit
from DataGridColumnStyle: DataGridBoolColumn or DataGridTextBox class.

You can also create your own set of DataGridColumnStyle objects and add
them to the GridColumnStylesCollection. When you do so, you must set the
MappingName of each column style to the ColumnName of a DataColumn to
synchronize the display of columns with the actual data.

CAUTION   Always create DataGridColumnStyle objects and add them to the
GridColumnStylesCollection before adding DataGridTableStyle objects to the
GridTableStylesCollection. When you add an empty DataGridTableStyle to the
collection, DataGridColumnStyle objects are automatically generated for
you. Consequently, an exception will be thrown if you try to add new
DataGridColumnStyle objects with duplicate MappingName values to the
GridColumnStylesCollection.

The following articles contain examples and detailed explanations on how to
add custom columns and for mats:  
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbco...
vbtskcreatingcustomcolumntypesindatagrid.asp
Adding Tables and Columns to the Windows Forms DataGrid Control

DataGridColumnStyle Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre...
frlrfsystemwindowsformsdatagridcolumnstyleclasstopic.asp

Walkthrough: Creating a Master-Detail Windows Form
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbco...
vbwlkWalkthroughCreatingMaster-DetailWindowsForm.asp

Formatting Windows Forms DataGrids at Design Time
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbco...
vbtskformattingthedatagridatdesigntime.asp

Hope this helps.

Ken Robison, MCSD

Want to know more? Check out the MSDN at HTTP://msdn.microsoft.com or the
Microsoft Knowledge Base at support.microsoft.com
Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
This posting is provided "AS IS", with no warranties, and confers no rights.

 
 
 

master detail, datagrid mappingname question

Post by Jim Ross [MVP » Fri, 03 May 2002 23:51:21


Is this a Winform datagrid or a Webform datagrid? You'd do better to
post this to the appropriate group for the datagrid. [aspnet or
webcontrols for a webform datagrid]

That said, it sounds as if you are using auto-generated columns. To
get only the columns you want in your grid, turn off auto-generate and
define the columns individually. The Visual Studio "Property Builder"
for the grid will help you with that. That way you don't need to
define a new table.


>Hy

>In a master detail form I like to show the details in a datagrid. When I set
>the datagrid.dataSource to the dataset and the datagrid.dataMember to the
>table, a default datagrid table style is aperenty made and shows ok! Also
>showing the table key column and the referential column witch is not what I
>want.
>So I try to make my own table style; but what ever mapping name I use for my
>own datagridtablestyle the default is still shown in my grid. also when I
>set the datagrid.datasource to the dataset-master table and the datagrid
>dataMember to the refering detail table it still dus not work.

>How this should be done???

>many thanks in advance.

>Rob Warning

Jim Ross
MS MVP [ASP.NET][VC/MFC emeritus]

To send email, change 'lotsofspamthroughhere' to 'msn' but please ask all questions in the newsgroups, not via private mail