Foundation Series seven wpf_MVVM_007 modify a data table

Modify the row of data in the table:
(1): declare a class, get the current row is selected, the field will need to be modified to backfill the corresponding control.
(2): click on the Save button to get the data modified by the user.
(3): declare a list, use linq check out the current line data.
(4): The fields need to be modified, replaced in the list.
(5): The list of the table fields, replacing the database.
Examples:
(1):
public class myClass
{
public String field a GET {; the SET;}
public int field two GET {; the SET;}?
Public Double GET field three {; the SET;}
}
// Gets the currently selected table row
myClass AS = myDataGrid.CurrentItem myClass myRow;
// assignment to the control
myTextBox.Text = myRow a field .trim ();.
myComboBox.SelectedValue = myRow two fields;.
myButton.Content = myRow three fields;.
(2):
Private void btnUpdate_Click (Object SENDER, RouteEventArgs E)
{
IF (myDataGrid.SelectedItem == null) return;
// Get the currently selected table row
myClass myRow = myDataGrid.SelectedItem AS myClass;
int My primary key ID = Convert.ToInt32 (myRow primary key ID.);
var = myModel.Table1.Where myTable (. p => p primary key ID == my primary key ID) .SingleOrDefault ();
IF (! myTable = null)
{
myTable Convert.ToString field a = (myTextBox.text);.
. myTable two fields = Convert.ToInt32 ( myComboBox.SelectedValue);
. myTable three fields = Convert.ToDouble (myButton.Content);

			myModel.Entry( myTable ).State = System.Data.Entity.EntityState.Modified;
			if( myModel.SaveChanges() > 0 ) 
				Message.Show("修改成功");
			else 
				Message.Show("修改失败");
		}	
	}

Notes:
[1] modified fields, should then be assigned to verify Table, length field, and type, needs to be consistent with the database.
[2]
received by the query linq a need to modify the field can be used to declare Table instead var:
the Table1 myTable myModel.Table1.Where = (. = P> P primary key ID == my primary key ID) .SingleOrDefault () ;
comparison: var myTable = myModel.Table1.Where (. p => p primary key ID == my primary key ID) .SingleOrDefault ();
[3]
where two ways of getting the current row table:
clicks within a table, get the current row : myDataGrid.CurrentItem;
select a data table, click the edit button in the current row of tables outside, get: myDataGrid.SelectedItem;

Published 130 original articles · won praise 36 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44548307/article/details/104768999