.Net → WPF – Dynamically Loading LINQ-To-SQL Tables and Columns Into ComboBox
Here is some code to dynamically load the table names into a ComboBox. Then on the selection of a specific table, load the columns for that table into a second ComboBox.
First is the 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 theDisplayMemberPath and SelectedValuePath in the Tables ComboBox and theDisplayMemberPath in the Columns ComboBox.
Next is the 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.
Tags: C#, LINQ-To-SQL, wpf
October 5th, 2011 at 7:39 PM
Great article. It actually made my penis bigger.
October 11th, 2011 at 4:32 PM
Just writing to tell you that you should safeguard your blog. I can’t tell you the number of times my anti-virus has come up warning me about the black hole exploit and other weird stuff. Be sure to check the write permissions on your blog. Google WordPress Write Permissions.