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

Linq to SQL binding to GridView

0 Answers 55 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jessica
Top achievements
Rank 1
Jessica asked on 16 Jan 2012, 07:01 PM
Hi,
I am trying to create a program that will have a listbox on the left of database table names and then a grid area to the right.

When the user double clicks on a table name  a grid with the table data should be populated with the table data. Once it is open the user should be able to make changes and hit the save button that will submit the datacontext changes.

I have a linq to sql set up for the Northwind database for this example.

As soon as I make a change in the grid to a cell the entire row disappears??? not sure what I am doing wrong.

Thanks,
Jessica 
DataProvider.cs
 
 
 
namespace SampleLinqToSQL
{
    public static class DataProvider
    {
        private static NorthwindDataContext dataContext = null;
 
        public static NorthwindDataContext DataContext
        {
            get
            {
                return dataContext;
            }
        }
 
        static DataProvider()
        {
            dataContext = new NorthwindDataContext();
        }
 
        public static void SetDataContext(string connectionString)
        {
            dataContext = null;
            dataContext = new NorthwindDataContext(connectionString);
        }
    }
}


MainWindow.xaml
 
 
<Window x:Class="SampleLinqToSQL.MainWindow"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" Title="MainWindow" Height="350" Width="525">
    <DockPanel LastChildFill="True">
        <StackPanel DockPanel.Dock="Bottom" HorizontalAlignment="Right" Orientation="Horizontal">
            <Button x:Name="SaveBtn" Width="50" Height="25" Margin="5" Click="SaveBtn_Click">Save</Button>
            <Button x:Name="CancelBtn" Width="50" Height="25" Margin="5" Click="CancelBtn_Click">Cancel</Button>
        </StackPanel>
         
        <ListBox x:Name="TableNames" DockPanel.Dock="Left" Width="140" MouseDoubleClick="TableNames_MouseDoubleClick"></ListBox>
         
        <Border>
            <DockPanel x:Name="DocumentArea">
                <telerik:RadGridView x:Name="dataGridView"  AutoGenerateColumns="True" ItemsSource="{Binding}"  />
            </DockPanel>
        </Border>
    </DockPanel>
</Window>


MainWindow.xaml.cs
 
 
 
namespace SampleLinqToSQL
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string sqlConnection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Path\Northwind.mdf;Integrated Security=True;User Instance=True";
        public MainWindow()
        {
            InitializeComponent();
 
            DataProvider.SetDataContext(sqlConnection);
 
            LoadTableNames();
        }
 
        private void LoadTableNames()
        {
            List<string> tableNames = new List<string>();
 
            foreach (PropertyInfo item in DataProvider.DataContext.GetType().GetProperties())
            {
                tableNames.Add(item.Name);
            }
 
            tableNames = tableNames.OrderBy(s => s).ToList();
            TableNames.ItemsSource = tableNames;
        }
 
        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            DataProvider.DataContext.SubmitChanges();
        }
 
        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            //TODO
        }
 
        private void TableNames_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (TableNames.SelectedItem != null)
            {
                dataGridView.DataContext = DataProvider.DataContext.GetTableByName(TableNames.SelectedItem.ToString()).AsParallel();
            }
        }
    }
}

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Jessica
Top achievements
Rank 1
Share this question
or