Though WPF is a Wonderful technology, at times programmers get frustrated with the lack of certain controls. One such control is the DataGrid or GridView. Silverlight 2 infact has a DataGrid, hope in the next release of WPF it will be as feature rich as Windows Forms.
It is very much possible to use Windows Forms controls in WPF.
Start out by dragging a WindowsFormsHost into WPF Windows.
Create an object of System.Windows.Forms..DataGrid and set the Child Property of WindowsFormsHost to the newly created DataGrid object. WindowsFormsHost can have only one child as is obvious form the Child Property.
System.Windows.Forms.DataGrid dg = new System.Windows.Forms.DataGrid();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
windowsFormsHost1.Child = dg;
}
Now you can work with the DataGrid just like you work with it in Windows Forms.
Here is the complete code for connecting the DataGrid to a DataSet.
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
System.Windows.Forms.DataGrid dg = new System.Windows.Forms.DataGrid();
SqlConnection cnn;
SqlDataAdapter da;
DataSet ds = new DataSet();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
windowsFormsHost1.Child = dg;
cnn = new SqlConnection(@"Integrated Security=sspi;Initial Catalog=Shalvin;Data source=.\sqlexpress");
cnn.Open();
da = new SqlDataAdapter("select * from Categories", cnn);
da.Fill(ds, "Cat");
dg.DataSource = ds.Tables["Cat"];
}
Thursday, August 14, 2008
Wpf and Windows Forms Controls Interop
Labels:
DataGrid,
DataSet,
Shalvin,
SqlDataAdapter,
WPF,
WPF Interop
Subscribe to:
Post Comments (Atom)
Any idea on how to solve the z-order issue.
ReplyDelete