感谢您使用微软产品。您可以在DataSet中,通过DataRelation建立customers和orders表之间的联系(根据表的具体情况,选择两个特定的字段),然后将此DataRelation增加到DataSet中。这样,在DataGrid的DataSource属性指定为DataSet中的主DataTable,如customers。DataGrid可以根据建立的DataRelation自动地显示其主从关系。 下面,提供一段示例代码,其中DataSet中的主表是dtCategories,从表是dtProducts。并根据CategoryId建立两个DataTable之间的DataRelation(drCategoryId)。最后在DataGrid中显示其主从表。 private void button1_Click(object sender, System.EventArgs e) { DataSet myDataSet = new DataSet(); myDataSet = getCategoryProducts(); myDataGrid.DataSource = myDataSet.Tables["dtCategories"].DefaultView; } private DataSet getCategoryProducts() { string connString = "Server=SHA-RICKIE-01;Database=northwind;uid=rickie;pwd=rickie"; SqlConnection myConn; DataSet myDataSet = new DataSet(); myConn = new SqlConnection(connString); myConn.Open(); string sqlString = "Select CategoryId, CategoryName, Description From Categories"; SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(sqlString, myConn); // Fill the DataSet with the data table named 'dtCategories' mySqlDataAdapter.Fill(myDataSet, "dtCategories");
// Now get ready to extract the product data string sqlString2 = " Select ProductId, ProductName, UnitPrice, UnitsInStock, CategoryId From Products"; SqlDataAdapter mySqlDataAdapter2 = new SqlDataAdapter(sqlString2, myConn); // Fill the DataSet with the data table named 'dtProducts mySqlDataAdapter2.Fill(myDataSet, "dtProducts"); // Establish the 1:Many relationship between categories and products // Name the relationship 'CategoryProducts'. DataColumn parentCol,childCol; DataRelation drCategoryId; parentCol = myDataSet.Tables["dtCategories"].Columns["CategoryId"]; childCol = myDataSet.Tables["dtProducts"].Columns["CategoryId"]; drCategoryId = new DataRelation("CategoryProducts",parentCol,childCol); //Add the relation to the DataSet. myDataSet.Relations.Add(drCategoryId); return myDataSet; }关于DataRelation的更详细信息及示例,请参考微软官方网站: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnavigatingrelationshipbetweentwotables.asp — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。 为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
下面,提供一段示例代码,其中DataSet中的主表是dtCategories,从表是dtProducts。并根据CategoryId建立两个DataTable之间的DataRelation(drCategoryId)。最后在DataGrid中显示其主从表。
private void button1_Click(object sender, System.EventArgs e)
{
DataSet myDataSet = new DataSet();
myDataSet = getCategoryProducts();
myDataGrid.DataSource = myDataSet.Tables["dtCategories"].DefaultView;
} private DataSet getCategoryProducts()
{
string connString = "Server=SHA-RICKIE-01;Database=northwind;uid=rickie;pwd=rickie";
SqlConnection myConn;
DataSet myDataSet = new DataSet();
myConn = new SqlConnection(connString);
myConn.Open();
string sqlString = "Select CategoryId, CategoryName, Description From Categories";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(sqlString, myConn);
// Fill the DataSet with the data table named 'dtCategories'
mySqlDataAdapter.Fill(myDataSet, "dtCategories");
// Now get ready to extract the product data
string sqlString2 = " Select ProductId, ProductName, UnitPrice, UnitsInStock, CategoryId From Products";
SqlDataAdapter mySqlDataAdapter2 = new SqlDataAdapter(sqlString2, myConn);
// Fill the DataSet with the data table named 'dtProducts
mySqlDataAdapter2.Fill(myDataSet, "dtProducts");
// Establish the 1:Many relationship between categories and products
// Name the relationship 'CategoryProducts'.
DataColumn parentCol,childCol;
DataRelation drCategoryId;
parentCol = myDataSet.Tables["dtCategories"].Columns["CategoryId"];
childCol = myDataSet.Tables["dtProducts"].Columns["CategoryId"];
drCategoryId = new DataRelation("CategoryProducts",parentCol,childCol);
//Add the relation to the DataSet.
myDataSet.Relations.Add(drCategoryId); return myDataSet;
}关于DataRelation的更详细信息及示例,请参考微软官方网站:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnavigatingrelationshipbetweentwotables.asp
— 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。