internal static object GetKeyValue(object gridViewRow, IMvcHelper helper) { string keyFldName = string.Empty; object data = helper.GetDataSource(); var tbl = data as DataTable; if (tbl != null) { if (tbl.PrimaryKey != null) { if (tbl.PrimaryKey.Length > 0) keyFldName = tbl.PrimaryKey[0].ColumnName; } if (string.IsNullOrWhiteSpace(keyFldName)) keyFldName = tbl.Columns[0].ColumnName; var row = gridViewRow as System.Data.DataRow; if (row == null) row = (gridViewRow as System.Data.DataRowView).Row; var keyValue = row[keyFldName]; return new { Name = keyFldName, Value = keyValue }; } return gridViewRow; }
public ControllerAction(IMvcHelper helper, object gridViewRow) { this.helper = helper; this.gridViewRow = gridViewRow; }
public MvcDataGridView(DataGridView dataGridView, IMvcHelper imvc) { this.iMvc = imvc; if (this.iMvc.IsRegistered) return; #region 执行控制器的子操作 Action<int, int> actExecPost = (iFlag, iCurrentRowIndex) => { if (iCurrentRowIndex > -1) this.CurrentRow = dataGridView.Rows[iCurrentRowIndex]; else this.CurrentRow = null; object oPrmValue = null; string currActionName = string.Empty; if (iFlag == 0) { currActionName = this.newActionName; oPrmValue = this.funcNewValues == null ? null : this.funcNewValues(this); } else if (iFlag == 1) { currActionName = this.editActionName; oPrmValue = this.funcEditValues == null ? null : this.funcEditValues(this); } else if (iFlag == 2) { currActionName = this.deleteActionName; oPrmValue = this.funcDeleteValues == null ? null : this.funcDeleteValues(this); } if (string.IsNullOrWhiteSpace(currActionName)) return; if (oPrmValue == null && (this.funcNewValues == null || this.funcEditValues == null || this.funcDeleteValues == null)) { if (this.CurrentRow != null) { this.iMvc.GridFirstColumnCellValue = this.GetKeyValue(); } this.iMvc.Action(currActionName); } else this.iMvc.Action(currActionName, oPrmValue); }; #endregion #region DataGridView事件与属性 dataGridView.VirtualMode = true; dataGridView.CellEnter += (s, e) => { bool isSubmited = false; //当进入单元格时,判断是否添加或编辑,则引发对应子操作 if (bNew && currChangeRowIndex != e.RowIndex) { actExecPost(0, currChangeRowIndex); //System.Diagnostics.Debug.WriteLine("new row update:" + currChangeRowIndex); isSubmited = true; } else if (currChangeRowIndex != e.RowIndex && bChged) { actExecPost(1, currChangeRowIndex); //System.Diagnostics.Debug.WriteLine("edit update:" + currChangeRowIndex); isSubmited = true; } if (isSubmited) { bChged = false; bNew = false; } }; dataGridView.CellValueChanged += (s, e) => { bChged = true; }; dataGridView.CellLeave += (s, e) => { currChangeRowIndex = e.RowIndex; }; dataGridView.Leave += (s, e) => { //当GRID失去焦点时,判断提交数据的改变,并引发对应子操作 if (bNew) { //System.Diagnostics.Debug.WriteLine("new row(leave) update:" + currChangeRowIndex); actExecPost(0, currChangeRowIndex); bNew = false; bChged = false; } if (!bChged) return; //System.Diagnostics.Debug.WriteLine("ocx update:" + currChangeRowIndex); actExecPost(1, currChangeRowIndex); bChged = false; }; dataGridView.UserAddedRow += (s, e) => { bNew = true; }; dataGridView.CancelRowEdit += (s, e) => { bNew = false; bChged = false; }; #region 行头单元格选中,切换编辑或行选中 dataGridView.CellClick += (s, e) => { if (e.RowIndex < 0) return; if (e.ColumnIndex < 0) { dataGridView.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView.ClearSelection(); dataGridView.EndEdit(); dataGridView.Rows[e.RowIndex].Selected = true; return; } dataGridView.EditMode = DataGridViewEditMode.EditOnEnter; dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect; }; #endregion dataGridView.UserDeletingRow += (s, e) => { //System.Diagnostics.Debug.WriteLine("delete:" + e.Row.Index); actExecPost(2, e.Row.Index); }; #endregion }