Title: How to add rows to GridView in asp.net c# application
Tags: c#;asp.net;gridview;client-server;code-behind
Question: I've already searched many articles on this page and couldn not find the answer that would fit me.
Ok i have two projects (one is server(host) and one is client). The first one is ASP.NET Empty Web Application and the second one is ASP.NET Web Forms site.
In my host i have code for displaying users from my Entity FrameWork database. And in my client i have a form (atleast im trying to create one) where i should be able to display data (users).
Code for displaying data works fine because i've already used it before.
But somehow i cannot create a code for Gridview, which would put data from database into my cells
Lets say ID column would display user ID...Name column would display user name...
I've already set up service reference.
This is a code i've written so far, but it's giving me a few errors.
GridView1.DataSource = null;
GridView1.Refresh();
ServiceReference1.mojWebServiceSoapClient client = new ServiceReference1.mojWebServiceSoapClient();
var userList = client.getUsers();
foreach (var user in userList)
DataTable dt = new DataTable();
DataColumn column = new DataColumn();
if (dt.Columns.Count == 0)
{
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Last Name", typeof(string));
}
DataRow row = dt.NewRow();
row[0] = user.userID.ToString();
row[1] = user.Name;
row[2] = user.lName;
dt.Rows.Add(row);
GridView1.DataSource = dt;
GridView1.DataBind();
Can you guys please help me fix this? I would really appreciate any input.
Btw this code works, but it only displays 1 user, instead of all (5)

Here is the accepted answer: You repeat binding in a for loop and it is incorrect in your case. You should first create a DataTable and next in a for loop add users to it. So the following should satisfy your nedd:
GridView1.DataSource = null;
GridView1.Refresh();
ServiceReference1.mojWebServiceSoapClient client = new ServiceReference1.mojWebServiceSoapClient();
var userList = client.getUsers();
DataTable dt = new DataTable();
DataColumn column = new DataColumn();
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Last Name", typeof(string));
foreach (var user in userList)
DataRow row = dt.NewRow();
row[0] = user.userID.ToString();
row[1] = user.Name;
row[2] = user.lName;
dt.Rows.Add(row);
GridView1.DataSource = dt;
GridView1.DataBind();
Hope that helped
Comment for this answer: actually yes it works. thank you very much kind sir!


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:11/15/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><asp.net><gridview><client-server><code-behind>', 'Title': 'How to add rows to GridView in asp.net c# application', 'ViewCount': 2680, 'id': 26946755, 'user_AboutMe': '', 'user_AccountId': 4491267.0, 'user_DisplayName': 'aiden87', 'user_Reputation': 869.0, 'user_Views': 236.0, 'user_id': 3652303.0}}