Data grid not binding properly.

Jan 26, 2009 at 3:40 PM
Hi,

I am binding a simple self populated datatable to an on screen datagrid. I am using the code given below but the grid doesn't gets populated with the values. Please pin point the error in my code or an alternative solution.


public partial class WinReuseMatrix : Window
    {
        String[,] reuseMatrixData   = new String[24, 24];
        String[]  reuseMatrixValues = new String[4]{"0", "1", "1#", "0#" };

        public WinReuseMatrix()
        {
            InitializeComponent();
            LoadDataInReuseMatrix();
            LoadDataToGrid();
        }

        private void LoadDataInReuseMatrix()
        {
            Random rand = new Random(234);

            for (int i = 0; i < 24; i++)
            {
                for (int j = 0; j < 24; j++)
                {
                    reuseMatrixData[i, j] = reuseMatrixValues[rand.Next(0, 3)];
                }
            }
        }

        private void LoadDataToGrid()
        {
            DataTable dtReuseMatrix = new DataTable("Data");

            dtReuseMatrix.Columns.Add("1",  Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("2",  Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("3",  Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("4",  Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("5",  Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("6",  Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("7",  Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("8",  Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("9",  Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("10", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("11", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("12", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("13", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("14", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("15", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("16", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("17", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("18", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("19", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("20", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("21", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("22", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("23", Type.GetType("System.String"));
            dtReuseMatrix.Columns.Add("24", Type.GetType("System.String"));
            
            for (int i = 0; i < 24; i++)
            {
                DataRow dr = dtReuseMatrix.NewRow();

                for (int j = 0; j < 24; j++)
                {
                    dr[j] = reuseMatrixData[i, j];                    
                }
                dtReuseMatrix.Rows.Add(dr);
            }

            Binding bReuseMatrix = new Binding();
            bReuseMatrix.Source = dtReuseMatrix;

            dgReuseMatrix.ItemsSource = dtReuseMatrix.DefaultView;

        }

        private void MenuItem_Exit_Clicked(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }

Cheers.
Coordinator
Jan 26, 2009 at 6:22 PM
I copied your code into a project, specified the DataGrid as <DataGrid Name="dgReuseMatrix" />, and I was able to see content (all the cells were one of "0," "1," "1#," or "0#").

I also noticed that in the sample code, you do not actually use bReuseMatrix. Perhaps there is additional code or markup that isn't included in the sample that breaks the functionality?

Thanks,
Ben