C#Datagridview控件数据连接 [英] C# Datagridview control data connectivity

查看:48
本文介绍了C#Datagridview控件数据连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在学习C#,我想开发一个用于计算电气设备的程序.

I'm learning C# and I want to develop a program for calculation of electrical equipment.

程序基本上获取输入,将它们显示在GUI中,从数据库中获取一些图形,将输入和数据库中的值放入数学公式中,在GUI中显示计算结果,并将输入和结果保存在 一个文件.

The program basically gets inputs, displays them into a GUI, gets some figures from a data base, puts the inputs and the values from the database into mathematical formulae, displays the calculation results in the GUI and saves the inputs and results in a file.

对许多设备进行了计算.

The calculations are done for many pieces of equipment.

大多数输入数据应采用表格形式,最初是Excel文件.我进行了一些阅读,发现从.txt文件中读取数据会更加简单.因此,输入数据应以.txt文件制成表格.其他一些输入 由用户提供.

Most of the input data shall be in a tabular form, originally in an Excel file. I have done some reading and found that reading data from a .txt file would be much more simple. So, the input data shall be tabulated in a .txt file. Some other inputs shall be provided by user.

对于GUI,我选择使用Datagridview控件.

For the GUI I chose to use Datagridview control.

因此GUI应该:

1.将.txt文件中第一行的输入数据导入到Datagridview控件的第一行中.

1. Import the input data of the first row in the .txt file into the first row of the Datagridview control.

2.在行中分配空单元格以供进一步的用户输入并显示结果.

2. Allocate empty cells in the row for furthur user inputs and to show results.

3.继续执行前面的步骤,导入.txt文件中的所有条目.

3. Proceed to do the previous steps importing all the entries in the .txt file.

4.接受用户输入到第一行的输入单元格中.

4. Accept the user inputs into the input cells in the first row.

5.使用输入数据从数据库中获取一些数据.

5.Use the input data to get some data from the database.

6.在某些公式中使用所有输入和数据库数字.

6. Use all the input and database figures in some formulae.

7.在第一行的相应单元格中显示计算结果.

7. Display the calculation results in the corresponding cells in the first row.

8.对于其余的行,继续进行下一行的第4步到第7步.

8. Proceeding to the next row the steps 4 to 7 for the rest of rows.

9.将输入的结果和结果列表保存为.txt文件.

9. Save the inputs and results tabulated to a .txt file.

我知道我将对数据库查询使用ADO.NET实体.我不知道如何将GUI连接到程序,以及如何使其从.txt文件读取/写入数据.

I know that I shall use ADO.NET Entities for database querries. I don't know how to connect the GUI to the program and to make it read/write data from/to the .txt file.

有什么建议吗?

推荐答案

基本上,要使用Entity Framework,您首先要确定两种基本类型,即代码优先或模型优先.从这里开始阅读,这里我正在使用Microsoft NorthWind数据库.

Basically, to work with Entity Framework you first decide on the two basic flavors, code first or model first. From here a basic read, here I'm using Microsoft NorthWind database.

public void GetCustomers()
{
    using (NorthWindEntities context = new NorthWindEntities())
    {
        try
        {
            DataSource = context.Customers.Select(cust => cust)
                .Distinct()
                .OrderBy(cust => cust.CompanyName)
                .ToList();

        }
        catch (Exception ex)
        {
            /*
                * Of course for a production application we need to consider much better options
                */
            Console.WriteLine(ex.Message);
        }
    }
}

将行添加回数据库

public int NewIdentifier  { get; set; }
public bool AddNew(Customer NewCustomer)
{

    using (NorthWindEntities context = new NorthWindEntities())
    {
        try
        {                    
            context.Customers.Add(NewCustomer);
            context.SaveChanges();
            NewIdentifier = NewCustomer.CustomerIdentifier;
            ValidationMessage = "";

            return true;

        }
        catch (Exception)
        {
            ValidationMessage = "Failed adding new customer.";

            return false;

        }
    }
}

更新

public bool UpdateCustomer(Customer EditedCustomer)
{
    using (NorthWindEntities context = new NorthWindEntities())
    {
        try
        {
            context.Entry(EditedCustomer).State = EntityState.Modified;
            context.SaveChanges();
            ValidationMessage = "";
            return true;
        }
        catch (System.Data.Entity.Validation.DbEntityValidationException ef)
        {                   
            UndoPendingChanges(context);
            ValidationMessage = ef.DbEntityValidationExceptionToString();
            return false;
        }
        catch (Exception)
        {
            UndoPendingChanges(context);
            ValidationMessage = "Update failed";
            return false;
        }
    }
}

删除

public void Remove(Customer sender)
{
    using (NorthWindEntities context = new NorthWindEntities())
    {              
        context.Entry(sender).State = EntityState.Deleted;
        context.SaveChanges();
    }
}

若要使用文本文件,请查看.NET Framework中的System.File.IO命名空间,以获取用于读写数据的方法,以及使用字符串方法将每一行读取到具有每个项目属性的类中的方法.您可能会认为该文本文件是 以逗号分隔.因此,您可以从IO.File.ReadAllLines开始.

For working with a text file look at System.File.IO namespace in the .NET Framework for methods to read and write data along with using string methods to read each line into a class that has properties for each item. You might consider that the text file is comma delimited. So you might start off with IO.File.ReadAllLines to get started.


这篇关于C#Datagridview控件数据连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆