How to display a collection in a DataGridView?

1,685 views
Skip to first unread message

Guillaume FRANCHET

unread,
Feb 13, 2013, 6:54:52 AM2/13/13
to mongodb...@googlegroups.com
I tried to display an entire collection in a DataGridView but I don't know exactly how to do that because a DataGridVIew require Columns and Rows or a DataSource.
How to convert Json or Bson into a DataSource or Data to put in a DataGridView ?

Nguyễn Ngọc Anh

unread,
Mar 26, 2013, 11:12:50 PM3/26/13
to mongodb...@googlegroups.com
May be useful for U:   
// 1. Query in mongodb
 public DataTable get_data_chude()
        {
            List<ChuDe> lst = new List<ChuDe>();
            MongoDatabase mdMongoDatabase = ConnectToMongDB();
            MongoCollection<ChuDe> mcMongoCollection = mdMongoDatabase.GetCollection<ChuDe>(strTableChude);
            foreach (ChuDe objChuDe in mcMongoCollection.FindAll())
            {
                lst.Add(objChuDe);
            }
            return ConvertToDataTable(lst);
        }
 //2. convet list ra datatable
        public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
               TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;

        }

Goodluck!
Vào 18:54:52 UTC+7 Thứ tư, ngày 13 tháng hai năm 2013, Guillaume FRANCHET đã viết:

madhu_sudhan

unread,
May 15, 2013, 12:07:27 AM5/15/13
to mongodb...@googlegroups.com
following vb.net code snippet also helpful for you....
           
            Dim Docs = database.GetCollection("sample").FindAll().SetSortOrder(SortBy.Ascending("DT"))
            Docs.setLimit(1000) // limit to 1000 records

            Dim Tab As New DataTable
            Dim cnt As Integer = -2
            For Each Doc As BsonDocument In Docs  // loop through Documents
                cnt += 1
                Dim Dr As DataRow = Table.NewRow()
                For Each element As BsonElement In Doc.Elements   // loop through Elements
                    Dim ColName As String = element.Name
                    Dim ColValue As BsonValue = element.Value
                    If Not ColumnName = "_id" Then  // for removing default '_id' column
                        If cnt = -1 Then
                            Table.Columns.Add(ColumnName)
                        End If
                        Dr(ColumnName) = ColumnValue
                    End If
                Next
                Tab.Rows.Add(Dr)
            Next
            If Tab.Columns.Count() > 0 Then
                GridView1.DataSource = Table
                GridView1.DataBind()
            End If

Good luck..
Reply all
Reply to author
Forward
0 new messages