将RadGridView数据保存回数据库WPF?

时间:2017-08-21 17:37:59

标签: c# mysql wpf linq radgridview

我正在使用Telerik的Radgridview。我目前用测试数据库中的数据填充它,我在窗口加载时使用linq查询来执行此操作。我有两个额外的空列供用户输入数据,我想要的是在窗口关闭时将输入这些列的任何数据保存回数据库。这样,当我再次加载应用程序时,linq查询仍然有效。我不完全确定如何在Window_Closing事件中执行此操作或放入什么内容。我几乎搜索了每一篇关于这篇文章的网络文章和帖子,但没有一篇能帮我解决我的问题。具体来说,我正在尝试更新DeductionInfo表。这是我的Window_Loaded事件,我有我的linq查询加载数据:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        //Loads queries from each of the designated data tables in BSI_Test
        var customerQuery =
            (from customer in testEntity.Customers
             from deduction in testEntity.DeductionInfoes.DefaultIfEmpty()
             join job in testEntity.Jobs
             on customer.CID equals job.CID
             join claim in testEntity.Claims
             on job.JID equals claim.JID
             select new DataProperties
             {
                 Customer_Name = customer.CName,
                 Job_Name = job.JName,
                 Claim_No = claim.CLNumber,
                 Check_No = deduction.checkNo,
                 Check_Date = deduction.checkDate
             })
            .OrderBy(c => c.Customer_Name);

        //Populates the Telerik data grid with data.
        gridView.ItemsSource = customerQuery.ToList();
    }

这是我在Window_Closing事件中尝试的,似乎没有用,我想知道是否有人可以帮助我理解如何做到这一点:

DataTable ds = new DataTable();
public void Window_Closing(object sender, CancelEventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source = databaseserver; Database = database; User ID = sampleUsername; Password = SamplePassword; Integrated Security = False;");
        con.Open();
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM DeductionInfo", con);
        adapter.Fill(ds);
        DataTable dt = ds.Tables[0];
        gridView.ItemsSource = dt;
        con.Close();
    }    

顺便说一句,DeductionInfo表为空,因为这些数据将用于填充它。

0 个答案:

没有答案