1.數據初始劃
private void Initialize_Data()
{
SqlString = "select distinct(substring(cast(outstore.`Date` as char),1,7)) as dDate from outstore "
+ " Inner Join storehouse ON storehouse.OutStoreKey = OutStore.outstoreKey "
+ " where storehouse.StoreHouseName='" + cmbBoxStoreName.Text + "'";
dataSet1 = DBHelper.ExecuteDataSet(SqlString, "outstore");
foreach (DataRow row in dataSet1.Tables["outstore"].Rows)
{
treeView1.Nodes.Add("-3", row["dDate"].ToString().Trim());
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Nodes.Count == 0)
{
if (e.Node.Name == "-3")
{
dataSet1.Clear();
if (ofrm.UserRight != "Admin")
{
SqlString = "select distinct(substring(cast(Outstore.`Date` as char),1,10)) as dDate from outstore "
+ " Inner Join storehouse ON storehouse.OutStoreKey = OutStore.outstoreKey "
+ " where outstore.Date like '" + e.Node.Text + "%' and storehouse.StoreHouseName='" + cmbBoxStoreName.Text + "'"
+ " order by outstore.`Date` ASC;";
}
else
{
SqlString = "select distinct(substring(cast(Outstore.`Date` as char),1,10)) as dDate from outstore "
+ " where outstore.Date like '" + e.Node.Text + "%'"
+ " order by outstore.`Date` ASC;";
}
dataSet1 = DBHelper.ExecuteDataSet(SqlString, "outstore");
for (int i = 0; i < dataSet1.Tables["outstore"].Rows.Count; i++)
{
DataRow row = dataSet1.Tables["outstore"].Rows[i];
e.Node.Nodes.Add("-2", row["dDate"].ToString());
}
}
else if (e.Node.Name == "-2")
{
dataSet1.Clear();
if (ofrm.UserRight != "Admin")
{
SqlString = "select Outstore.OutstoreKey,Outstore.OutstoreID,outstore.flag from outstore "
+ " Inner Join storehouse ON storehouse.OutStoreKey = OutStore.outstoreKey "
+ " where outstore.`Date`='" + e.Node.Text + "' and storehouse.ProductKey=-1 "
+ " and storehouse.StoreHouseName='" + cmbBoxStoreName.Text + "'"
+ " order by outstore.outstoreID ASC;";
}
else
{
SqlString = "select Outstore.OutstoreKey,Outstore.OutstoreID,outstore.flag from outstore "
+ "where outstore.`Date`='" + e.Node.Text + "'"
+ "order by outstore.outstoreID ASC;";
}
dataSet1 = DBHelper.ExecuteDataSet(SqlString, "outstore");
for (int i = 0; i < dataSet1.Tables["outstore"].Rows.Count; i++)
{
DataRow row = dataSet1.Tables["outstore"].Rows[i];
TreeNode no = new TreeNode();
no.Text = row["OutstoreID"].ToString();
no.Name = row["OutstoreKey"].ToString();
if ((row["flag"].ToString() == true.ToString())||(row["flag"].ToString() =="1"))
{
no.ForeColor = Color.Red;
no.Text = '*' + row["OutstoreID"].ToString();
}
e.Node.Nodes.Add(no);
}
}
else if (e.Node.Name.Length > 0)
{
Load_Data(e.Node.Name);
}
}
}
修改或新增數據後按保存,更新treeview
方法一:
private void Update_Treeview()
{
string cName="";
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
dataSet1.Clear();
if (ofrm.UserRight == "Admin")
{
SqlString = "select distinct(substring(cast(outstore.`Date` as char),1,7)) as dDate from outstore;";
}
else
{
SqlString = "select distinct(substring(cast(outstore.`Date` as char),1,7)) as dDate from outstore "
+ " Inner Join storehouse ON storehouse.OutStoreKey = OutStore.outstoreKey "
+ " where storehouse.StoreHouseName='" + cmbBoxStoreName.Text + "'";
}
dataSet1 = DBHelper.ExecuteDataSet(SqlString, "Outstore");
foreach (DataRow row in dataSet1.Tables["outstore"].Rows)
{
treeView1.Nodes.Add("-3", row["dDate"].ToString().Trim());
}
foreach (TreeNode nd in treeView1.Nodes)
{
dataSet1.Clear();
if (ofrm.UserRight == "Admin")
{
SqlString = "select distinct(substring(cast(outstore.`Date` as char),1,10)) as dDate from outstore "
+ " where outstore.Date like '" + nd.Text + "%'"
+ " order by outstore.`Date` ASC;";
}
else
{
SqlString = "select distinct(substring(cast(outstore.`Date` as char),1,10)) as dDate from outstore "
+ " Inner Join storehouse ON storehouse.OutStoreKey = OutStore.outstoreKey "
+ " where outstore.Date like '" + nd.Text + "%' and storehouse.StoreHouseName='" + cmbBoxStoreName.Text + "'"
+ " order by outstore.`Date` ASC;";
}
dataSet1 = DBHelper.ExecuteDataSet(SqlString, "outstore");
foreach (DataRow row in dataSet1.Tables["outstore"].Rows)
{
TreeNode node = new TreeNode();
node.Text = row["dDate"].ToString();
node.Name = "-2";
DataSet ds = new DataSet();
if (ofrm.UserRight == "Admin")
{
SqlString = "select outstore.outstoreKey,outstore.outstoreID,outstore.flag from outstore "
+ " where outstore.`Date` ='" + node.Text + "'"
+ " order by outstore.outstoreID ASC;";
}
else
{
SqlString = "select outstore.outstoreKey,outstore.outstoreID,outstore.flag from outstore "
+ " Inner Join storehouse ON storehouse.OutStoreKey = OutStore.outstoreKey "
+ " where outstore.`Date` ='" + node.Text + "' and storehouse.ProductKey=-1 "
+ " and storehouse.StoreHouseName='" + cmbBoxStoreName.Text + "'"
+ " order by outstore.outstoreID ASC;";
}
ds = DBHelper.ExecuteDataSet(SqlString, "outstore");
foreach (DataRow ro in ds.Tables["outstore"].Rows)
{
TreeNode no = new TreeNode();
no.Name = ro["outstoreKey"].ToString();
no.Text = ro["outstoreID"].ToString();
if ((ro["flag"].ToString() == true.ToString())||ro["flag"].ToString() =="1")
{
no.ForeColor = Color.Red;
no.Text = '*' + ro["outstoreID"].ToString();
}
node.Nodes.Add(no);
if (no.Name == cOutStoreKey)
{
cName = no.Parent.Text;
no.Parent.ExpandAll();
}
}
nd.Nodes.Add(node);
if (cName == node.Text)
{
node.Parent.Expand();
}
}
}
treeView1.EndUpdate();
}
方法貳:
private void Update_Treeview()
{
treeView1.BeginUpdate();
if (mflag == myFlag.EditFlag)
{
DataSet ds = new DataSet();
SqlString = " SELECT products.ProductKey,products.ProductName,products.Specification,products.KindName,products.ProductID,products.Flag + " FROM products "
+ " where products.ProductKey=" + cProductKey
+ " order by products.ProductID ASC,products.ProductName ASC,products.Specification +1,SUBSTRING(products.Specification,1) DESC;";
ds = DBHelper.ExecuteDataSet(SqlString, "products");
if (ds.Tables["products"].Rows.Count > 0)
{
DataRow row = ds.Tables["products"].Rows[0];
TreeNode no = FindTreeNode(treeView1.Nodes[0], cProductKey);
if (no != null)
{
no.BackColor = Color.SkyBlue;
no.Name = row["ProductKey"].ToString();
no.Text = row["ProductName"].ToString() + "-{" + row["Specification"].ToString() + "}";
if (row["flag"].ToString() == true.ToString() || row["flag"].ToString() == "1")
{
no.BackColor = Color.White;
no.Text =row["ProductName"].ToString() + "-{" + row["Specification"].ToString() + "}";
}
ExpandTree(no);
treeView1.SelectedNode = no;
no.Checked = true;
}
}
}
treeView1.EndUpdate();
}
private TreeNode FindTreeNode(TreeNode objTreeNode, string strNodeValue)
{
if (objTreeNode == null) return null;
if (objTreeNode.Name == strNodeValue) { return objTreeNode; }
else if (objTreeNode.Text == strNodeValue) { return objTreeNode; }
TreeNode ndResult = null;
foreach (TreeNode tn in objTreeNode.Nodes)
{
ndResult= FindTreeNode(tn, strNodeValue);
if (ndResult != null) return ndResult;
}
return ndResult ;
}
private void ExpandTree(TreeNode objTreeNode)
{
objTreeNode.Expand();
while (objTreeNode.Parent is TreeNode)
{
objTreeNode = ((TreeNode)objTreeNode.Parent);
objTreeNode.Expand();
}
}