(三)
XtraGrid的功能与GridView的功能相似,使用方法也类似,不过他的功能更加令人惊讶,看看下面的效果
Code//生成此效果的解决方案:
// 1.从数据库去出两张表,
//后台
SqlConnManager.cs
public Class SqlConnManager
{
string _strConn = string.Empty;
SqlConnection _conn ;
public SqlConnManager(string strConn)
{
_strConn = strConn;
if (_conn == null)
_conn = GetSqlConn;
}
public System.Data.SqlClient.SqlConnection GetSqlConn
{
get
{
if (_conn == null||_conn.ConnectionString.Length==0)
_conn = new SqlConnection(strConn);
if (_conn.State == ConnectionState.Closed)
{
try
{
_conn.Open();
}
catch
{
return null;
}
}
return _conn;
}
set
{
_conn.Close();
}
}
}
TestXtraGrid.cs
SqlConnManager dbManager = new SqlConnManager(System.Configuration.ConfigurationManager.AppSettings["ConnenctionString"].ToString());
DataSet ds = new DataSet();
using(SqlDataAdapter adapter= new SqlDataAdapter ("select parent from Master",dbManager.GetSqlConn))
{
adapter.Fill(ds,"master");
}
//相同的方法过去子表的集合
//定义两个表之间的关系
System.Data.DataRelation relation = new System.Data.DataRelation("master-detail",ds.Tables[0].Columns["Master"],ds.Tables[1].Columns["Child"]);
ds.Relations.Add(relation);
//最后将此数据集绑定到XtraGrid