?
Solved

c# bind array results to gridview

Posted on 2014-08-12
8
Medium Priority
?
495 Views
Last Modified: 2014-08-16
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?
0
Comment
Question by:dodgerfan
  • 4
  • 4
8 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40256841
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
 

Author Comment

by:dodgerfan
ID: 40256907
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40258259
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:dodgerfan
ID: 40258717
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
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40259018
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
 

Author Closing Comment

by:dodgerfan
ID: 40259200
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40259729
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
 

Author Comment

by:dodgerfan
ID: 40265741
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question