DataGrid data binding usage Summary

Source: Internet
Author: User

Anyone who has developed ASP. NET knows that ASP. NET provides powerful list data controls, from ASP. net1.1

The DataGrid of the times, the gridview of ASP. net2.0, And the listview of ASP. net3.5

In silverlight2, a similar data list control is provided.

DataGrid, although Silverlight is a subset of WPF, it does not provide the datagridr control in WPF.

The DataGrid Control provides a flexible way to display table data. The built-in table types include text box columns.

, Check box column and template column, built-in support for sorting, lock column, and built-in support for drop-down display of a record details function.

Method 1: automatically generate Columns

First, define a data object and set the autogeneratecolumns attribute of the DataGrid to true, so that the DataGrid can automatically generate columns. In this way, the bound data can be automatically edited. It will automatically select datagridcheckboxcolumn and datagridtextcolumn. in your data object, setting whether the field attribute is writable or readable means that the column can be edited.

Data Object bookclass:

View code

Public class bookclass
{
Public String title {Get; set;} // use the datagridtextcolumn template automatically
Public String author {Get; set;} // use the datagridtextcolumn template automatically
Public datetime time {get {return datetime. Now ;}// unchangeable, read-only
Public bool ischeck {Get; set;} // use the datagridcheckboxcolumn template automatically

/// <Summary>
/// Instantiate
/// </Summary>
/// <Param name = "title"> title </param>
/// <Param name = "author"> prepared by </param>
/// <Param name = "ischeck"> whether to select </param>
Public bookclass (String title, string author, bool ischeck)
{
Title = title;
Author = author;
Ischeck = ischeck;
}
}

Data Binding control datagridbook: to conveniently detect whether to change the data object, add a button

View code

<Usercontrol xmlns: Data = "CLR-namespace: system. Windows. controls; Assembly = system. Windows. Controls. Data" X: class = "datagridtest. Control. datagridbook"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" width = "400" Height = "150">
<Grid X: Name = "layoutroot" background = "white">
<Data: DataGrid name = "gridautogeneratecolumns" autogeneratecolumns = "true">
</Data: DataGrid>
<Button name = "btntest" content = "detect" Click = "button_click" width = "60" Height = "24" verticalalignment = "bottom"/>
</GRID>
</Usercontrol>

The backend CS code is:

View code

/// <Summary>
/// Automatic generation of data Columns
/// </Summary>
Public partial class datagridbook: usercontrol
{
Public datagridbook ()
{
Initializecomponent ();
Databinding ();
}

Public void databinding ()
{
List <bookclass> List = new list <bookclass> () {New bookclass ("Legend of the Condor Heroes", "Jin Yong", true), new bookclass ("how the bad guys are made ", "Six channels", false )};
Gridautogeneratecolumns. itemssource = List;
}

/// <Summary>
/// Detection
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button_click (Object sender, routedeventargs E)
{
If (gridautogeneratecolumns. selectedindex =-1)
Return;
Bookclass book = gridautogeneratecolumns. selecteditem as bookclass;
MessageBox. Show (book. Title + "," + book. Author + "," + book. Time + "," + book. ischeck. tostring ());
}
}

Run page

Now let's modify the data in the DataGrid and check whether the data object has changed. After modifying the value content, we click "check ".

Except that the time attribute is readable and cannot be modified, data objects are changed.

Other properties of the DataGrid:

Canuserreordercolumns: whether columns can be dragged

Canuserresizecolumns: Can I set the column width?

Canusersortcolumns: Can I sort columns?

Use method 2 custom Columns

 

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.