C#绑定TreeList的DataSource属性及获取DataRow数据
C#绑定TreeList的DataSource属性及获取DataRow数据
TreeList数据源表结构定义, 定义isid及ParentID.
显示数据
显示选中结点对应的DataTable中DataRow数据.
数据访问,存取Access数据库
Source code:
TreeList数据源表结构定义, 定义isid及ParentID.
显示数据
private void btnLoad_Click(object sender, EventArgs e)
{
DataTable dt = DataProvider.Instance.GetTable("select * from [tb_department]", "tb_department");
treeList1.DataSource = dt;
}
{
DataTable dt = DataProvider.Instance.GetTable("select * from [tb_department]", "tb_department");
treeList1.DataSource = dt;
}
显示选中结点对应的DataTable中DataRow数据.
private void btnShowValue_Click(object sender, EventArgs e)
{
if (treeList1.FocusedNode == null) return;
object row = treeList1.GetDataRecordByNode(treeList1.FocusedNode);
DataRow dr = (row as DataRowView).Row;
MessageBox.Show(dr["TagValue"].ToString());
}
{
if (treeList1.FocusedNode == null) return;
object row = treeList1.GetDataRecordByNode(treeList1.FocusedNode);
DataRow dr = (row as DataRowView).Row;
MessageBox.Show(dr["TagValue"].ToString());
}
数据访问,存取Access数据库
/// <summary>
/// 数据提供者.
/// </summary>
public class DataProvider
{
private static DataProvider _Instance = null;
/// <summary>
/// 数据提供者对象实例,因程序频繁请求数据,减少创建对象的次数。
/// </summary>
public static DataProvider Instance
{
get
{
if (_Instance == null)
{
_Instance = new DataProvider();
}
return _Instance;
}
}
/// <summary>
/// 提供数据表
/// </summary>
/// <param name="sql">SQL 查询语句</param>
/// <param name="tableName">数据表名</param>
/// <returns></returns>
public DataTable GetTable(string sql, string tableName)
{
OleDbConnection conn = this.CreateConnection();
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable(tableName);
adp.Fill(dt);
conn.Close();
conn.Dispose();
return dt;
}
/// <summary>
/// 创建一个OleDbConnection连接.
/// </summary>
/// <returns></returns>
private System.Data.OleDb.OleDbConnection CreateConnection()
{
string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb";
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State != ConnectionState.Connecting) conn.Open();
return conn;
}
}
/// 数据提供者.
/// </summary>
public class DataProvider
{
private static DataProvider _Instance = null;
/// <summary>
/// 数据提供者对象实例,因程序频繁请求数据,减少创建对象的次数。
/// </summary>
public static DataProvider Instance
{
get
{
if (_Instance == null)
{
_Instance = new DataProvider();
}
return _Instance;
}
}
/// <summary>
/// 提供数据表
/// </summary>
/// <param name="sql">SQL 查询语句</param>
/// <param name="tableName">数据表名</param>
/// <returns></returns>
public DataTable GetTable(string sql, string tableName)
{
OleDbConnection conn = this.CreateConnection();
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable(tableName);
adp.Fill(dt);
conn.Close();
conn.Dispose();
return dt;
}
/// <summary>
/// 创建一个OleDbConnection连接.
/// </summary>
/// <returns></returns>
private System.Data.OleDb.OleDbConnection CreateConnection()
{
string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb";
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State != ConnectionState.Connecting) conn.Open();
return conn;
}
}
Source code:
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网