1

I have been spinning my wheels trying to display how many rows there are in my DB into a DataGrid Column called 'JobsCount' using COUNT.

sqlCon.Open();
            string query = "SELECT UserName, COUNT(UserName) as count FROM tblJobs WHERE JobStatus != 'Booked' Group by UserName";
            SqlCommand cmd = new SqlCommand(query, sqlCon);

            using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();

                adapter.Fill(dt);
                tblJobsBooked.ItemsSource = dt.DefaultView;
            }
            sqlCon.Close();

DataGrid Columns

<DataGrid.Columns>
  <DataGridTextColumn Header="User" Binding="{Binding UserName}"/>
  <DataGridTextColumn Header="Jobs Booked" Binding="{Binding JobsCount}" />
</DataGrid.Columns>

The COUNT should be calculated by UserName.

2
  • 6
    The databinding in your DataGridTextColumn binds to JobsCount whereas the column in the SQL statement is count Commented Nov 28, 2019 at 16:49
  • 2
    Face palm, completely overlooked it Commented Nov 29, 2019 at 8:20

1 Answer 1

3

Change:

string query = "SELECT UserName, COUNT(UserName) as count FROM tblJobs WHERE JobStatus != 'Booked' Group by UserName";

To this:

string query = "SELECT UserName, COUNT(UserName) as JobsCount FROM tblJobs WHERE JobStatus != 'Booked' Group by UserName";

See the difference? The DataGrid can't bind unless the names match exactly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.