Iterate Rows with wrong Results

2 posts, 0 answers
  1. Amin
    Amin avatar
    1 posts
    Member since:
    Jan 2012

    Posted 19 Sep Link to this post

    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; }
        }
    }

  2. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 20 Sep Link to this post

    Hello Amin,

    Thank you for writing.

    The screenshot indicates that you have two templates created at the same level. Could you please make sure that you are indeed creating a single template. In the RowSourceNeeded event you would also need to work with the hierarchy row which can be accessed from the event arguments. Please check the following documentation article: http://docs.telerik.com/devtools/winforms/gridview/hierarchical-grid/load-on-demand-hierarchy.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top