有两种方式,添加NavBar
一种是采用devexpress 的
this.gridControl.UseEmbeddedNavigator = true;
第二种是采用 System.Windows.Forms.BindingNavigator
然后,gridctrl和BindingNavigator指向同一个数据源。
this.keHuBindingNavigator.BindingSource = this.keHuBindingSource;
this.keHuGridControl.DataSource = this.keHuBindingSource;
如采用System.Windows.Forms.BindingNavigator,则在其SaveItem事件中:
private void keHuBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.keHuBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.lMISV1DataSet); }
正常的:
private void gridViewProject_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e) { try { DataRowView drv = (DataRowView)e.Row; DataRow row = drv.Row; UpdateProject(row); } catch (System.Exception ex) { MyErrExcu.E(ex); } }
void UpdateProject(DataRow row) { try { string strProjectName = ""; //Trace.Assert(row.RowState != DataRowState.Unchanged); if (row.RowState == DataRowState.Unchanged) { return; } if (row.RowState == System.Data.DataRowState.Added) { //不允许添加重名项目 strProjectName = row["ProjectGroupName"].ToString(); if (this.projectTableTableAdapter.GetDataByProjectName(strProjectName).Count > 0) { MessageBox.Show("存在重复的项目"); return; } } else if (row.RowState == System.Data.DataRowState.Modified) { //不允许改到一个重名的项目 //不允许添加重名项目 strProjectName = row["ProjectGroupName"].ToString(); MyDataSet.ProjectTableDataTable tmpTable = this.projectTableTableAdapter.GetDataByProjectName(strProjectName); if (tmpTable.Count > 0) { //检查是不是自己所在的行,如果不是,则报错 int nCurProjectId = Convert.ToInt32(row["ProjectID"].ToString()); LMISV1DataSet.ProjectTableRow curRow = (LMISV1DataSet.ProjectTableRow)tmpTable.Rows[0]; if (nCurProjectId != curRow.ProjectID) { MessageBox.Show("存在重复的项目"); return; } } } else { } this.projectTableTableAdapter.Update(row); //UpdateRelCaption(strProjectName); } catch (System.Exception ex) { MyErrExcu.E(ex); } }
当然,多数情况下,下面几句就可以了:
private void gridViewXXX_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e) { DataRowView drv = (DataRowView)e.Row; DataRow row = drv.Row; this.xxxTableAdapter.Update(row); curcsr = null; }
private void gridViewProject_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) { try { DataRowView drv = (DataRowView)e.Row; DataRow row = drv.Row; //不允许改到一个重名的项目 //不允许添加重名项目 string strProjectName = row["ProjectGroupName"].ToString(); if (System.String.IsNullOrEmpty(strProjectName)) { e.ErrorText = "请指定项目名\r\n"; e.Valid = false; } MyDataSet.ProjectTableDataTable tmpTable = this.projectTableTableAdapter.GetDataByProjectName(strProjectName); if (tmpTable.Count > 0) { //检查是不是自己所在的行,如果不是,则报错 int nCurProjectId = Convert.ToInt32(row["ProjectID"].ToString()); LMISV1DataSet.ProjectTableRow curRow = (LMISV1DataSet.ProjectTableRow)tmpTable.Rows[0]; if (nCurProjectId != curRow.ProjectID) { e.ErrorText = "存在重复的项目\r\n"; e.Valid = false; return; } } } catch (System.Exception ex) { e.ErrorText = "有效性检查失败\r\n"; e.Valid = false; MyErrExcu.E(ex); } }
private void gridControlShr_EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e) { int hRowHandle = gridViewShr.FocusedRowHandle; if (hRowHandle < 0) return; object objRowId = gridViewShr.GetRowCellValue(hRowHandle, "id"); if (null == objRowId) { return; } int nId = Convert.ToInt32(objRowId); //删除当前项 if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Remove) { this.relShrProjectTableAdapter.DeleteById(nId); } }
if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.Append) { labchon: InputBoxResult test = InputBox.Show(this, "请输入项目组名称:", "项目组名称", "", 100, 0); if (test.ReturnCode == DialogResult.OK) { //不允许添加重名项目 m_tmpstrProjectName = test.Text; if (this.projectTableTableAdapter.GetDataByProjectName(m_tmpstrProjectName).Count > 0) { if (System.Windows.Forms.DialogResult.Yes == MessageBox.Show("存在重复的项目", "重新设定吗", MessageBoxButtons.YesNo)) { goto labchon; } } }
}
private void gridViewProject_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e) { DevExpress.Utils.Menu.DXMenuItem item = new DevExpress.Utils.Menu.DXMenuItem(); item.Caption = "克隆项目组"; item.Click += new EventHandler(this.createNewDocumentItemClick); e.Menu.Items.Add(item); } private void createNewDocumentItemClick(object sender, EventArgs e) { int hRowHandle = this.gridViewProject.FocusedRowHandle; if (hRowHandle < 0) return; object objRowId = gridViewProject.GetRowCellValue(hRowHandle, "ProjectID"); object objRowName = gridViewProject.GetRowCellValue(hRowHandle, "ProjectGroupName"); if (null == objRowId) { return; } int nId = Convert.ToInt32(objRowId); InputBoxResult test = InputBox.Show(this, "请输入项目组名称:", "项目组名称", objRowName.ToString() + "克隆", 100, 0); if (test.ReturnCode == DialogResult.OK) { //不允许添加重名项目 string strProjectName = test.Text; if (this.projectTableTableAdapter.GetDataByProjectName(strProjectName).Count > 0) { MessageBox.Show("存在重复的项目"); return; } DT.LMISDAL.BLL.ProjectTable mProjectDalBLL = new DT.LMISDAL.BLL.ProjectTable(); if (mProjectDalBLL.CloneProject(nId, strProjectName)) { LoadData(); syslog.Username = user.USERNAME; syslog.Events = "克隆项目组"; syslog.Bz = "原项目ID为" + nId.ToString(); syslogdal.AddAInfo(syslog); MessageBox.Show("克隆成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("添加失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } }
private void gridViewShr_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e) { int hRowHandle = this.gridViewProject.FocusedRowHandle; if (hRowHandle < 0) return; object objRowId = this.gridViewProject.GetRowCellValue(hRowHandle, "ProjectID"); if (null == objRowId) { MessageBox.Show("请选择一个项目"); return; } int nCurProjId = Convert.ToInt32(objRowId); DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView; view.SetRowCellValue(e.RowHandle, view.Columns["ProjectId"], nCurProjId.ToString()); view.ShowEditor(); }
在devexpress的示例中,总是使用这种暴力的方式存入数据库
void SaveItems()
{
this.Validate();
this.resourcesBindingSource.EndEdit();
this.resourcesTableAdapter.Update(this.schedulerDBDataSet);
schedulerDBDataSet.AcceptChanges();
}
或是这样:
private void schedulerStorage_AppointmentsChanged(object sender, DevExpress.XtraScheduler.PersistentObjectsEventArgs e)
{
this.appointmentsTableAdapter.Update(schedulerDBDataSet);
schedulerDBDataSet.AcceptChanges();
}
理论上,与上面, this.xxxTableAdapter.Update(row);
应当无差别,性能差别应当不大,因为可以在 schedulerDBDataSet中记录下哪些行发生了改变
private void gridViewCSR_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
if (gridViewCSR.FocusedRowHandle == DevExpress.XtraGrid.GridControl.NewItemRowHandle)
{
switch (gridViewCSR.FocusedColumn.FieldName)
{
case "ChaoSongRenId":
if (null != curcsr)
{
gridViewCSR.ActiveEditor.EditValue = curcsr.id.ToString();
}
break;
}
}
}
private void gridViewProject_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
//
//得到proect 名称,然后设置到下面的grid的标题上
int hRowHandle = e.FocusedRowHandle;
if (hRowHandle < 0)
return;
object objRowId = gridViewProject.GetRowCellValue(hRowHandle, "ProjectID");
object objRowName = gridViewProject.GetRowCellValue(hRowHandle, "ProjectGroupName");
if (null != objRowId)
{
int nCurProjectId = Convert.ToInt32(objRowId);
if (tabCtrlVgrid.SelectedIndex != 0)
{
tabCtrlVgrid.SelectedIndex = 0;
}
}
if (null != objRowName)
{
this.gridViewShr.ViewCaption = objRowName.ToString();
}
}
this.projectTableTableAdapter.Fill(this.lMISV1DataSet.ProjectTable);
shouhuorenv2BindingSource.Filter = "id=" + nId.ToString();
窗体加载时,初始化一个过滤器
private void dlgSelectCsrSimple_Load(object sender, EventArgs e)
{
if (!System.String.IsNullOrEmpty(m_strProjectName))
{
string strFilter = "StartsWith([ProjectGroupName], '" + m_strProjectName + "')";
DevExpress.XtraGrid.Views.Base.ViewFilter curFilter = this.gridViewVCsrProject.ActiveFilter;
//
DevExpress.XtraGrid.Columns.ColumnFilterInfo filter = new DevExpress.XtraGrid.Columns.ColumnFilterInfo(strFilter);
curFilter.Add(colProjectGroupName, filter);
}
}