Results 1 to 11 of 11

Thread: DataGridView in WPF

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    DataGridView in WPF

    Hi All.
    I found interesting code to use DataGridView in WPF.
    XAML
    Code:
    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="Window2" Height="300" Width="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <WindowsFormsHost Grid.Row="0">
                <WinForms:DataGridView x:Name="myDG">
                </WinForms:DataGridView>
            </WindowsFormsHost>
        </Grid>
    </Window>
    VB code
    Code:
    Imports System.Windows.Data
    Imports System.Windows.Forms.Integration
    Imports System.Data
    
    Partial Public Class Window1
    
        Public Sub New()
            InitializeComponent()
    
            Dim dt As New DataTable()
            dt.Columns.Add("Col1", GetType(String))
            dt.Columns.Add("Col2", GetType(String))
            dt.Rows.Add("Hello", "World")
            dt.Rows.Add("Green", "Apple")
            dt.Rows.Add("Big", "Orange")
            dt.Rows.Add("Fresh", "Water")
    
            myDG.DataSource = dt
        End Sub
    End Class
    How to modify this code using DataSet and TableAdapter?
    Thanks.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: DataGridView in WPF

    take out the code that adds the cols and the rows, and replace it with the dataadaptor and fill your datatable.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    Re: DataGridView in WPF

    Thanks for replay.
    I modified VB part by this way and lost DataGridView
    Code:
    Imports System.Windows.Data
    Imports System.Windows.Forms.Integration
    Imports System.Data
    
    Partial Public Class Window1
        Private taNorthwind As New NorthwindDataSetTableAdapters.ProductsTableAdapter
        Private dsNorthwind As New NorthwindDataSet
    
        Public Sub New()
            InitializeComponent()        
            taNorthwind.Fill(Me.dsNorthwind.Products)
            myDG.DataContext = From a In dsNorthwind.Products Order By "ProductName"
        End Sub
    End Class
    What is not correct?
    Thanks.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: DataGridView in WPF

    What is this:
    Code:
    myDG.DataContext = From a In dsNorthwind.Products Order By "ProductName"
    Here was the original example:
    Code:
    myDG.DataSource = dt
    Sooo.....
    Code:
    myDG.DataSource = dsNorthwind.Tables(0)
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    Re: DataGridView in WPF

    Thanks a lot. It is works.
    For instance, I would like to display on DataGridView only ProductName and UnitPrice fields from Products table in this form and other form I will use all fields from same table. How to declare according fields for this DataGridView.
    Thanks.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataGridView in WPF

    First up, please don't post the same question more than once.

    http://www.vbforums.com/showthread.php?t=563219

    If you think you've posted in the wrong place then ask a moderator to move your thread.

    As for the question, if you're going to let the grid create the columns for you then it's going to create them all. You're either going to have to hide or remove the columns you don't want or, preferably, don't let the grid create any columns at all and you create only those that you do want. For the second option you must set AutoGenerateColumns to False.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    Re: DataGridView in WPF

    Thanks for replay.
    That is my question. How to create columns on a grid and how to display only columns that I want. Can you show me and explain it briefly?
    Thanks.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataGridView in WPF

    You need to create an object of the appropriate type, e.g. DataGridViewTextBoxColumn, DataGridViewComboBoxColumn, etc., then set its properties and add it to the grid's Columns collection. You need to set its DataPropertyName property to the name of the data source column you want it bound to. For instance, if you had a Person table with FirstName, LastName and DateOfBirth columns but you only wanted to display the first name and last name in the grid, you'd create two DataGridViewTextBoxColumn objects and set their DataPropertyName properties to "FirstName" and "LastName".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    Re: DataGridView in WPF

    jmcilhinney, I will appreciate if you show me example. I have problem how to create an object DataGridViewTextBoxColumn and DataGridViewComboBoxColumn in WPF
    Thanks for help.
    Last edited by eugz; Mar 27th, 2009 at 11:29 AM.

  10. #10
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: DataGridView in WPF

    Sorry to butt in but if you need to highlight your xaml you can do so here:
    x:Light

    Its still in BETA but a new release is due shortly!

    Code:
    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="Window2" Height="300" Width="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <WindowsFormsHost Grid.Row="0">
                <WinForms:DataGridView x:Name="myDG">
                </WinForms:DataGridView>
            </WindowsFormsHost>
        </Grid>
    </Window>

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    Re: DataGridView in WPF

    Hi All.
    I tried to specify Backcolor in XAML and run program result of Backcolor is not effected but DataGridView display me columns what I want. When I change DataGridView to DataGrid in VB part I got error massege: "AutoGenerateColumns is not a member of System.Windows.Forms.DataGrid". After AutoGenerateColumns line is commented I got result of Backcolor but of cause DataGrid display me all columns of a table. Is it possible to get effect of Backcolor and display according columns on DataGridView? That is XAML:
    Code:
    <Grid>
          <wfi:WindowsFormsHost Name="windowsFormsHost1" Width="Auto" Height="Auto">
              <wf:DataGridView x:Name="myDG" BackColor="Beige" RowHeadersVisible="False" />
          </wfi:WindowsFormsHost>
    </Grid>
    Thanks.
    Last edited by eugz; Mar 31st, 2009 at 11:19 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width