迭代行结果错误

时间:2016-09-19 10:35:09

标签: c# mongodb gridview telerik

我尝试在Master / Details模式下使用GridView从MongoDb Server获取数据。

如图所示,我从用户集合中获取Id和名称,然后我迭代父行以获取Id,并在第2个产品集合中获取以获取每个用户的详细信息。 / p>

在服务器中,第一个用户只有一个产品,但在图片中它显示3次,有2个表。

那么如何获得正确的结果以及如何使用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 UserWatchTblCls
{
    [BsonId]
    [BsonElement("_id")]
    public ObjectId Id { get; set; }
    [BsonElement("fbId")]
    public string fbId { get; set; }
    [BsonElement("Name")]
    public string Name { get; set; }
    [BsonElement("pass")]
    public string Pass { get; set; }
    [BsonElement("Watchtbl")]
    public List<WatchTblCls> WatchTbls { get; set; }
}

public class WatchTblCls
{
    [BsonElement("wid")]
    public string WID { get; set; }
    [BsonElement("name")]
    public string Name { get; set; }
    [BsonElement("Symboles")]
    public List<SymboleCls> Symbols { get; set; }
}

public class SymboleCls
{
    [BsonElement("Name")]
    public string Name { get; set; }
}
}

enter image description here

1 个答案:

答案 0 :(得分:1)

你可以通过绑定来实现。

private GridViewTemplate CreateChildTemplate()
{
    GridViewTemplate childTemplate = new GridViewTemplate();
    GridViewTextBoxColumn column = new GridViewTextBoxColumn("WID");
    childTemplate.Columns.Add(column);

    column = new GridViewTextBoxColumn("Name");
    childTemplate.Columns.Add(column);

    childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    return childTemplate;
}

private GridViewTemplate CreateGrandChildTemplate()
{
    GridViewTemplate grandChildTemplate = new GridViewTemplate();
    GridViewTextBoxColumn column = new GridViewTextBoxColumn("Name");
    grandChildTemplate .Columns.Add(column);

    grandChildTemplate .AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    return grandChildTemplate ;
}

private void RadForm1_Load(object sender, EventArgs e)
{
    var client = new MongoClient("mongodb://localhost:27017");

    var database = client.GetDatabase("test");
    var collectionWatchtbl = database.GetCollection<BsonDocument>("UserWatchtbl");

    var filter = new BsonDocument();

    var user = new List<UserWatchTblCls>();
    var cursor = collectionWatchtbl.FindAsync(filter).Result;
    cursor.ForEachAsync(batch =>
    {
        user.Add(BsonSerializer.Deserialize<UserWatchTblCls>(batch));
    });

    this.radGridView1.DataSource = user;
    this.radGridView1.Columns["fbId"].IsVisible = false;
    this.radGridView1.Columns["Pass"].IsVisible = false;
    GridViewTemplate childTemplate = CreateChildTemplate();
    childTemplate.DataSource = new BindingSource(user, "WatchTbls");
    GridViewTemplate grandChildTemplate = CreateGrandChildTemplate();
    grandChildTemplate.DataSource = new BindingSource(childTemplate.DataSource, "Symbols");
    this.radGridView1.Templates.Add(childTemplate);
    childTemplate.Templates.Add(grandChildTemplate);
    var gridViewRelation1 = new GridViewRelation(radGridView1.MasterTemplate, childTemplate);
    this.radGridView1.Relations.Add(gridViewRelation1);
    var gridViewRelation2 = new GridViewRelation(childTemplate, grandChildTemplate);
    this.radGridView1.Relations.Add(gridViewRelation2);
}

public class UserWatchTblCls
{
    [BsonId]
    [BsonElement("_id")]
    public ObjectId Id { get; set; }

    public string fbId { get; set; }
    public string Name { get; set; }

    [BsonElement("pass")]
    public string Pass { get; set; }

    [BsonElement("Watchtbl")]
    public List<WatchTblCls> WatchTbls { get; set; }
}

public class WatchTblCls
{
    [BsonElement("wid")]
    public string WID { get; set; }

    [BsonElement("name")]
    public string Name { get; set; }

    [BsonElement("Symboles")]
    public List<SymboleCls> Symbols { get; set; }
}

public class SymboleCls
{
    public string Name { get; set; }
}

这是一个更好的解决方案,而不是覆盖所需的行源。