在代码中使用DataAdapter和SqlCommand填充Devexpress GridView

本文关键字:SqlCommand 填充 Devexpress GridView DataAdapter 代码 | 更新日期: 2023-09-27 18:20:37

我在填充DevExpress Gridview时遇到了一个小问题,我想要一个两级网格视图并使用SqlCommand。首先,我创建了一个Dataset,添加了两个表,并为它们定义了关系。但它不起作用。你能帮我找到问题吗?

这是我的代码

string owner = "SELECT [OBJECTID],[Name] ,[Family] ,[Father] ,[Dftarche] ,[Birthday] ,[education] ,[home_address] ,[farm_address] ,[ensurance] ,[phone] ,[home_number] ,[owner_id]  FROM [dbo].[OWNER]";
string property = "SELECT [number] ,[owner_ID] ,[GPSId] ,[Energy],[corp_type] ,[Pool],[irrigation]   ,[variety] ,[trees] ,[utilizat] ,[address] ,[water_hour] ,[w_source] ,[w_inche],[w_dore],[NoeMalekiat],[MotevasetBardasht],[Area] ,[OBJECTID],[Shape]  FROM [dbo].[Property] ";
string strConnString = Properties.Settings.Default.land_gisConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
 SqlCommand command = new SqlCommand(owner, con);
              SqlDataAdapter adapter = new SqlDataAdapter();
              System.Data.DataSet dsMain = new System.Data.DataSet();
              adapter.SelectCommand = command;
              adapter.Fill(dsMain, "First Table");
              adapter.SelectCommand.CommandText = property;
              adapter.Fill(dsMain, "Second Table");

              dsMain.Tables.Add(iFeatureSet.DataTable.Copy());
              adapter.Dispose();
              command.Dispose();
              DataRelation newRelation = new DataRelation("املاک شخصی", dsMain.Tables["First Table"].Columns["owner_id"], dsMain.Tables["Second Table"].Columns["owner_ID"]);
              dsMain.Relations.Add(newRelation);
              GridAttrebuteTable.DataSource = dsMain.Tables[2];
            //  gridView5.DataSource = dsMain.Tables[1];
              dataGridView1.DataSource = dsMain;

我搜索发现了这个http://msdn.microsoft.com/en-us/library/bh8kx08z.aspx我的代码似乎是对的,但它在网格中没有显示任何内容

非常感谢您的帮助

我可以想办法修复它。它现在工作得很好(上面的代码已编辑),但现在如果我添加一个新的DataTable,我不知道为什么它不能再次工作

在代码中使用DataAdapter和SqlCommand填充Devexpress GridView

每个明细表都需要一个新的GridView。不能在同一个GridView中同时显示主控形状和细节。

试试这个例子