简体   繁体   English

在C#中使用实体框架在datagridview中显示数据库中的数据

[英]display data from database in datagridview with entity framework in c#

I'm using Empty Web Application as my host and i have different classes, such as users in it. 我使用Empty Web Application作为主机,并且具有不同的类,例如其中的用户。

I also have a client (Windows Forms app). 我也有一个客户端(Windows Forms应用程序)。 Please notice this is Entity framework database! 请注意,这是实体框架数据库!

Ok here's the thing. 好的,这就是事情。 I want to display each and every single data in datagridview (seperate columns for ID, Name, etc...) 我想在datagridview中显示每个数据(ID,名称等的单独列...)

In my Empty Web app, i have Webservice where i have the code for displaying users. 在我的Empty Web应用程序中,我具有Webservice,其中包含用于显示用户的代码。

Here is the code 这是代码

[WebMethod]
    public List<string> getUsers()
    {
        List<string> userList = new List<string>();
        using (var db = new Database())
        {
            var query = from x in db.userList
                            orderby x.UserID
                            select x;

            foreach (var user in query)
            {
                userList.Add(user.userID + user.Name); //also some other information, but doesnt really matter here
            }
        }
        return userList;
    }

Ok, off to Windows Forms App (client). 好的,转到Windows Forms App(客户端)。

Now i need to display this data in datagridview (in seperate columns), meaning: 现在,我需要在datagridview中显示此数据(在单独的列中),这意味着:

userID gets displayed in ID colument Name gets displayed in Name column 在ID列中显示userID在名称列中显示名称

Here is where problem occurres. 这是发生问题的地方。 All the data gets displayed in each column. 所有数据都显示在每一列中。 USERID + NAME GETS DISPLAYED IN ID COLUMN USERID +名称栏显示在ID列中

My code 我的密码

ServiceReference1.mojWebServiceSoapClient client = new ServiceReference1.mojWebServiceSoapClient();

        var userL = client.pridobiUporabnike();

        foreach (string user in userL)
        {
            string[] row = new string[6];
            row[0] = user;
            row[1] = user;
            row[2] = user;
            row[3] = user;
            row[4] = user;
            row[5] = user;               
            dataGridView1.Rows.Add(row);
        }

You're going to say "ofcourse it displays same data, when u have "user" everywhere. I know...i tried many things, but somehow i just can't make it work. 您会说“当然,当您到处都有“用户”时,它会显示相同的数据。我知道...我尝试了很多事情,但是以某种方式我无法使其正常工作。

I think code should look something like row[0] = user.UserID; 我认为代码应类似于row [0] = user.UserID; row 1 = user.Name; 1行= user.Name;

Can you please guys help me fix this, i would really appreaciate it 你们能帮我解决这个问题吗,我会很感激

Thanks a lot to any1 who gives input. 非常感谢提供输入的任何人。

Im also including the results of running program. 我还包括了运行程序的结果。

在此处输入图片说明

You are returning a List, which can contain only one single value per user, in your case you are assigning the id and name fields to that property. 您将返回一个列表,每个用户只能包含一个值,在这种情况下,您正在为该属性分配id和name字段。

You should rather contain a separate class that you will fill with values based upon your requirements and then return a list or collection of that class. 您应该包含一个单独的类,该类将根据您的要求填充值,然后返回该类的列表或集合。

[DataContract]
public class Userdata
{
    public Userdata(int id, string name, string surname, string number, string mobile, string email)
    {
        this.Id = id;
        this.Name = name;
        this.Surname = surname;
        this.Number = number;
        this.Mobile = mobile;
        this.Email = email;
    }

    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; } 

    [DataMember]      
    public string Surname { get; set; }

    [DataMember]
    public string Number { get; set; }

    [DataMember]
    public string Mobile{ get; set; } 

    [DataMember]      
    public string Email{ get; set; }
}

public List<Userdata> getUsers()
    {
        var userList = new List<Userdata>();
        using (var db = new Database())
        {
            var query = from x in db.userList
                        orderby x.UserID
                        select x;

            foreach (var user in query)
            {
                userList.Add(new Userdata(user.id, user.name, user.surname, user.number, user.mobile, user.email));
            }
        }
        return userList;
    }

I think this small example should get you going in the right direction. 我认为这个小例子可以使您朝正确的方向前进。

I saw that you are using a web service, you should then decorate your dto (data transfer object) class with some attributes. 我看到您正在使用Web服务,然后应该使用一些属性装饰dto(数据传输对象)类。 Please see the Attributes on the UserData class. 请参阅UserData类上的属性。

Finally for your client side code, while there are many ways of setting up a data grid, I will stick with what you currently have. 最后,对于您的客户端代码,尽管有很多方法可以设置数据网格,但我会坚持使用您目前拥有的方法。 You should after the changes be able to setup the grid as follows. 更改后,您应该能够如下设置网格。

var userL = client.pridobiUporabnike();

    foreach (string user in userL)
    {
        string[] row = new string[2];
        row[0] = user.Id;
        row[1] = user.Name;              
        dataGridView1.Rows.Add(row);
    }

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

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