none
DataSet => ObservableCollection => Datagrid without making objects?

    Question

  • Hello all,

    I just want to test something:

    Got a table tblCustomer on SQL Server with firstname , lastname,...

    Now I want to display all those customers in a datagrid with the full functionality so I can edit, add or delete one from the list and directly update in SQL Server. Would it be possible to do this without making objects from each row? (like new Customer and then add it to a ObservableCollection and then bind it to the datagrid)

    So I don't have to:

    - write SQL to update, add, delete

    - write new classes for each table I want to use or adjust a class when I add a new field

    And would it be possible to view a detailed window about a selected customer in the datagrid?

     

    (My goal is get speed of programming and designing from Access and find a way to use it in WPF)

    Thanks in advance!

     

     

     

    Tuesday, June 01, 2010 8:51 AM

Answers

  • Hi Gaz3ll,

    Maybe you can try to use the RowDetailsTemplate of the DataGrid, and set theRowDetailsVisibilityMode as VisibleWhenSelected, and bind the columns data to the control in the RowDetailsTemplate.
    Something likes the follows:

    <dg:DataGrid  AutoGenerateColumns="False"

          ItemsSource="{Binding ...}"

          RowDetailsVisibilityMode="VisibleWhenSelected">

        <dg:DataGrid.RowDetailsTemplate>

            <DataTemplate>

                <StackPanel Orientation="Vertical" Margin="5">

                    <TextBox Foreground="CadetBlue" FontSize="20" Width="400" TextWrapping="Wrap" Text="{Binding ...}"/>

                </StackPanel>

            </DataTemplate>

    </dg:DataGrid.RowDetailsTemplate>


    </dg:DataGrid>

    You can refer the like as follows for more about RowDetailsTemplate of DataGrid:
    http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.rowdetailstemplate(VS.95).aspx

    If you want to use MVVM pattern, you need to create the classes for the tables, for example Customer class and Order class, because the MVVM is a object-oriented design pattern.

    Hope this helps.

    Best regards,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Tuesday, June 08, 2010 2:44 AM
    Thursday, June 03, 2010 7:37 AM
  • I responded to this post yesterday and it appears to have black holed.

    As Linda says, you need types if you're going to use MVVM.  It's a good pattern and an "no-brainer" choice for WPF development.

    So my suggestion is to use Entity Framework 4 and generate the classes.  So you have those classes but you don't write them, you use a t4 template to generate them.  That gives you the speed since it's kind of a drag drop and click of a button.  It also gives you the advantages of decoupling the database structure from the view. 

    • Marked as answer by Linda Liu Tuesday, June 08, 2010 2:44 AM
    Thursday, June 03, 2010 9:05 AM

All replies

  • Have a look at the following article, i believe it describes exactly what you want in detail.

    http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx


    Best Regards,
    Tom

    If you have found this post helpful, please click the Vote as Helpful link (the green triangle and number on the top-left).
    If this post answers your question, click the Mark As Answered link below. It helps others who experience the same issue in future to find the solution.
    • Edited by AntiDesigner Tuesday, June 01, 2010 8:58 AM typo
    Tuesday, June 01, 2010 8:58 AM
  • Thank you,

    but what if I want to open a detailwindow and show all the fields of the selected customer when I double click on a datagridrow? (working with the MVVM pattern)

    The event is not the problem, but what do I have to inject into the detailwindow so I can access all the fields? Now I'm trying to pass a datagridrow but I don't think that will work...

     

    Tuesday, June 01, 2010 1:27 PM
  • Hi Gaz3ll,

    Maybe you can try to use the RowDetailsTemplate of the DataGrid, and set theRowDetailsVisibilityMode as VisibleWhenSelected, and bind the columns data to the control in the RowDetailsTemplate.
    Something likes the follows:

    <dg:DataGrid  AutoGenerateColumns="False"

          ItemsSource="{Binding ...}"

          RowDetailsVisibilityMode="VisibleWhenSelected">

        <dg:DataGrid.RowDetailsTemplate>

            <DataTemplate>

                <StackPanel Orientation="Vertical" Margin="5">

                    <TextBox Foreground="CadetBlue" FontSize="20" Width="400" TextWrapping="Wrap" Text="{Binding ...}"/>

                </StackPanel>

            </DataTemplate>

    </dg:DataGrid.RowDetailsTemplate>


    </dg:DataGrid>

    You can refer the like as follows for more about RowDetailsTemplate of DataGrid:
    http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.rowdetailstemplate(VS.95).aspx

    If you want to use MVVM pattern, you need to create the classes for the tables, for example Customer class and Order class, because the MVVM is a object-oriented design pattern.

    Hope this helps.

    Best regards,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Tuesday, June 08, 2010 2:44 AM
    Thursday, June 03, 2010 7:37 AM
  • I responded to this post yesterday and it appears to have black holed.

    As Linda says, you need types if you're going to use MVVM.  It's a good pattern and an "no-brainer" choice for WPF development.

    So my suggestion is to use Entity Framework 4 and generate the classes.  So you have those classes but you don't write them, you use a t4 template to generate them.  That gives you the speed since it's kind of a drag drop and click of a button.  It also gives you the advantages of decoupling the database structure from the view. 

    • Marked as answer by Linda Liu Tuesday, June 08, 2010 2:44 AM
    Thursday, June 03, 2010 9:05 AM