c# bind array results to gridview

I have connection to a database that is returning results to a webpage in asp.net (C#). How can I bind the results to a gridview? Is there a way to do this?
dodgerfanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
If the array is of an object type and the object / Class has public properties then those properties will show up as columns in the DataGrid. Although I have not tried it myself the following should work if the above is true.

DataGridInstance.DataSource = arrayInstance;
DataGridInstance.DataBind();
0
dodgerfanAuthor Commented:
The array is returning data from a database connection, specifically a mongodb database. The array is returning the specified fields, then it is displaying results with a foreach loop in html. I'd like to put the results into a gridview. Some of the relevant code is below. Sorry I should have added that when I posted the question.

var connectionString = "mongodbL//test";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database  = server.GetDatabase("mydb");
var collection = database.GetCollection<LazyBsonDocument>("entity_list");
var inserted = tbID.Text.ToString();
var q = QueryMatches("ID.data", "^" + inserted + ".*");
String[] StringNames = new String[]{"Comp", "FNAME", "LNAME", "MNAME", "EMPID" };

string html = "<table><tr>";

foreach (var stringName in StringNames)
{
html += "<th>" + stringName + "</th>";
}

html += "<tr>";
foreach (var doc in collection.Find(q))
{
html += "<tr>";
using (doc)
{
foreach (var stringName in StringNames)
{
html += "<td>";
if(doc.Contains(stringName))
{
var data = doc[stringName];
foreach (var subDoc in data.AsBsonArray)
{
html += subDoc["data"].AsString;
}
}
html += "</td>";
}
}
html += "</tr>"
}
html += "</table>";

Response.Write(html);
}
0
Fernando SotoRetiredCommented:
Hi dodgerfan;

The way the array is being created will not work with the DataGrid. What you will need to do is to create a class that contains all the columns being returned from the database as properties. Then add those object to a List<class> and that bind the List<class> to the DataGrid. So for example create a class something like this:
public class MyDataFromDB
{
    public string Comp { get; set; }
    public string FNAME {get; set; }
    public string LNAME { get; set; }
    public string MNAME { get; set; }
    public string EMPID { get; set; }
}

Open in new window


Then for each row of data you read from the database you fill one new instance of a MyDataFromDB and add that to a List<MyDataFromDB>.

When you have filled the List<MyDataFromDB> with all the data then you can bind it to the DataGrid like this
// Assuming that the List<MyDataFromDB> is called mdfDB and the DataGrid is called myDataGrid
myDataGrid.DataSource = mdfDB;
myDataGrid.DataBind();

Open in new window


Now I have NOT worked with Mongodb and so I do not know what type of collection is returned from this statement:
var collection = database.GetCollection<LazyBsonDocument>("entity_list");

Open in new window


But it may already be in the correct format for displaying in a DataGrid. So before doing the above I would try doing the following:
myDataGrid.DataSource = collection;
myDataGrid.DataBind();

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

dodgerfanAuthor Commented:
Thanks. The myDataGrid.DataSource = collection; did not work, though.

Is there an example for your statement here:
"Then for each row of data you read from the database you fill one new instance of a MyDataFromDB and add that to a List<MyDataFromDB>."

I'm not sure how to do that. I really have no experience with arrays and Lists.
0
Fernando SotoRetiredCommented:
Hi dodgerfan;

As I have stated I have not worked with mongoDB before and so there might be a better way to convert it to a list but this should do what you need.
List<MyDataFromDB> mdfDBList = new List<MyDataFromDB>();

foreach (var doc in collection.Find(q))
{
	MyDataFromDB mdfDB = new MyDataFromDB();
	using (doc)
	{
		foreach (var stringName in StringNames)
		{
			if(doc.Contains(stringName))
			{
				var data = doc[stringName];
				foreach (var subDoc in data.AsBsonArray)
				{
					string value = subDoc["data"].AsString;
					switch(stringName)
					{
						case "Comp":
						    mdfDB.Comp = value;
						    break;
						case "FNAME":
						    mdfDB.FNAME = value;
						    break;
						case "LNAME":
						    mdfDB.LNAME = value;
						    break;
						case "MNAME":
						    mdfDB.MNAME = value;
						    break;
						case "EMPID":
						    mdfDB.EMPID = value;
						    break;
					}
				}
			}
		}
	}
	mdfDBList.Add(mdfDB);
}

myDataGrid.DataSource = mdfDBList;
myDataGrid.DataBind();


public class MyDataFromDB
{
    public string Comp { get; set; }
    public string FNAME {get; set; }
    public string LNAME { get; set; }
    public string MNAME { get; set; }
    public string EMPID { get; set; }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dodgerfanAuthor Commented:
Yep, that did it perfectly. This is my first encounter with mongo, so I'm learning. I was also wondering how I can use the data returned. I'd like to select one of the returned records and insert the contents (lastname, firstname, etc) into a SQL Server DB. I also want to format the gridview, but how do I work with templates? Can ask as another question and point you to it? I wanted to accept this answer because you answered my original question perfectly. Thanks.
0
Fernando SotoRetiredCommented:
Hi dodgerfan;

I have done some but not a lot of programming in ASP.Net so you would be better off posting a new question. Once you have done that if you can post the link to the new question here and I will have a look to see if I can help on it.
0
dodgerfanAuthor Commented:
Fernando Soto:
Thanks for the help before. Here is the link to the next question:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28499330.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.