Counting number of columns or rows.

Aug 21, 2008 at 11:21 PM
After populating my Database it seems that datagrid1.Columns.Count seems to always be equal to zero.  This obviously isn't the way its done.  Also, how do you count the rows?


Aug 21, 2008 at 11:40 PM
The number of rows is DataGrid.Items.Count.  Could you be a little more specific on what you mean by populating the Database and datagrid1.Columns.Count seems to always be equal to zero.  I'm assuming you are auto-generating the columns.  So is the problem, when you set the DataGrid.ItemsSource to your DataView no columns are generated and nothing shows up?
Aug 22, 2008 at 1:43 PM
Sorry for not being more specific.  Yes I'm auto-generating the columns.  In .net 2.0 datagrid.Columns.Count would tell you how many columns where in the grid. Specifically, I'm trying to check the column width of each column that used to be done with DataGrid.Columns[x].width.  DataGrid.Items.Count is seems strange for a Column count?.  What about a row count?
Aug 22, 2008 at 5:21 PM
row count is DataGrid.Items.Count.  Column count is DataGrid.Columns.Count and to get the column width it is DataGrid.Columns[idx].Width.
Aug 22, 2008 at 6:08 PM
DataGrid.Columns.Count gives me an accurate column count
DataGrid.Items.Count always gives me zero row count

Anyone else have this same problem?
Aug 22, 2008 at 7:26 PM
Well see that's what I thought DataGrid.Items.Count gives the correct rows but DataGrid.Columns.Count is always = to 0 and DataGrid.Columns[idx].Width is always 0 as well.  This is all after populating the grid with a database.
Aug 22, 2008 at 7:28 PM
Are you sure you don't have that backwards.  My DataGrid.Columns.Count is always 0 and DataGrid.Items.Count give me correct row count?

If so, then I think there is a bug?
Aug 22, 2008 at 8:18 PM
Could you post an example of the code you are running.
Aug 22, 2008 at 10:22 PM
The grid displays the database properly even though it is sqlite database. Of which I applaud you guys.  Good job.  In the following code datagrid.Columns is always equal to zero.

                foreach (DataGridColumn dgColumn in datagrid.Columns)
                    gridwidth += Convert.ToInt16(dgColumn.Width);

I also tryed your suggestion of using datagrid.Columns[idx].Width and datagrid.Columns.Count using a for next loop but they are also 0.

                for(int x = 0; x < dataGridView1.Columns.Count; x++)
                    gridwidth += dataGridView1.Columns[x].Width;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
using System.Data.SQLite;
using System.Threading;

using System.ComponentModel;
using System.Data;
using System.Runtime.InteropServices;
using System.IO;

namespace WpfApplication2
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
        private SQLiteDataAdapter dataAdapter = null;
        private DataSet dataSet = null;

        public Window1()

            // Insert code required on object creation below this point.
        private void loaddata()
            int gridwidth = 0;

                this.dataSet = new DataSet();
                string connString = String.Format("Data Source={0};New=False;Version=3", @"d:\icon\nominal.db");

                SQLiteConnection sqlconn = new SQLiteConnection(connString);

                string CommandText = String.Format("Select * from [{0}];", "Nominal Table");

                this.dataAdapter = new SQLiteDataAdapter(CommandText, sqlconn);
                SQLiteCommandBuilder builder = new SQLiteCommandBuilder(this.dataAdapter);

                this.dataAdapter.Fill(this.dataSet, "Nominal Table");
                DataTable NominalTable = this.dataSet.Tables["Nominal Table"];
                datagrid.ItemsSource = NominalTable.DefaultView;
                this.Width = 0;
                foreach (DataGridColumn dgColumn in datagrid.Columns)
                    gridwidth += Convert.ToInt16(dgColumn.Width);
                this.Width = gridwidth - this.Width;

            catch (SQLiteException sqlex)
            catch (Exception ex)
        private void Window_Closing(object sender, CancelEventArgs e)
            this.dataAdapter.Update(this.dataSet, "Nominal Table");  
Aug 23, 2008 at 12:45 AM
The problem is that the columns haven't been created by the time you are querying for them in the constructor.  When columns are auto-generated they are created on the first measure pass.  This is by design.  You can query for them in an override of OnContentRendered on the Window:

protected override void OnContentRendered(EventArgs e)
    int count = DataGrid_Standard.Columns.Count;


Count should be greater than zero in this function.
Aug 26, 2008 at 7:06 PM
Thanks that did the trick.