TreeView在显示大容量数据库数据时的使用
由于在利用treeView在显示大容量数据库数据时,使用递归算法非常费时,要求用户长时间的等待。那么如何在使用treeView显示大容量数据库数据时改善用户体验呢?
笔者使用用户自主选择的方法很好的解决了这一问题。
具体方法如下:
private void btnOpenData_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//获取数据源
datafilename = openFileDialog1.FileName;
string constring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + datafilename;
this.lbltishi.ForeColor = Color.Red;
this.lbltishi.Text = "正在生成栏目资源目录……";
this.Refresh();
ds.Clear();
//ds1.Clear();
OleDbConnection con = new OleDbConnection();
try
{
con.ConnectionString = constring;
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select * from tchannel order by channel_parentid";
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
}
catch (Exception ex)
{
throw (ex);
}
finally
{
con.Close();
}
//生成一级树目录
treeView1.Nodes.Clear();
TreeNode tn = new TreeNode("所有栏目资源");
this.treeView1.Nodes.Add(tn);
addtree("0", tn);
}
}
private void addtree(string ParentID, TreeNode pNode)
{
//使用DataView提高用户体验,并在所有的同级子目录下都增加“请双击搜索子资源(XXXXX)”的下一级子目录,以便用户通过双击展开。
DataView dvtree = new DataView(ds.Tables[0]);
dvtree.RowFilter = "channel_parentid='" + ParentID + "'";
foreach (DataRowView row in dvtree)
{
TreeNode node = pNode.Nodes.Add(row["channel_name"].ToString());
node.Nodes.Add("请双击搜索子资源(" + row["channel_id"].ToString() + ")");
}
}
private void treeView1_NodeMouseDoubleClick_1(object sender, TreeNodeMouseClickEventArgs e)
{
//从所双击的节点得到TREEVIEW
TreeView td = (TreeView)sender;
//得到当前的节点
TreeNode tn = td.SelectedNode;
//得到当前节点的父节点
TreeNode parenttn = tn.Parent;
//从“请双击搜索子资源(channel_id)"中得到channel_id值,以便根据这个channel_id值在数据库中查找channel_parentid为此值的所有下一节点;
int start = tn.Text.LastIndexOf("(");
int end = tn.Text.LastIndexOf(")");
try
{
string channelparentid = tn.Text.Substring(start + 1, end - start - 1);
//如果找到值说明是双击的是“请双击搜索子资源(channel_id)”这一节点。
//对这个节点应删除,以便添加新的子节点
tn.Remove();
addtree(channelparentid, parenttn);
}
//否则就是一个已搜索出的有效的节点,对已搜索出的有效的节点,得到相应的数据资源路径,并在数据表中查询操作。
catch (ArgumentOutOfRangeException ee)
{
//得到当前节点的数据资源路径,并去掉0层上的“所有栏目资源\”,从而得到真正的数据资源路径。
try
{
this.lblCurdata.Text = tn.FullPath.Substring(7);
string channelid = this.lblCurdata.Text;
bindgrid(channelid,typestring,this.dateTimePicker1.Value ,this.dateTimePicker2.Value); //查询数据操作
}
catch (ArgumentOutOfRangeException ef)
{
this.lblCurdata.Text = "所有栏目";
bindgrid("-", typestring, this.dateTimePicker1.Value, this.dateTimePicker2.Value);
}
}
}
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//获取数据源
datafilename = openFileDialog1.FileName;
string constring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + datafilename;
this.lbltishi.ForeColor = Color.Red;
this.lbltishi.Text = "正在生成栏目资源目录……";
this.Refresh();
ds.Clear();
//ds1.Clear();
OleDbConnection con = new OleDbConnection();
try
{
con.ConnectionString = constring;
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select * from tchannel order by channel_parentid";
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
}
catch (Exception ex)
{
throw (ex);
}
finally
{
con.Close();
}
//生成一级树目录
treeView1.Nodes.Clear();
TreeNode tn = new TreeNode("所有栏目资源");
this.treeView1.Nodes.Add(tn);
addtree("0", tn);
}
}
private void addtree(string ParentID, TreeNode pNode)
{
//使用DataView提高用户体验,并在所有的同级子目录下都增加“请双击搜索子资源(XXXXX)”的下一级子目录,以便用户通过双击展开。
DataView dvtree = new DataView(ds.Tables[0]);
dvtree.RowFilter = "channel_parentid='" + ParentID + "'";
foreach (DataRowView row in dvtree)
{
TreeNode node = pNode.Nodes.Add(row["channel_name"].ToString());
node.Nodes.Add("请双击搜索子资源(" + row["channel_id"].ToString() + ")");
}
}
private void treeView1_NodeMouseDoubleClick_1(object sender, TreeNodeMouseClickEventArgs e)
{
//从所双击的节点得到TREEVIEW
TreeView td = (TreeView)sender;
//得到当前的节点
TreeNode tn = td.SelectedNode;
//得到当前节点的父节点
TreeNode parenttn = tn.Parent;
//从“请双击搜索子资源(channel_id)"中得到channel_id值,以便根据这个channel_id值在数据库中查找channel_parentid为此值的所有下一节点;
int start = tn.Text.LastIndexOf("(");
int end = tn.Text.LastIndexOf(")");
try
{
string channelparentid = tn.Text.Substring(start + 1, end - start - 1);
//如果找到值说明是双击的是“请双击搜索子资源(channel_id)”这一节点。
//对这个节点应删除,以便添加新的子节点
tn.Remove();
addtree(channelparentid, parenttn);
}
//否则就是一个已搜索出的有效的节点,对已搜索出的有效的节点,得到相应的数据资源路径,并在数据表中查询操作。
catch (ArgumentOutOfRangeException ee)
{
//得到当前节点的数据资源路径,并去掉0层上的“所有栏目资源\”,从而得到真正的数据资源路径。
try
{
this.lblCurdata.Text = tn.FullPath.Substring(7);
string channelid = this.lblCurdata.Text;
bindgrid(channelid,typestring,this.dateTimePicker1.Value ,this.dateTimePicker2.Value); //查询数据操作
}
catch (ArgumentOutOfRangeException ef)
{
this.lblCurdata.Text = "所有栏目";
bindgrid("-", typestring, this.dateTimePicker1.Value, this.dateTimePicker2.Value);
}
}
}