Page 1 of 1

WPF - Dynamically Load LINQ-To-SQL Tables and Columns Into ComboBox Dynamically loading tables and columns into ComboBoxes

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Posted 24 May 2010 - 07:40 AM

I recently answered a question in the WPF forum where a member was trying to dynamically load the tables and columns from a database using LINQ-To-SQL. So I figured I would do a tutorial about it.

First, we have our XAML:
<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox 
            Name="cboTables" 
            Height="23" 
            HorizontalAlignment="Left" 
            Margin="186,60,0,0" 
            VerticalAlignment="Top" 
            Width="120"
            ItemsSource="{Binding}"
            Selectionchanged="cboTables_Selectionchanged"
            DisplayMemberPath="RowType.Name" 
            SelectedValuePath="RowType.DataMembers" />
        
        <ComboBox 
            Height="23" 
            HorizontalAlignment="Left" 
            Margin="186,107,0,0" 
            Name="cboColumns" 
            VerticalAlignment="Top" 
            Width="120"
            ItemsSource="{Binding}"
            DisplayMemberPath="MappedName"/>
        <Label 
            Name="label1"
            Content="Tables:" 
            Height="28" 
            HorizontalAlignment="Left" 
            Margin="134,60,0,0"  
            VerticalAlignment="Top" />

        <Label 
            Name="label2" 
            Content="Columns:" 
            Height="28" 
            HorizontalAlignment="Left" 
            Margin="121,107,0,0" 
            VerticalAlignment="Top" />
    </Grid>
</Window>



Notice that we have set the ItemsSource attribute to {Binding}. This lets the control know that we are binding data to the control(big surprise). Notice that I have also set the DisplayMemberPath and SelectedValuePath in the Tables ComboBox and the DisplayMemberPath in the Columns ComboBox.

Next is our code:
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // creates a new instance of our DataContext
            using (InformationDataContext db = new InformationDataContext())
            {
                // gets the list of tables
                var tableList = db.Mapping.GetTables();
                cboTables.ItemsSource = tableList;
            }
        }

        private void cboTables_Selectionchanged(object sender, SelectionchangedEventArgs e)
        {
            cboColumns.ItemsSource = cboTables.SelectedValue as ReadOnlyCollection<System.Data.Linq.Mapping.MetaDataMember>;
        }
    }
}



We use the GetTables method of the DataContext class to get the list of tables. The objects themselves are of type System.Data.Linq.Mapping.AttributedMetaTable. Two properties of this class are RowType.Name and RowType.DataMembers. The RowType.Name is the name of the table without the "dbo" in front. The RowType.DataMembers is a list of the columns of that table.

The RowType.DataMembers objects are of type System.Data.Linq.Mapping.MetaDataMember. One property of this class is the MappedName property that we are using as the DisplayMemberPath. This gives us the name of the column without any extra data.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1