This is a migrated thread and some comments may be shown as answers.

Multiple tables but one GridView

7 Answers 125 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Guy
Top achievements
Rank 1
Guy asked on 06 Mar 2011, 06:15 PM
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

7 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 06 Mar 2011, 06:53 PM
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
0
Guy
Top achievements
Rank 1
answered on 06 Mar 2011, 08:39 PM
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
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 06 Mar 2011, 10:53 PM
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
0
Guy
Top achievements
Rank 1
answered on 06 Mar 2011, 11:14 PM
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
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 07 Mar 2011, 07:57 AM
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
0
Guy
Top achievements
Rank 1
answered on 07 Mar 2011, 05:35 PM
Thank you both for your help, everything is now working.

Regards,

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 07 Mar 2011, 05:38 PM
Glad you have a working solution now Guy.
Please let me know if you need anything further
Richard
Tags
GridView
Asked by
Guy
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Guy
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Share this question
or