📄 frmproductdetails.cs
字号:
try{
frmProduct frmPrd = new frmProduct();
frmPrd.MdiParent = this.ParentForm;
frmPrd.Show();
}
catch(Exception excep)
{
MessageBox.Show ("发生错误 "+excep.Message.ToString ());
}
}
private void btnUpdate_Click(object sender, System.EventArgs e)
{
try
{
if (updatedFlag) //if update has taken place in the datagrid
{
//retrieve data from the datagrid
DataSet ds = new DataSet();
DataTable dtProd = (DataTable)dbgDispProd.DataSource;
DataRow drPrd = dtProd.NewRow ();
foreach(DataRow drow in dtProd.Rows)
{
drPrd = drow;
}
//update the table by calling the ModifyProduct method
StockManagement stkMgmt = new StockManagement();
stkMgmt.ModifyProduct(drPrd);
dbgDispProd.Refresh ();
MessageBox.Show("产品相信信息已更新");
}
else
{
DataTable dtProd = (DataTable)dbgDispProd.DataSource;
if (dtProd.Rows.Count <= 0)
MessageBox.Show("没有详细信息用以更新");
else
MessageBox.Show("您没有更新任何的详细信息");
}
}
catch(Exception excep)
{
MessageBox.Show ("发生错误 "+excep.Message.ToString ());
}
}
private void btnExit_Click(object sender, System.EventArgs e)
{
frmMainMenu.varPrdMgmt = 0;
this.Close();
}
private void tvwCatCode_Click(object sender, System.EventArgs e)
{
//check if we haven't added new category recently but just clicked the
// treeview
if (this.addFlag)
this.populateTree();
}
public void populateTree()
{
try{
addFlag = false; // for the next add operation
//if tree exists, delete it and create fresh new tree with latest records
if (this.tvwCatCode.GetNodeCount(true) > 0)
{
this.tvwCatCode.SelectedNode = this.tvwCatCode.TopNode;
this.tvwCatCode.Nodes.Remove(this.tvwCatCode.SelectedNode);
}
//retrieve data for the treeview from the table
DataConnection.commnd.CommandText = "Select catcode,flvlcatcode from productcategories order by flvlcatcode";
OleDbDataReader reader = DataConnection.commnd.ExecuteReader();
if (reader.HasRows)
{
//create root node for the tree view
TreeNode node = this.tvwCatCode.Nodes.Add("产品类别");
TreeNode newNode = new TreeNode ();
string current,parent;
while(reader.Read ())
{
current = reader.GetString(0);
parent = reader.GetString(1);
//if this node has no parent category, add this node to the root node
if(parent.Equals ("00"))
{
newNode = new TreeNode (current);
node.Nodes.Add(newNode);
}
else //if this node does have a parent category, find its parent and then add this node to that parent node
{
//TreeNode tnode = this.tvwCatCode.TopNode;
foreach(TreeNode nd in node.Nodes)
{
if (nd.Text.Equals(parent))
{
newNode = new TreeNode (current);
nd.Nodes.Add (current);
}
foreach(TreeNode nd2 in nd.Nodes)
{
if (nd2.Text.Equals (parent))
{
newNode = new TreeNode (current);
nd2.Nodes.Add (current);
}
}
}
}
}//end while
treePopulated = true;
}
if (treePopulated)
this.tvwCatCode.ExpandAll();
reader.Close();
}
catch(Exception excep)
{
//MessageBox.Show ("发生错误 "+excep.Message.ToString ());
MessageBox.Show ("发生错误 "+excep.StackTrace.ToString ());
}
}
private void dbgDispProd_CurrentCellChanged(object sender, System.EventArgs e)
{
try
{
DataTable dtProd = (DataTable)dbgDispProd.DataSource;
if (dbgDispProd.CurrentRowIndex < dtProd.Rows.Count)
{
dtProd.ColumnChanging += new DataColumnChangeEventHandler(ValidateColumn);
DataRow drPrd = dtProd.Rows[dbgDispProd.CurrentRowIndex];
}
}
catch(Exception excep)
{
MessageBox.Show ("发生错误 "+excep.Message.ToString ());
}
}
private void ValidateColumn(object sender, DataColumnChangeEventArgs evt)
{
try
{
updatedFlag=true;
if (evt.Column.ColumnName.Equals("产品标识"))
{
//Do not allow this column to be changed
string origValue = (string)(evt.Row[evt.Column]);
object badValue = evt.ProposedValue;
//evt.Row.RowError = "Product Id column cannot be changed";
evt.Row.SetColumnError(evt.Column, "产品标识不能修改。");
evt.Row[evt.Column]= origValue;
}
if (evt.Column.ColumnName.Equals("产品名称"))
{
//Do not allow "" as a product name
if (evt.ProposedValue.Equals(""))
{
object badValue = evt.ProposedValue;
evt.Row.RowError = "产品名称列包含一个错误";
evt.Row.SetColumnError(evt.Column, "请输入有效的产品名称值。 它不能是 " + badValue);
}
}
if (evt.Column.ColumnName.Equals("现有数量"))
{
//negative values are invalid
if (int.Parse(evt.ProposedValue.ToString()) < 0)
{
object badValue = evt.ProposedValue;
evt.Row.RowError = "现有数量列包含一个错误";
evt.Row.SetColumnError(evt.Column, "现有数量不能是 " + badValue);
}
}
if (evt.Column.ColumnName.Equals("产品型号"))
{
//negative values are invalid
if (int.Parse(evt.ProposedValue.ToString()) < 0)
{
object badValue = evt.ProposedValue;
evt.Row.RowError = "产品型号列包含一个错误";
evt.Row.SetColumnError(evt.Column, "产品型号不能是 " + badValue);
}
}
}
catch(Exception excep)
{
MessageBox.Show ("发生错误 "+excep.Message.ToString ());
}
}
private void frmProductDetails_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
frmMainMenu.varPrdMgmt = 0;
}
private void tvwCatCode_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
try
{
this.catCode = tvwCatCode.SelectedNode.Text;
if (!this.catCode.Equals ("产品类别"))
{
str = "Select * from products where catcode='"+this. catCode+"'";
OleDbDataAdapter adapter=new OleDbDataAdapter(str,DataConnection.oleconn);
DataSet dset=new DataSet();
adapter.Fill (dset);
this.dbgDispProd.DataSource=dset.Tables[0];
}
}
catch(Exception excep)
{
MessageBox.Show ("发生错误 "+excep.Message.ToString ());
}
}
public void populateProdId()
{
//if items already exist in the combo box clear them and
//then re-populate populate product id combo box
if (!reader.IsClosed)
reader.Close();
if (this.cboProdId.Items.Count >0)
this.cboProdId.Items.Clear ();
DataConnection.commnd.CommandText = "select productid from products";
reader = DataConnection.commnd.ExecuteReader();
while (reader.Read())
{
cboProdId.Items.Add (reader.GetString(0));
}
reader.Close(); //close the connection
}
private void cboProdId_DropDown(object sender, System.EventArgs e)
{
populateProdId();
}
private void txtPrdName_MouseHover(object sender, System.EventArgs e)
{
this.tipProductDetails.SetToolTip(this.txtPrdName,"输入要搜索的产品名称...(可选)");
}
private void cboCatCode_MouseHover(object sender, System.EventArgs e)
{
this.tipProductDetails.SetToolTip(this.cboCatCode,"选择要搜索的已有产品的类别代码...(可选)");
}
private void cboProdId_MouseHover(object sender, System.EventArgs e)
{
this.tipProductDetails.SetToolTip(this.cboProdId,"选择要搜索的产品标识...(可选)");
}
private void tvwCatCode_MouseHover(object sender, System.EventArgs e)
{
this.tipProductDetails.SetToolTip(this.tvwCatCode,"产品类别树");
}
private void dbgDispProd_MouseHover(object sender, System.EventArgs e)
{
this.tipProductDetails.SetToolTip(this.dbgDispProd,"产品详细信息。如要更新,请更改后点击“更新”");
}
private void btnAdd_MouseHover(object sender, System.EventArgs e)
{
this.tipProductDetails.SetToolTip(this.btnAdd,"添加新产品");
}
private void btnUpdate_MouseHover(object sender, System.EventArgs e)
{
this.tipProductDetails.SetToolTip(this.btnUpdate,"更新记录");
}
private void frmProductDetails_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
try
{
if (flag) //check if records have been added using New button
{
searchValue[0]="";
searchValue[1]="";
searchValue[2]="";
StockManagement stkMgmt = new StockManagement();
//fetch the product details
//refresh the datagrid
DataTable prodDetails = stkMgmt.FetchProdDetails(searchValue);
if (prodDetails.Rows.Count >0)
{
this.dbgDispProd.DataSource = prodDetails;
this.dbgDispProd.Refresh();
}
}
//populate category treeview
this.populateTree();
this.tvwCatCode.Sorted = true;
//next, populate the catcode combo box
//but first check if it already has items and //delete those items so that
//we can add afresh
if (this.cboCatCode.Items.Count >0 )
this.cboCatCode.Items.Clear();
DataConnection.commnd.CommandText = "select distinct catcode from products";
reader = DataConnection.commnd.ExecuteReader();
while (reader.Read())
{
cboCatCode.Items.Add(reader.GetString(0));
}
reader.Close();
//populate product id combo box
populateProdId();
}
catch(Exception excep)
{
MessageBox.Show ("发生错误 "+excep.Message.ToString ());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -