📄 frmbasedataman.cs
字号:
this.MinimizeBox = false;
this.Name = "FrmBaseDataMan";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "基础数据管理";
this.Closing += new System.ComponentModel.CancelEventHandler(this.FrmBaseDataMan_Closing);
((System.ComponentModel.ISupportInitialize)(this.dgDisplay)).EndInit();
this.grpDescription.ResumeLayout(false);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void btnAddNode_Click(object sender, System.EventArgs e)
{//添加节点
//如果树中还没有节点的话,就说明需要初始化数据库。
//并且还要将以后显示的树第一层树节点添加完毕
if(this.trDataFunction .Nodes.Count!=0)
{
DataRow drNew=this.dtBDU .NewRow ();
//如果网格中还没有数据的话,可以先在文本框中填写数据,然后再点击添加按钮
//但是必须要求txtID方框不空
if(this.dvBDU .Count ==0 && this.txtName.Text!="")
{
drNew["sectionID"]=this.txtSectionID.Text;
drNew["name"]=this.txtName.Text ;
drNew["parentID"]=this.bduCurrent .intID;
drNew["description"]=this.txtDescription.Text;
}
else
{
//新生成一行
drNew["sectionID"]="";
drNew["name"]="";
drNew["parentID"]=this.bduCurrent .intID;
drNew["description"]="";
}
this.dtBDU.Rows .Add (drNew);
}
else
{
InitBaseDataTable();
this.dgDisplay .DataSource=this.dvBDU;
return;
}
//将当前行移到新添加的行处
BindingManagerBase bm;
bm = this.BindingContext[this.dvBDU ];
bm.Position = bm.Count;
this.txtSectionID.ReadOnly=false;
this.txtName.ReadOnly =false;
this.txtDescription.ReadOnly =false;
this.btnSave .Enabled =true;
this.btnCancelOperation.Enabled=true;
}
private void trDataFunction_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
//找到选中节点
LookForCurrentRow(trDataFunction .SelectedNode);
//根据当前节点的ID设置视图的行过滤器
this.dvBDU .RowFilter ="parentID="+bduCurrent.intID.ToString ();
this.dgDisplay .DataSource=this.dvBDU;
if(this.dgDisplay.VisibleRowCount>0)
{
BindingManagerBase bm;
bm = this.BindingContext[this.dvBDU ];
bm.Position = bm.Count;
}
}
private void btnSave_Click(object sender, System.EventArgs e)
{
//如果数据视图没有可显示的记录的话,就不做处理,
//否则要结束当前行的编辑操作
//可以防止有的行仍然处于编辑状态还无法插入
if(this.dvBDU .Count !=0)
{
BindingManagerBase bm = this.BindingContext[this.dvBDU];
DataRowView drv;
drv = (DataRowView)bm.Current;
drv.EndEdit();
}
//更新数据源
new BaseDataManBLC ().UpdateDataSourceBLC(dsBDU);
//重新取得数据,并从新生成树
dsBDU=new BaseDataManBLC().GetAllBaseData ();
dtBDU=dsBDU.Tables [0];
dvBDU =dtBDU.DefaultView ;
RefreshTree();
//清空三个文本框中的数据防止以前的数据干扰
this.txtSectionID.Text="";
this.txtName.Text ="";
this.txtDescription.Text ="";
this.trDataFunction .SelectedNode=this.trDataFunction .Nodes [0];
//重新绑定三个文本框
this.txtSectionID.DataBindings.Clear();
this.txtName.DataBindings .Clear ();
this.txtDescription .DataBindings .Clear ();
this.txtSectionID.DataBindings.Add("Text",dvBDU,"sectionID");
this.txtName.DataBindings .Add ("Text",dvBDU,"name");
this.txtDescription .DataBindings .Add ("Text",dvBDU,"description");
this.txtSectionID.ReadOnly=true;
this.txtName.ReadOnly =true;
this.txtDescription .ReadOnly =true;
this.btnSave .Enabled =false;
this.btnCancelOperation.Enabled =false;
}
private void btnDeleteNode_Click(object sender, System.EventArgs e)
{
//如果没有没有显示的当前行就提示出错
if(this.dvBDU .Count!=0)
{
//弹出对话框,确认是否真的删除
string strMsg="确定要删除当前行?按确定就删除,取消就撤消";
string strCaption="提示";
MessageBoxButtons buttons = MessageBoxButtons.OKCancel;
DialogResult result=MessageBox.Show(this, strMsg, strCaption, buttons,MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if(result == DialogResult.OK)
{
this.dvBDU.AllowDelete=true;
this.dvBDU.AllowEdit=true;
this.dvBDU.AllowNew=true;
this.dvBDU.Delete (this.dgDisplay .CurrentRowIndex);
this.btnSave.Enabled =true;
this.btnCancelOperation.Enabled =true;
}
}
else
{
MessageBox.Show ("没有纪录,无法删除!","错误");
}
}
private void btnCancelOperation_Click(object sender, System.EventArgs e)
{
//撤消所有的操作
this.dtBDU.RejectChanges ();
this.RefreshTree();
this.btnSave .Enabled =false;
this.btnCancelOperation.Enabled =false;
}
private void RefreshTree()
{//功能:刷新功能树,即清空树中原有的节点,然后重新生成功能树
this.trDataFunction.Nodes.Clear();
new BaseDataManBLC().MakeTree(dtBDU,trDataFunction);
}
private void LookForCurrentRow(TreeNode tnParameters)
{
foreach(DataRow dr in this.dtBDU.Rows)
{
if(dr.RowState!=DataRowState.Deleted)
{
if(dr["ID"].Equals(tnParameters.Tag))
{
this.bduCurrent.intID =Convert.ToInt16 (dr["ID"]);
this.bduCurrent .sectionID =dr["sectionID"].ToString();
this.bduCurrent.strName=dr["name"].ToString ();
this.bduCurrent.intParentID=Convert.ToInt16 (dr["parentID"]);
this.bduCurrent.strDescription =dr["description"].ToString ();
return;
}
}
}
this.bduCurrent.intID=-1;
}
private void InitBaseDataTable()
{
DataRow drNew=this.dtBDU .NewRow ();
drNew["sectionID"]="";
drNew["name"]="行政区域";
drNew["parentID"]=0;
drNew["description"]="";
this.dtBDU.Rows .Add (drNew);
drNew=this.dtBDU .NewRow ();
drNew["sectionID"]="";
drNew["name"]="试题类型";
drNew["parentID"]=0;
drNew["description"]="";
this.dtBDU.Rows .Add (drNew);
drNew=this.dtBDU .NewRow ();
drNew["sectionID"]="";
drNew["name"]="培训教材大纲";
drNew["parentID"]=0;
drNew["description"]="";
this.dtBDU.Rows .Add (drNew);
drNew=this.dtBDU .NewRow ();
drNew["sectionID"]="";
drNew["name"]="试题难度";
drNew["parentID"]=0;
drNew["description"]="";
this.dtBDU.Rows .Add (drNew);
new BaseDataManBLC ().UpdateDataSourceBLC(dsBDU);
//重新取得数据,并从新生成树
dsBDU=new BaseDataManBLC().GetAllBaseData ();
dtBDU=dsBDU.Tables [0];
dvBDU =dtBDU.DefaultView ;
RefreshTree();
this.trDataFunction .SelectedNode=this.trDataFunction .Nodes [0];
//清空三个文本框中的数据防止以前的数据干扰
this.txtSectionID.Text="";
this.txtName.Text ="";
this.txtDescription.Text ="";
//重新绑定三个文本框
this.txtSectionID.DataBindings .Clear();
this.txtName.DataBindings .Clear ();
this.txtDescription .DataBindings .Clear ();
this.txtSectionID.DataBindings .Add("Text",dvBDU,"sectionID");
this.txtName.DataBindings .Add ("Text",dvBDU,"name");
this.txtDescription .DataBindings .Add ("Text",dvBDU,"description");
}
private void SetDgTableStyle()
{//设置dgDisplay的显示格式,将英文格式转换成中文列名
DataGridTableStyle dgStyle = new DataGridTableStyle();
dgStyle.MappingName =this.dtBDU.TableName ;
DataGridColumnStyle SectionID=new DataGridTextBoxColumn();
SectionID.MappingName = "sectionID";
SectionID.HeaderText = "编号";
SectionID.Width = 40;
dgStyle.GridColumnStyles.Add(SectionID);
DataGridColumnStyle Name=new DataGridTextBoxColumn();
Name.MappingName = "name";
Name.HeaderText = "名称";
Name.Width =150;
dgStyle.GridColumnStyles.Add(Name);
DataGridColumnStyle ParentID=new DataGridTextBoxColumn();
ParentID.MappingName = "parentID";
ParentID.HeaderText = "父编号";
ParentID.Width =60;
dgStyle.GridColumnStyles.Add(ParentID);
DataGridColumnStyle Description=new DataGridTextBoxColumn();
Description.MappingName = "description";
Description.HeaderText = "描述";
Description.Width = 120;
dgStyle.GridColumnStyles.Add(Description);
this.dgDisplay.TableStyles.Add (dgStyle);
}
private void btnModify_Click(object sender, System.EventArgs e)
{
this.txtSectionID.ReadOnly=false;
this.txtName.ReadOnly =false;
this.txtDescription.ReadOnly =false;
this.btnSave.Enabled =true;
this.btnCancelOperation.Enabled=true;
}
private void FrmBaseDataMan_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
DataTable dtNew=dtBDU.GetChanges(DataRowState.Added);
DataTable dtDeleted=dtBDU.GetChanges(DataRowState.Deleted);
DataTable dtModified=dtBDU.GetChanges(DataRowState.Modified);
if(dtNew==null && dtDeleted==null && dtModified==null)
{
return;
}
string strMsg="信息没有保存!是否保存?";
string strCaption="提示";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result=MessageBox.Show(this, strMsg, strCaption, buttons,MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
if(result==DialogResult.Yes)
{//如果选择了"是",就保存;否则就不保存而直接退出系统
//如果数据视图没有可显示的记录的话,就不做处理,
//否则要结束当前行的编辑操作
//可以防止有的行仍然处于编辑状态还无法插入
if(this.dvBDU .Count !=0)
{
BindingManagerBase bm = this.BindingContext[this.dvBDU];
DataRowView drv;
drv = (DataRowView)bm.Current;
drv.EndEdit();
}
//更新数据源
new BaseDataManBLC ().UpdateDataSourceBLC(dsBDU);
}
}
private void trDataFunction_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
{//在这儿改变系统的焦点可以让编辑的行结束编辑
this.btnAddNode .Focus ();
//因为系统可能出现异常情况,所以我加了这一点,就是每一次选择完以后就让绑定的当前位置置为0
if(this.dgDisplay.VisibleRowCount>1)
{
BindingManagerBase bm;
bm = this.BindingContext[this.dvBDU ];
bm.Position =0;
}
}
private void lblExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void grpDescription_Enter(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -