Validating rows when using LINQ to SQL

来源:互联网 发布:网络无武侠小说 编辑:程序博客网 时间:2024/06/06 02:31
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using DevExpress.XtraGrid;using DevExpress.XtraEditors;using DevExpress.XtraGrid.Columns;using System.Data.Linq;namespace WindowsFormsApplication51 {    public partial class Form1 : Form {        DataClasses1DataContext d = new DataClasses1DataContext();        CurrencyManager c;        public Form1() {            InitializeComponent();            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;            gridView1.RowUpdated += new DevExpress.XtraGrid.Views.Base.RowObjectEventHandler(gridView1_RowUpdated);            bindingSource1.DataSource = d.Products.ToList();        }        bool ifValid = true;        void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e) {            try {                if (e.RowHandle == GridControl.NewItemRowHandle)                    d.Products.InsertOnSubmit((Product)e.Row);                d.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);            }            catch (Exception ex) {                // refresh data                bindingSource1.DataSource = null;                d.Dispose();                d = new DataClasses1DataContext();                bindingSource1.DataSource = d.Products.ToList();                BeginInvoke(new Action(() => {                    gridView1.FocusedRowHandle = e.RowHandle;                    gridView1.SetColumnError(null, ex.Message);                    if (e.RowHandle == GridControl.NewItemRowHandle) {                        gridView1.AddNewRow();                    }                    // if you want to keep user changes                     PropertyDescriptorCollection props = TypeDescriptor.GetProperties(e.Row);                    foreach (GridColumn col in gridView1.VisibleColumns) {                        PropertyDescriptor prop = props.Find(col.FieldName, false);                        if (prop != null)                            gridView1.SetFocusedRowCellValue(col, prop.GetValue(e.Row));                    }                }));            }        }        void c_ListChanged(object sender, ListChangedEventArgs e) {             if (e.ListChangedType == ListChangedType.ItemAdded) {                Product p = c.List[e.NewIndex] as Product;            }        }        void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) {            e.Valid = ifValid;        }        private void simpleButton1_Click(object sender, EventArgs e) {            try {                d.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);            }                      catch(Exception){                d.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, d.Products);                gridControl1.RefreshDataSource();                gridView1.FocusedRowHandle = GridControl.NewItemRowHandle;                gridView1.FocusedRowHandle = 1;            }        }    }}

0 0