问题描述
datagridview显示父子表如DataGridViewdgvInfo=newDataGridView();stringstrConn="SERVER=127.0.0.1;DATABASE=NORTHWIND;UID=SA;PWD=sa";//数据库连接字符串SqlConnectionconn=newSqlConnection(strConn);conn.Open();stringstrSql="SELECT*FROMCUSTOMERS";SqlDataAdaptersda=newSqlDataAdapter(strSql,conn);sda.Fill(ds,"Customers");strSql="SELECT*FROMOrders";sda=newSqlDataAdapter(strSql,conn);sda.Fill(ds,"Orders");conn.Close();//dtInfo=ds.Tables[0];//InitDataSet();//CreateRelation();DataRelationcustomerOrdersRelation=ds.Relations.Add("Customers.CustOrders",ds.Tables["Customers"].Columns["CustomerID"],ds.Tables["Orders"].Columns["CustomerID"]);dgvInfo.DataSource=ds.Tables[0];dgvInfo.DataMember="Customers.CustOrders";怎么要报错,而不在DataGridView中显示父子表关系呢
解决方案
解决方案二:
ds.Relations.Add(newDataRelation("CustOrders",ds.Tables["Customers"].Columns["CustomerID"],ds.Tables["Orders"].Columns["CustomerID"]));dgvInfo.DataSource=ds;dgvInfo.DataMember=ds.Tables["Customers"].ToString();dgvDetail.DataSource=ds;dgvDetail.DataMember="Customers.CustOrders";
解决方案三:
只用一DataGridView中显示父子表关系呢
解决方案四:
DataSetmyds=newDataSet();SqlDataAdapterCustomerDa;stringconnectionstr="server=localhost;database=northwind;uid=sa;pwd=";SqlConnectionmyconn=newSqlConnection(connectionstr);myconn.Open();stringOrdersSql="selectCustomerID,ShipNamefromOrders";stringCustomersSql="selectCustomerID,CompanyName,CountryfromCustomers";SqlDataAdapterOrderDa=newSqlDataAdapter(OrdersSql,myconn);CustomerDa=newSqlDataAdapter(CustomersSql,myconn);OrderDa.Fill(myds,"Orders");CustomerDa.Fill(myds,"Customers");myds.Relations.Add("OrderCustomers",myds.Tables["Customers"].Columns["CustomerID"],myds.Tables["Orders"].Columns["CustomerID"]);DataGrid1.SetDataBinding(myds,"Customers");
解决方案五:
DataGridView显示不了父子表。可以使用DataGrid控件。
解决方案六:
可以使用DataGrid控件。