Priya
Priya

Reputation: 49

How to load database to TreeView in wpf with MVVM

this is the functionality i want to achieve thru passing the Connection string as parameter

     using (DataConnectionDialog dlg = new DataConnectionDialog())
      {

      DataSource.AddStandardDataSources(dlg);
      DataConnectionDialog.Show(dlg).ToString().Equals("OK");
      DataProvider provider = dlg.SelectedDataProvider;
     BcpConfig.DatabaseClient = provider.Name    ;
      string sqlDatabase = ConfigurationManager.AppSettings[provider.Name];
       BcpConfig..Database = DatabaseFactory.CreateDb(dlg.ConnectionString, sqlDatabase);

above on is my model class which creates the connection and give as connection string path like "Data Source=.;Initial Catalog=Northwind;Integrated Security=True"

Upvotes: 0

Views: 3140

Answers (1)

Adolfo Perez
Adolfo Perez

Reputation: 2874

This should get you started based on @sa_ddam213 answer here.

The ViewModels:

public class NodeViewModel
{
    public NodeViewModel()
    {
        Children = new ObservableCollection<NodeViewModel>();
    }
    public string Id { get; set; }
    public string Name { get; set; }
    public ObservableCollection<NodeViewModel> Children { get; set; }
}

 public class TreeViewModel
{
    public TreeViewModel()
    {
        BuildTree();
    }

    public TreeViewModel Tree
    {
        get { return this; }
    }

    private void BuildTree()
    {
        string connectionString = GetConnectionString();
        using (var connection = new SqlConnection(connectionString))
        {
            // Connect to the database then retrieve the schema information.
            connection.Open();

            // Get the schema information of Databases in your instance
            DataTable databasesSchemaTable = connection.GetSchema("Databases");


            Items = new ObservableCollection<NodeViewModel>();
            var rootNode = new NodeViewModel
            {
                Name = "Databases",
                Children = new ObservableCollection<NodeViewModel>()
            };
            Items.Add(rootNode);

            IEnumerable<string> databases = GetNameList(databasesSchemaTable.Rows, 0);

            foreach (string dbName in databases)
            {
                var dbNode = new NodeViewModel {Name = dbName};
                rootNode.Children.Add(dbNode);
                if (dbName.ToUpper().Equals("<yourdatabase>"))
                {
                    DataTable table = connection.GetSchema("Tables");
                    IEnumerable<string> tables = GetNameList(table.Rows, 2);

                    var tableNode = new NodeViewModel {Name = "Tables"};
                    dbNode.Children.Add(tableNode);
                    foreach (string tableName in tables)
                    {
                        tableNode.Children.Add(new NodeViewModel {Name = tableName});
                    }
                }
            }
        }
    }

    private IEnumerable<string> GetNameList(DataRowCollection drc, int index)
    {
        return drc.Cast<DataRow>().Select(r => r.ItemArray[index].ToString()).OrderBy(r => r).ToList();
    }

    private static string GetConnectionString()
    {
        // To avoid storing the connection string in your code,
        // you can retrieve it from a configuration file.
        return @"Data Source=<yoursource>;Database=<yourdb>;" +
           "Integrated Security=true;";
    }

    public ObservableCollection<NodeViewModel> Items { get; set; } 
}

The View:

<Window x:Class="DBTree.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DBTree.ViewModel"
    Title="MainWindow" Width="343" Height="744.625">
<Window.DataContext>
    <local:TreeViewModel></local:TreeViewModel>
</Window.DataContext>
<TreeView ItemsSource="{Binding Tree.Items}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:NodeViewModel}" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}"></TextBlock>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
</Window>

You will get something very plain like this:

tree

Then you can add styling, color, icons, etc

EDIT: I got rid of the View code-behind and used the TreeViewModel as the ViewModel of the MainWindow

Upvotes: 1

Related Questions