WPF Learning using the DataGrid

Source: Internet
Author: User
Tags reference xmlns

We have a large number of grid controls in WinForm or webform that we can use, datagridview,gridview,repeater, and so on, so that the grid data space provides us with a great deal of convenience to allow data to be displayed in a definable way and provide such navigation , paging, sorting, filtering, data updating and other additional operations, and programmers need to pay little. But in WPF we don't usually have such superior grid controls, And to do this, in addition to using grid.rowdefinitions and grid.columndefinitions to create a grid, or to take advantage of the Listview.gridview view of the ListView control, we seem to have no choice. And for these uses do not have as powerful as the WinForm functions, more need programmers to control the display of data and provide additional operational implementation. Microsoft recently released the WPF Toolkit on CodePlex, which provides a WPF version of the DataGrid, as always powerful and easy to use, and today we're going to experience this powerful control.

1.Start with DataGrid

The WPF development team specifically rewrote a set of DataGrid to allow it to run on top of WPF, where you can get the source code and its DLL files for this toolkit, specifically how to write these controls, HTTP://WWW.CODEPLEX.COM/WPF. As you are interested in WPF, you need to study it. Because it is worthwhile to study.

After we get the WPFToolkit.dll file, we add a reference to it in the project, in XAML because the Wpftoolkit namespace is not part of the default mapping namespace for WPF and the. NET Framework. So you need to declare a reference to its namespace first in the XAML file, and then declare an instance object. To simplify our program, here we will let the grid control generate columns of its own based on the data source: autogeneratecolumns= "True".

<Window x:Class="DataGridIntro.SimpleDataGrid"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
  Loaded="Window_Loaded"
  Title="Simple DataGrid" Height="600" Width="800">
  <Grid>
    <dg:DataGrid x:Name="NorthwindDataGrid" AutoGenerateColumns="True" />
  </Grid>
</Window>

As with normal WinForm controls, next you need to assign data to their data sources. The difference is the same as for all WPF data controls, where the data source property is Itemsource. Before we declared the window's loaded event, we directly assigned the value:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   using (NorthwindDataContext dc = new NorthwindDataContext())
   {
      NorthwindDataGrid.ItemsSource = dc.Customers.ToList();
   }
}

Note The example uses LINQ to get data from the Customers table in the Northwind database and to give the DataGrid as a data source. As in Asp.net/winform, the list<> data is given directly to the Itemsource property. That's all you need to do, easy? Take a look at your results.

Oh, really nice, because we've only written 2 lines of code, and we've done all these functions, it's fantastic. Can't wait to try it? Come on, let's go.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.