我在treeview为什么只显示了数据库的第一条记录,其他为什么没有显示出来

felix61 2003-07-22 11:22:25
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{
private const string Stock_Image_Url = "images/mediastock.gif";
private const string Sort_Image_Url = "images/mediastock.gif";
private System.Windows.Forms.TreeView tvw;
private System.Windows.Forms.ImageList imageList1;
private System.ComponentModel.IContainer components;
public Form1()
{

InitializeComponent();
this.CreateTreeView();
}

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void CreateTreeView()
{
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\TaxLaw\\TaxLaw\\feitiumjoy.mdb";
tvw.Nodes.Clear() ;
string sql = "SELECT * FROM Type ";
OleDbConnection conn = new OleDbConnection() ;
conn.ConnectionString = connStr ;
OleDbDataAdapter oleDa = new OleDbDataAdapter(sql,connStr);
DataSet ds = new DataSet();
oleDa.Fill(ds,"type");
oleDa.SelectCommand.CommandText = "SELECT * FROM SmallClass";
oleDa.Fill(ds,"smallclass");
conn.Close();
DataColumn primaryKey = ds.Tables["SmallClass"].Columns["SmallClassID"];
ds.Tables["smallclass"].PrimaryKey = new DataColumn[]{primaryKey};
TreeNode node = GetRootNode(ds.Tables["type"],ds.Tables["smallclass"]);
if(node != null)
tvw.Nodes.Add(node);
node.Expand();
}
private TreeNode GetRootNode(DataTable dtType,DataTable dtSmallClass)
{
//调用函数前必须检查是否有记录
if(dtType.Rows.Count > 0)
{

TreeNode stockNode = new TreeNode();
stockNode.Text = dtType.Rows[0]["TypeName"].ToString();
stockNode.SelectedImageIndex = 1;
return stockNode;
}
else
{
return null;
}
}

}
}
...全文
22 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧