DataGrid Background code for the example
1) The Entity Framework 6.1 reads the city information data from the S_city table in the database (local database)/test, reads the province information from the S_ province table, The anti-data is then displayed to a DataGrid on a window in WPF by way of binding. The specific code is as follows.
Using system;using system.collections.generic;using system.collections.objectmodel;using System.Data.Entity;using System.linq;using system.text;using system.threading.tasks;using system.windows;using System.Windows.Controls; Using system.windows.data;using system.windows.documents;using system.windows.input;using System.Windows.Media; Using system.windows.media.imaging;using system.windows.shapes;using wpfapp1.models; namespace wpfapp1{//<summary>///Windowgrid.xaml interactive logic///</summary> public partial class Win Dowgrid:window {public Windowgrid () {InitializeComponent (); Database.setinitializer<testdbcontext> (NULL); } private void Btnrefresh_click (object sender, RoutedEventArgs e) {BINDDRP (); GetData (); } Testdbcontext db = new Testdbcontext (); protected void GetData () {list<s_city> List = db. S_city.tolist<S_City> (); Gridcitys.itemssource = list; } protected void Binddrp () {list<s_province> List = db. S_province.tolist<s_province> (); Cboprovince.itemssource = list; Provincelist = list; Public list<s_province> provincelist {get; set;} private void Btnupdate_click (object sender, RoutedEventArgs e) {try {s_city City = (s_city) Gridcitys.selecteditem; City. dateupdated = DateTime.Now; Txtmsg.text = City. Provinceid + "//" + City. CityName; S_city modifycity = db. S_city.find (city. Cityid); modifycity = City; Db. SaveChanges (); Txtmsg.text + = "saved successfully!"; } catch (Exception ex) {txtmsg.text + = ex. Message; } } }}
2) After writing the above code, press F5, the program runs up, click "Refresh". You will see the following results. As a result.
3) The DataGrid automatically generates some columns, but in fact we don't need these automatically generated columns. So you need to set the AutoGenerateColumns to False. Take the 2nd step again. You will see the following results.
4) Although the content display of the drop-down box is implemented, there is a problem that does not show what should be displayed. After the last query network to know, promise to make the following changes.
DataGrid How to bind a ComboBox column
Datagridcomboboxcolumn has the following requirements for the data source:
To populate the drop-down list, first set the ItemsSource property of the ComboBox by using one of the following options:
- 1, static resources. For more information, see StaticResource Markup extensions.
- 2, X: Static code entity. For more information, see x:static Markup extensions.
- 3. An inline collection of comboboxitem types.
1) When using the DataGrid, sometimes it is necessary to make some list as a ComboBox, then naturally think of using datagridcomboboxcolumn, but if you are using ItemsSource data binding background object, you will find that this is not used at all.
2) After the default refresh button, there is no data in the drop-down box. I looked at the code carefully, the foreground code is not data binding, and the background code does not write the bound statement. The foreground code is as follows. If such as.
<datagridcomboboxcolumn clipboardcontentbinding= "{x:null}" header= "Provinceid" selectedvaluebinding= "{x:Null}" Selecteditembinding= "{x:null}" textbinding= "{x:null}"/>
3) I have made some changes to the program, the foreground code is not modified, or the following.
<datagridcomboboxcolumn x:name= "Cboprovince" clipboardcontentbinding= "{x:null}" header= "ProvinceID" Selectedvaluebinding= "{x:null}" selecteditembinding= "{x:null}" textbinding= "{x:null}"/>
The background code is as follows, and the drop-down box is bound.
protected void Binddrp () { list<s_province> List = db. S_province.tolist<s_province> (); Cboprovince.itemssource = list; }
The results are as follows. The bindings were successful, but the values I needed to display were not displayed.
4) The following changes are made to the foreground code. The Datagridcomboboxcolumn is bound. Then F5, run the discovery, the drop-down box display is normal, but no matter how I change, the DataGrid in the "Provinceid" by default just show blank, nothing is displayed, such as. The foreground code is as follows.
<datagridcomboboxcolumn x:name= "Cboprovince" clipboardcontentbinding= "{x:null}" header= "ProvinceID" Selectedvaluepath= "Provinceid" displaymemberpath= "Provincename" selecteditembinding= "{x:Null}" TextBinding= "{ Binding Provincename} "/>
5) But when I load the data into the DataGrid, the Provinceid column is still empty by default and does not display data such as the province name I want to display. After some search and study, the foreground code is finally modified as follows. After this modification, after the DataGrid binds the data by default, the Provinceid column achieves the result I want. Such as.
<datagridcomboboxcolumn x:name= "Cboprovince" clipboardcontentbinding= "{x:null}" header= "ProvinceID" Selectedvaluepath= "Provinceid" selectedvaluebinding= "{Binding path=provinceid,updatesourcetrigger= propertychanged} " displaymemberpath=" Provincename "selecteditembinding=" {x:null} "> </ Datagridcomboboxcolumn>
WPF Getting Started Tutorial series 21--datagrid Example (ii)