I'm using Empty Web Application as my host and i have different classes, such as users in it.
I also have a client (Windows Forms app). 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...)
In my Empty Web app, i have Webservice where i have the code for displaying users.
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).
Now i need to display this data in datagridview (in seperate columns), meaning:
userID gets displayed in ID colument Name gets displayed in Name column
Here is where problem occurres. All the data gets displayed in each column. USERID + NAME GETS DISPLAYED IN ID COLUMN
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; row1 = 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.
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. Please see the Attributes on the UserData class.
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);
}