XtraGrid 主/从关系使用赋值源码
private void Form1_Load(object sender, EventArgs e)
{
try
{
string sqlStr = "Data Source=.;Initial Catalog=Northwind;" + "Integrated Security=SSPI";
SqlConnection myConnection = new SqlConnection(sqlStr);
String SelectCmdString1 = "select * from Orders";
string SelectCmdString2 = "select*from OrderDetails";
SqlDataAdapter mySqlDataAdapter1 = new SqlDataAdapter(SelectCmdString1, myConnection);
SqlDataAdapter mySqlDataAdapter2 = new SqlDataAdapter(SelectCmdString2, myConnection);
DataSet dataSet1 = new DataSet();
mySqlDataAdapter1.Fill(dataSet1, "Orders");
DataTable ordersDt = dataSet1.Tables[0];
if (ordersDt.Rows.Count > 0)
{
mySqlDataAdapter2.Fill(dataSet1, "OrderDetails");
DataColumn keyColumn = dataSet1.Tables["Orders"].Columns["Id"];
DataColumn foreignColumn = dataSet1.Tables["OrderDetails"].Columns["fId"];
dataSet1.Relations.Add("详细信息", keyColumn, foreignColumn);
this.gridControl1.DataSource = dataSet1.Tables["Orders"];
}
}
catch (Exception ex)
{
throw ex;
}
}
数据库表 Orders
数据库表 OrderDetails
运行时刻设计
字表名称必须与代码中指定的字表名称一致