📄 bomform.cs
字号:
this.Load += new System.EventHandler(this.BomForm_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
//-------------创建窗体时,显示树形图的BOM表------------
private void BomForm_Load(object sender, System.EventArgs e)
{
this.ShowTree();//将BOM表用树形图的方式显示出来
}
//-------------读入数据并准备显示树形图-----------------
private void ShowTree()
{
tblBom.Clear();
this.sqlDataAdapter1.Fill(tblBom);//读入数据
//DataTable tempDataTable = tblBom.Copy();//将组织机构表另存一份为tempDataTable
DataView viewBom = new DataView(tblBom);//新建一个数据视图
viewBom.RowFilter="低层码=0";
//将数据集中的所有记录逐个根据他们之间的关系添加到树形表中去
if (viewBom.Count > 0)
{
int i=0;//由于低层码为0的产品可能不止一个,因此设置序号,以便准确定位父项
foreach (DataRowView myRow in viewBom)
{
string strName = myRow["物料编号"].ToString().Trim()+SetNodeName(myRow["物料编号"].ToString().Trim());
//此处是添加前面头两个节点
this.treeView1.Nodes.Add(new TreeNode(strName));
//此处初始化参数是第一个节点,然后该函数会递归添加所有子节点
PopulateTreeView(strName,treeView1.Nodes[i],myRow);
i++;
treeView1.SelectedNode = treeView1.Nodes[0]; //选中第一个节点
}
}
}
//---------根据输入的节点信息,递归调用最终添加所有的节点-----------------------
private void PopulateTreeView(string parentPart,TreeNode parentNode,DataRowView parentRow)
{
string strName = "";
DataTable tempDataTable = tblBom.Copy();
DataView viewBom = new DataView(tempDataTable);
//筛选获得当前传递过来的节点的子项,并将其添加到树形图中
//判断方法是:凡父项编号等于传递过来的节点的物料编号的,就是该节点的子项
viewBom.RowFilter = "父项编号 = '" + parentRow["物料编号"].ToString().Trim() + "'";
//递归的添加每一个节点的所有子节点
foreach (DataRowView myRow in viewBom)
{
strName = myRow["物料编号"].ToString().Trim()+SetNodeName(myRow["物料编号"].ToString().Trim());
TreeNode myNode = new TreeNode(strName);
parentNode.Nodes.Add(myNode);
//函数递归调用,将所有节点按顺序添加完毕
PopulateTreeView(strName,myNode,myRow);
}
}
//---------根据物料编号,查找其名称----------
private string SetNodeName(string materialID)
{
string strConn = "server=localhost;Integrated Security=SSPI;database=mrpbook";
SqlConnection cn=new SqlConnection(strConn);
cn.Open();
SqlCommand cmd=cn.CreateCommand();
cmd.CommandText="select 物料名称 from 物料主文件 where 物料编号='"+materialID+"'";
return(cmd.ExecuteScalar().ToString().Trim());
}
//-----------将所选节点中的物料名称和编号分别提取出来-------------
private string[] GetNodeInfo(string inputStr)
{
int i;
string str1,str2;
str1="";
str2="";
int length=inputStr.Length;
char[] chars=inputStr.ToCharArray(0,length);
for(i=0;i<length;i++)
{
char ch=chars[i];
if(ch>='0'&&ch<='9')
str1+=ch.ToString();
else
str2+=ch.ToString();
}
string[] resultStr = {str1,str2};
return(resultStr);
}
//-----------------在树形图中选择一个节点,显示其详细信息----------------
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
string[] thisNodeInfo=GetNodeInfo(e.Node.Text);//显示当前节点信息
txt1.Text=thisNodeInfo[0];
txt2.Text=thisNodeInfo[1];
if(e.Node.Parent!=null)//显示主节点信息
{
string[] parentNodeInfo=GetNodeInfo(e.Node.Parent.Text);
txt3.Text=parentNodeInfo[0];
txt4.Text=parentNodeInfo[1];
}
else
{
txt3.Text="0";//顶层两个节点父项编号是0
txt4.Text="顶级节点无父节点";
}
string strConn = "server=localhost;Integrated Security=SSPI;database=mrpbook";
SqlConnection cn=new SqlConnection(strConn);
cn.Open();
SqlCommand cmd=cn.CreateCommand();
cmd.CommandText="select 需要数量,低层码,领料车间,领料库房,损耗率,审核日期,其它事项 from 物料清单 where (物料编号='"+txt1.Text+"')and(父项编号='"+txt3.Text+"')";
SqlDataReader dr=cmd.ExecuteReader();
dr.Read();
txt5.Text=dr.GetValue(0).ToString().Trim();//保存需要数量
txt6.Text=dr.GetValue(1).ToString().Trim();//保存低层码
txt7.Text=dr.GetValue(2).ToString().Trim();//保存领料车间
txt8.Text=dr.GetValue(3).ToString().Trim();//保存损耗率
txt9.Text=dr.GetValue(4).ToString().Trim();//保存审核日期
txt10.Text=dr.GetValue(5).ToString().Trim();//保存其它事项
}
//---------------设置工具栏操作--------------
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(treeView1.SelectedNode==null)//若未选中任何节点,则不做任何动作
{
return;
}
string strConn = "server=localhost;Integrated Security=SSPI;database=mrpbook";
SqlConnection cn=new SqlConnection(strConn);
cn.Open();
SqlCommand cmd=cn.CreateCommand();
if(e.Button.ToolTipText=="新增")//增加选中节点的子项
{
SetModifyMode(true);//允许选择文本框进行编辑
selBtn1.Enabled=true;
txt3.Text=txt1.Text;//当前节点成为新节点的父节点
txt4.Text=txt2.Text;
txt1.Clear();//清除原有数据并设置默认值
txt2.Clear();
txt5.Text="0";//默认需要数量为0
int newOrder=Convert.ToInt32(txt6.Text.Trim())+1;
txt6.Text=newOrder.ToString();
txt7.Clear();
txt8.Clear();
txt9.Clear();
txt10.Clear();
curStatus=1;//标识现在是新增状态
}
if(e.Button.ToolTipText=="修改")
{
SetModifyMode(true);
txt1.ReadOnly=true;//修改时不允许修改物料编号
selBtn1.Enabled=false;
curStatus=2;//表示现在是编辑状态
}
if(e.Button.ToolTipText=="删除")
{
if(treeView1.SelectedNode.LastNode!=null)
MessageBox.Show("请先删除该节点的所有子节点","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
else
{
DialogResult result=MessageBox.Show("确认删除?","删除数据",MessageBoxButtons.OKCancel);
if(result==DialogResult.OK)
{
cmd.CommandText="delete from 物料清单 where (物料编号='"+txt1.Text+"')and(父项编号='"+txt3.Text+"')";
cmd.ExecuteNonQuery();
treeView1.Nodes.Clear();
ShowTree();//重新绘制树形图
}
}
}
if(e.Button.ToolTipText=="提交")
{
if(curStatus==0)//非新增,编辑状态,不作操作
return;
if(curStatus==1)//新增状态
{
cmd.CommandText="insert into 物料清单([父项编号], [物料编号], [需要数量], [领料车间],"+
"[领料库房], [审核日期], [其它事项],[低层码]) "+
"values('"+txt3.Text+"','"+txt1.Text+"','"+txt5.Text+"','"+txt7.Text+"','"+
txt8.Text+"','"+txt9.Text+"','"+txt10.Text+"','"+txt6.Text+"')";
cmd.ExecuteNonQuery();
curStatus=0;//恢复普通状态
SetModifyMode(false);
treeView1.Nodes.Clear();
ShowTree();//重新绘制树形图
}
if(curStatus==2)//编辑状态
{
cmd.CommandText="update 物料清单 set 需要数量='"+txt5.Text+"',领料车间='"+txt7.Text
+"',领料库房='"+txt8.Text+"',审核日期='"+txt9.Text+"',其它事项='"+txt10.Text+"'"
+" where (物料编号='"+txt1.Text+"')and(父项编号='"+txt3.Text+"')";
cmd.ExecuteNonQuery();
curStatus=0;//恢复普通状态
SetModifyMode(false);
treeView1.Nodes.Clear();
ShowTree();//重新绘制树形图
}
}
if (e.Button.ToolTipText == "取消")
{
treeView1.SelectedNode=treeView1.Nodes[0];//默认选中首项
SetModifyMode(false);
}
if(e.Button.ToolTipText=="退出")
{
this.Close();
}
}
//--------------对控件的ReadOnly属性做设置---------------
private void SetModifyMode(bool blnModify)
{
//设置文本框
txt1.ReadOnly=!blnModify;
txt5.ReadOnly=!blnModify;
txt7.ReadOnly=!blnModify;
txt8.ReadOnly=!blnModify;
txt9.ReadOnly=!blnModify;
txt10.ReadOnly=!blnModify;
//允许使用选择按钮
selBtn1.Enabled=blnModify;
}
//------------调出窗体,选择物料--------------
private void selBtn1_Click(object sender, System.EventArgs e)
{
SelectMaterial.callForm=3;
SelectMaterial selFrm=new SelectMaterial();
selFrm.ShowDialog();
SendKeys.Send("{Tab}");//向活动应用程序发送Tab键,跳到下一控件
}
//---------将选择得到的物料编号和名称填入文本框----------
private void selBtn1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
txt1.Text=mID;
txt2.Text=mName;
SendKeys.Send("{Tab}");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -