I tried to use GridView in Master/Details Mode to get data from MongoDb Server.
And as the pic shows, I get the Id and the name from a Collection of Users, then I Iterate the parent Rows to get the Id and I fetch in the 2nd Colection of Product to get the details of each user.
In the server, the 1st user have only 1 product but in the pic it show it 3 times with 2 tables.
So how to get the right results and how work with Master/Detail GridView
namespace
TelerikGridView
{
public
partial
class
Form2 : Form
{
List<WatchTblCls> wts;
List<UserCls> user;
List<SymboleCls> symb;
public
Form2()
{
InitializeComponent();
wts =
new
List<WatchTblCls>();
user =
new
List<UserCls>();
symb =
new
List<SymboleCls>();
}
private
async
void
button1_Click(
object
sender, EventArgs e)
{
// add user into datagridview from MongoDB Colelction Watchtbl
var client =
new
MongoClient(
"mongodb://servername:27017"
);
var database = client.GetDatabase(
"WatchTblDB"
);
var collectionWatchtbl = database.GetCollection<BsonDocument>(
"Watchtbl"
);
var collectionUser = database.GetCollection<BsonDocument>(
"Users"
);
//wts = await collectionWatchtbl.Find(x => true).ToListAsync();
//Get User Data
var filter =
new
BsonDocument();
using
(var cursor = await collectionUser.FindAsync(filter))
{
while
(await cursor.MoveNextAsync())
{
var batch = cursor.Current;
foreach
(var document
in
batch)
{
user.Add(
new
UserCls()
{
Id = ObjectId.Parse(document[
"_id"
].ToString()),
Name = document[
"Name"
].ToString()
});
}
}
}
//Get WatchTbl Data
using
(var cursor = await collectionWatchtbl.FindAsync(filter))
{
while
(await cursor.MoveNextAsync())
{
var batch = cursor.Current;
foreach
(var document
in
batch)
{
wts.Add(
new
WatchTblCls()
{
Id = ObjectId.Parse(document[
"_id"
].ToString()),
UserId = document[
"userId"
].ToString(),
WID = document[
"wid"
].ToString(),
Name = document[
"name"
].ToString()
//Symbole
});
}
}
}
this
.radGridView1.DataSource = user;
this
.radGridView1.Columns[
"fbId"
].IsVisible =
false
;
this
.radGridView1.Columns[
"Pass"
].IsVisible =
false
;
}
GridViewTemplate childTemplate;
private
GridViewTemplate CreateChildTemplate()
{
childTemplate =
new
GridViewTemplate();
this
.radGridView1.Templates.Add(childTemplate);
GridViewTextBoxColumn column =
new
GridViewTextBoxColumn(
"wid"
);
childTemplate.Columns.Add(column);
column =
new
GridViewTextBoxColumn(
"name"
);
childTemplate.Columns.Add(column);
childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
return
childTemplate;
}
private
void
Form2_Load(
object
sender, EventArgs e)
{
GridViewTemplate childTemplate = CreateChildTemplate();
this
.radGridView1.Templates.Add(childTemplate);
childTemplate.HierarchyDataProvider =
new
GridViewEventDataProvider(childTemplate);
}
private
void
radGridView1_RowSourceNeeded(
object
sender, GridViewRowSourceNeededEventArgs e)
{
foreach
(GridViewRowInfo item
in
radGridView1.Rows)
{
var itll = item.Cells[
"id"
].Value.ToString();
foreach
(var itemWts
in
wts)
{
if
(itll == itemWts.UserId.ToString())
{
GridViewRowInfo row = e.Template.Rows.NewRow();
row.Cells[
"wid"
].Value = itemWts.WID.ToString();
row.Cells[
"name"
].Value = itemWts.Name.ToString();
//symbole
e.SourceCollection.Add(row);
}
}
}
}
}
public
class
WatchTblCls
{
[BsonId]
public
ObjectId Id {
get
;
set
; }
public
string
UserId {
get
;
set
; }
public
string
WID {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
List<SymboleCls> Symbols {
get
;
set
; }
}
public
class
UserCls
{
[BsonId]
public
ObjectId Id {
get
;
set
; }
public
string
fbId {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
string
Pass {
get
;
set
; }
}
public
class
SymboleCls
{
[BsonId]
public
ObjectId Id {
get
;
set
; }
public
string
Name {
get
;
set
; }
}
}