简体   繁体   English

使用C#将行添加到WPF中的DataGrid中并在其中显示MSAccess数据

[英]Adding row to datagrid in wpf using c# and display msaccess data in it

I am new to WPF and C# programming. 我是WPF和C#编程的新手。 I have created a form with some text boxes around 15. I have created a datagrid by the name of dgSearch and added 15 columns to it in xaml. 我创建了一个带有约15个文本框的表单。我创建了一个名为dgSearch的数据网格,并在xaml中添加了15列。 Now the problem is how to add a new row to the dgSearch datagrid and display the data which is read from msaccess table in that datagrid. 现在的问题是如何在dgSearch数据网格中添加新行,并显示从该数据网格中的msaccess表读取的数据。 I am using WPF and C# with Visual Studio 2012. Kindly help me plzzz???? 我在Visual Studio 2012中使用WPF和C#。请帮助我吗?

There are many different ways to get the data from the DB: 有许多不同的方法可以从数据库获取数据:

  1. Quick and Dirty. 快速而肮脏。 Just use the OdbcConnection and OdbcCommand and query for what you want. 只需使用OdbcConnection和OdbcCommand并查询所需的内容。 There are many examples out there just google them. 有很多例子在那里只是谷歌他们。

  2. Use LinqToSql and create objects based on your database. 使用LinqToSql并根据您的数据库创建对象。

Then to display the data in the grid it would depend if your using MVVM or not. 然后,要在网格中显示数据,将取决于您是否使用MVVM。

  1. Put the data in a DataSet and then bind it to the grid and it will automatically display and create your columns. 将数据放入数据集,然后将其绑定到网格,它将自动显示并创建您的列。

  2. Put the data in a collection like List, or ObservableCollection and bind that to your datagrid. 将数据放入List或ObservableCollection之类的集合中,并将其绑定到您的datagrid。

  3. Put it data in a any collection and then link to your control and insert, or use code to set the ItemsSource. 将其数据放入任何集合中,然后链接到控件并插入,或使用代码设置ItemsSource。

There are dozens of ways to do it, but you will need to define what approach you wish to take. 有很多方法可以实现,但是您将需要定义您希望采用的方法。 I would strongly recommend you read about Data Binding in WPF, and perhaps Model-View-ViewModel before you decide. 我强烈建议您在决定之前先阅读一下WPF中的数据绑定,以及Model-View-ViewModel。 There are probably many examples that do exactly what you want. 可能有许多示例可以完全满足您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM