Multiple tables but one GridView

8 posts, 2 answers
  1. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 06 Mar 2011 Link to this post

    Afternoon,

    I apologise in advanced if this isn't strictly Telerik ground but as it's regarding using two of the controls I felt it would be better answered here.

    I have just started a new small application with has a TreeView and A GridView. What I am trying to achieve is that when one of the nodes is selected it displays the appropriate table from my database into the GridView.

    I know it must be simple but I cannot seem to resolve getting the table adapter to fill with another table.

    Any help would be really appreciated!

    Regards,

    Guy
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 06 Mar 2011 Link to this post

    Hello Guy,

    I don't know the semantics of your code, but you should ensure that the ClearBeforeFill property of the tableadapter is set to true.
    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 06 Mar 2011 Link to this post

    Hi Richard,

    Whilst that is helpful to know, i'm actually a stage or two before that. What I am unable to work out is after an event has occured how do I switch out one table with another in the same GridView?

    Regards,

    Guy
  5. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 06 Mar 2011 Link to this post

    Hello Guy,

    you said that you are using a RadTreeView. In this case you would pick up on the SelectedNodeChanged event and Fill your table adapter, then (depending on how you are binding) call the ResetBindings on your BindingSource or set the data source for the Grid to null, and back again.

    Hope this helps
    Richard
  6. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 06 Mar 2011 Link to this post

    Hi Richard,

    Thanks for the reply. I currently have the SelectedNodeChanged event sorted and have worked out how to tell the name of the node that is selected so that I can use that to determin which table to load inside the GridView. What I am stuck on is the code to actually tell the GridView to change to a different table.

    I will admit that my knowldege is a little short coming on how to deal with data sources and so far its just been based around setting the binding source for controls using the properties so i'm not entirely sure how to do it programmatically at run time. I've tried searching several sites but without even knowing what i'm searching for correctly, i'm not getting very far.

    What would be the most effecient (and correctly coded way) to tell the GridView to change from one table to another?

    Thanks,

    Guy
  7. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 07 Mar 2011 Link to this post

    Hello Guy,

    Just take a look at the following example:
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadTreeView radTreeView1;
        private RadGridView radGridView1;
        private List<SelfReferencingTreeSource> list;
     
        public Form1()
        {
            InitializeComponent();
     
            this.Size = new Size(800, 600);
     
            this.Controls.Add(radGridView1 = new RadGridView());
            radGridView1.Dock = DockStyle.Fill;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
     
            list = new List<SelfReferencingTreeSource>();
            list.Add(new SelfReferencingTreeSource(1,0,"Parent1", "ParentTable1"));
            list.Add(new SelfReferencingTreeSource(2,1,"Child1", "ChildTable1"));
            list.Add(new SelfReferencingTreeSource(3,1,"Child2", "ChildTable2"));
            list.Add(new SelfReferencingTreeSource(4,0,"Parent2", "ParentTable2"));
     
            this.Controls.Add(radTreeView1 = new RadTreeView());
            radTreeView1.SelectedNodeChanged += new RadTreeView.RadTreeViewEventHandler(radTreeView1_SelectedNodeChanged);
            radTreeView1.ParentIDMember = "ParentId";
            radTreeView1.ValueMember = "Id";
            radTreeView1.DisplayMember = "Name";
            radTreeView1.DataSource = list;
            radTreeView1.Dock = DockStyle.Left;
        }
     
        void radTreeView1_SelectedNodeChanged(object sender, RadTreeViewEventArgs e)
        {
            if (e.Node == null || e.Node.DataBoundItem == null)
            {
                return;
            }
     
            // get the selected node data bound item here;
            var selectedNode = e.Node.DataBoundItem as SelfReferencingTreeSource;
     
            // get the grid data source here
            //begin data source load
            var list = new List<GridDataSource>();
            for (int i = 0; i < selectedNode.Id * 10; i++)
            {
                list.Add(new GridDataSource(i, selectedNode.Name + " " + i));
            }
            // end data source load
     
            // set the datasource for the grid
            radGridView1.DataSource = list;
        }
    }
     
    public class GridDataSource
    {
        public int Id { get; set; }
     
        public string Name { get; set; }
     
        public GridDataSource(int id, string name)
        {
            this.Id = id; this.Name = name;
        }
    }
     
    public class SelfReferencingTreeSource
    {
        public int Id { get; set; }
     
        public int ParentId { get; set; }
     
        public string Name { get; set; }
     
        public string TableName { get; set; }
     
        public SelfReferencingTreeSource(int id, int parentId, string name)
        {
            this.Id = id;
            this.ParentId = parentId;
            this.Name = name;
        }
     
        public SelfReferencingTreeSource(int id, int parentId, string name, string tableName)
            : this(id, parentId, name)
        {
            this.TableName = tableName;
        }
    }

    I really hope it's clear enough.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
  8. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 07 Mar 2011 Link to this post

    Thank you both for your help, everything is now working.

    Regards,

    Guy
  9. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 07 Mar 2011 Link to this post

    Glad you have a working solution now Guy.
    Please let me know if you need anything further
    Richard
Back to Top
UI for WinForms is Visual Studio 2017 Ready