刷新数据源主从XtraGrid

时间:2018-06-30 10:02:15

标签: c# winforms devexpress gridcontrol

我有这段代码可以在运行时创建Master-Detail XtraGrid

i = 0
i = 1 FAIL
i = 2 FAIL
i = 3 FAIL

我想单击按钮刷新DataSource,所以我添加了这段代码

    public partial class FRM_Reserved : DevExpress.XtraEditors.XtraForm
{
    DataTable Table1 = new DataTable("Table1");
    DataTable Table2 = new DataTable("Table2");
    DataSet dataSet = new DataSet();

    public FRM_Reserved()
    {
        InitializeComponent();

        Table1 = ord.Get_Orders();
        Table2 = ord.Get_Order_Res();
        dataSet.Tables.Add(Table1);
        dataSet.Tables.Add(Table2);

        dataSet.Relations.Add("OrderDetails",
        dataSet.Tables["Table1"].Columns["Bon N"],
        dataSet.Tables["Table2"].Columns["Bon N"]);

        gridControl3.DataSource = dataSet.Tables["Table1"];

    }

但是代码不起作用,请您帮我。

3 个答案:

答案 0 :(得分:0)

尝试以下代码。希望它能起作用

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.mystyle {
    font-weight: bold;
}
</style>
</head>
<body>

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
text to bold.
</div>

<script>
function myFunction() {
   var element = document.getElementById("myDIV");
   element.classList.toggle("mystyle");
}
</script>

</body>
</html>

答案 1 :(得分:0)

使用以下代码

gridControl3.DataSource = dataSet.Tables["Table1"];
gridControl3.RefreshDataSource();

尝试这个

private void button1_Click(object sender, EventArgs e)
{

    var ds = new DataSet();
    var dt1 = GetTable1Data();
    var dt2 = GetTable2Data();

    ds.Tables.Add(dt1);
    ds.Tables.Add(dt2);

    ds.Relations.Add("RelationTable",
       ds.Tables["Table1"].Columns["col1"],
       ds.Tables["Table2"].Columns["col1"]);

    gridControl1.DataSource = ds.Tables["Table1"];
    gridControl1.RefreshDataSource();
}

private DataTable GetTable1Data()
{
    using (var conn = new SqlConnection("Conneciton string goes here"))
    {
        conn.Open();
        using (var cmd = new SqlCommand("Stored procedure name goes here", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            var dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            return dt;
        }
    }
}

private DataTable GetTable2Data()
{
    using (var conn = new SqlConnection("Conneciton string goes here"))
    {
        conn.Open();
        const string sql = "SELECT * FROM Table2";
        using (var cmd = new SqlCommand(sql, conn))
        {
            var dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            return dt;
        }
    }
}

答案 2 :(得分:0)

问题出在每次更改的表的名称上

dataSet.Tables[(Table1.TableName)]
dataSet.Tables[(Table2.TableName)]

所以我做了这个改变

        private void simpleButton1_Click(object sender, EventArgs e)
    {

        dataSet.Relations.Clear();
        dataSet.Tables["Table2"].Clear();
        dataSet.Tables["Table1"].Clear();


        Table1 = ord.Get_Orders();
        Table2 = ord.Get_Order_Res();
        dataSet.Tables.Add(Table1);
        dataSet.Tables.Add(Table2);

        dataSet.Relations.Add("OrderDetails",
        dataSet.Tables[(Table1.TableName)].Columns["Bon N"],
        dataSet.Tables[(Table2.TableName)].Columns["Bon N"]);
        gridControl3.DataSource = dataSet.Tables[(Table1.TableName)];
        gridControl3.ForceInitialize();
    }

感谢您的帮助