[DevExpress]GridControl主从表显示
程序员文章站
2024-03-21 08:54:40
...
今天接到用户的需求,物流分摊费用保存在销货单主表中,但物流人员需要看到从表的销货明细包含哪些产品和数量。主从表设计:
先看看效果吧:
private void GetDataList()
{
string sb = GetSQL();
try
{
DBCon db = new DBCon(this.company);
//销货单
DataSet MatserDs = SqlHelper.ExecuteDataset(db.StringConnection(), CommandType.Text, sb.ToString());
DataTable MatserDt = MatserDs.Tables[0];
MatserDt.TableName = "Matser";
if (MatserDt.Rows.Count > 0)
{
//销货明细
string subSQL = "Select TH003 序号,TH004 品号,TH005 品名,TH006 规格,TH008 数量,TH001+Rtrim(TH002) AS DetailID FROM COPTH ";
DataSet DetailDs = SqlHelper.ExecuteDataset(db.StringConnection(), CommandType.Text, subSQL.ToString());
DataTable DetailDt = DetailDs.Tables[0];
DetailDt.TableName = "Detail";
DetailDs.Tables.Remove(DetailDt);
MatserDs.Tables.Add(DetailDt);
DataColumn keyColumn = MatserDs.Tables["Matser"].Columns["MasterID"]; //主键
DataColumn foreignColumn = MatserDs.Tables["Detail"].Columns["DetailID"]; //外键
//
//对于主从表,层次名至关重要,关系名必须和从表的层次名一致,
//否则从表显示的是从表的所有字段,而不是所设计的显示字段
//
MatserDs.Relations.Add("Detail", keyColumn, foreignColumn,false); //从表的层次名
GridControl1.DataSource = MatserDs.Tables["Matser"];
btnFirst.Enabled = true;
btnLast.Enabled = true;
btnNetxt.Enabled = true;
btnPre.Enabled = true;
btnPreview.Enabled = true;
}
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.ToString(), "警告");
}
txtScan.Text = "";
}
上一篇: mysql导出表结构
下一篇: mysql修改表结构