|
-
Mar 26th, 2009, 11:15 AM
#1
Thread Starter
Addicted Member
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.
-
Mar 26th, 2009, 12:25 PM
#2
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
-
Mar 26th, 2009, 01:40 PM
#3
Thread Starter
Addicted Member
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.
-
Mar 26th, 2009, 02:25 PM
#4
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
-
Mar 26th, 2009, 02:52 PM
#5
Thread Starter
Addicted Member
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.
-
Mar 26th, 2009, 08:02 PM
#6
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.
-
Mar 27th, 2009, 08:28 AM
#7
Thread Starter
Addicted Member
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.
-
Mar 27th, 2009, 08:35 AM
#8
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".
-
Mar 27th, 2009, 11:08 AM
#9
Thread Starter
Addicted Member
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.
-
Mar 27th, 2009, 07:00 PM
#10
Frenzied Member
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>
-
Mar 30th, 2009, 05:08 PM
#11
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|