Uchiha Itachi
Uchiha Itachi

Reputation: 1271

DataGrids and data bindings

I'm using C# and XAML to create a dialog box that displays information from a database. The window, more or less, functions the way it should. When it's initialized the database data populate the DataGrid appropriately.

enter image description here

As you can see, there is also a ComboBox that is meant to filter the contents of the DataGrid. It kind of does this. When I select the desired item from the ComboBox, it filters my database, and creates a List<> of the selected object types with all of their data. The problem that I'm having is that can't figure out a way to fill a temporary table with the data from the List<> to populate the DataGrid. What end's up happening is this:

enter image description here

There are the correct number of rows for the elements in the database, and when I run it with a break, and all the correct data is in the List<>; but the data isn't being displayed in the rows.

I can't create a new table in the database for the results because then the data bindings for the DataGrid won't work; I've tried repopulating the table with the information from the List<> via helper classes... Can't figure this out.

Any ideas and/or help would be stellar.

Here is about as much of the code as I can post: XML Data Bindings (the Binding Path is in accordance to columns in the database):

<DataGridTextColumn Header="Size" Binding="{Binding Path=ItemSize }"/>
<DataGridTextColumn Header="Elbow" Binding="{Binding Path=Ell}"/>
<DataGridTextColumn Header="Tee" Binding="{Binding Path=Tee}"/>
<DataGridTextColumn Header="Long-Turn Elbow" Binding="{Binding Path=LngEl}"/>
<DataGridTextColumn Header="Check Valve" Binding="{Binding Path=Chk}"/>
<DataGridTextColumn Header="Butterfly Valve" Binding="{Binding Path=Bfy}"/>
<DataGridTextColumn Header="Gate Valve" Binding="{Binding Path=Gate}"/>
<DataGridTextColumn Header="Alarm Valve" Binding="{Binding Path=Alm}"/>
<DataGridTextColumn Header="Dry Pipe Valve" Binding="{Binding Path=DPV}"/>
<DataGridTextColumn Header="45⁰ Elbow" Binding="{Binding Path=45Ell}"/>
<DataGridTextColumn Header="Tee Run" Binding="{Binding Path=Teerun}"/>
<DataGridTextColumn Header="Coupling" Binding="{Binding Path=Coup}"/>
<DataGridTextColumn Header="Swing Check" Binding="{Binding Path=Swg}"/>

C#:

var newTable = new CEqlTable();

var results = from myRow in vTable.AsEnumerable()
              where myRow.SubCategoryID == GetPipeNumber()
              select myRow;

foreach (DataRow dr in results)
{
     var nextEntry = new CEqlTableRec(
         false, (int)dr[0], (int)dr[1], (double)dr[2], (double)dr[3],
         (double)dr[4], (double)dr[5], (double)dr[6], (double)dr[7],
         (double)dr[8], (double)dr[9], (double)dr[10], (double)dr[11],
         (double)dr[12], (double)dr[13], (double)dr[14], (double)dr[15],
         Convert.ToByte(dr[18]));

    newTable.Add(nextEntry);
}

dgPipeDetail.ItemsSource = newTable;

Upvotes: 0

Views: 66

Answers (1)

WBuck
WBuck

Reputation: 5511

So never really got enough information to diagnose the issue but below I posted a very simple example of binding to a collection in the code behind. I also put a button on the main window. When the button is clicked I clear the previous data and fill the datagrid with new data. Compare what I have below with what you have. See if it gives you any ideas.

MainWindow.xaml

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="350" 
        Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <DataGrid ItemsSource="{Binding Classes}"
              CanUserAddRows="False"
              AutoGenerateColumns="False"
              Margin="0,0,220,0.5">
        <DataGrid.Columns>
           <DataGridTextColumn Header="FIRST" Binding="{Binding First}"/>
           <DataGridTextColumn Header="SECOND" Binding="{Binding Second}"/>
           <DataGridTextColumn Header="THIRD" Binding="{Binding Third}"/>
           <DataGridTextColumn Header="FOURTH" Binding="{Binding Fourth}"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Swap" 
            HorizontalAlignment="Left" 
            Margin="400,70,0,0" 
            VerticalAlignment="Top" 
            Width="75"
            Command="{Binding SwapCommand}"/>
</Grid>

MainWindow.xaml.cs

namespace DataGridTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            SwapCommand = new RelayCommand(OnExecuteSwap);

            InitializeComponent();

            for(int i = 0; i < 10; i++)
            {
                Classes.Add(
                    new MyClass
                    {
                        First = 10,
                        Second = 20,
                        Third = 30,
                        Fourth = 40
                    });
            }
        }

        private void OnExecuteSwap()
        {
            Classes.Clear();

            for(int i = 0; i < 10; i++)
            {
                Classes.Add(
                    new MyClass
                    {
                        First = 50,
                        Second = 60,
                        Third = 70,
                        Fourth = 80
                    } );
            }
        }

        public ICommand SwapCommand { get; }

        public ObservableCollection<MyClass> Classes { get; } =
            new ObservableCollection<MyClass>();
    }
}

MyClass POCO

namespace DataGridTest
{
    public class MyClass
    {
        public int First { get; set; }
        public int Second { get; set; }
        public int Third { get; set; }
        public int Fourth { get; set; }
    }
}

Upvotes: 1

Related Questions