# .NET and More > WPF, WCF, WF >  DataGridView in WPF

## eugz

Hi All.
I found interesting code to use DataGridView in WPF.
XAML

```
<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

```
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.

----------


## techgnome

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

-tg

----------


## eugz

Thanks for replay.
I modified VB part by this way and lost DataGridView


```
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.

----------


## techgnome

What is this:


```
myDG.DataContext = From a In dsNorthwind.Products Order By "ProductName"
```

Here was the original example:


```
myDG.DataSource = dt
```

Sooo.....


```
myDG.DataSource = dsNorthwind.Tables(0)
```

-tg

----------


## eugz

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.

----------


## jmcilhinney

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.

----------


## eugz

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.

----------


## jmcilhinney

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".

----------


## eugz

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.

----------


## DeanMc

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!



```
<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>
```

----------


## eugz

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:

```
<Grid>
      <wfi:WindowsFormsHost Name="windowsFormsHost1" Width="Auto" Height="Auto">
          <wf:DataGridView x:Name="myDG" BackColor="Beige" RowHeadersVisible="False" />
      </wfi:WindowsFormsHost>
</Grid>
```

Thanks.

----------

