0

我正在填充以下 WPF 数据网格:

在此处输入图像描述

使用以下方法:

private void ButtonFilterWorkflowWhenClick(object sender, RoutedEventArgs e)
{
    var db = new DatabaseHandle();



    dataGridWorkflow.ItemsSource = db.DisplayHealthIndicator(DateTime.Now, DateTime.Now);


    labelWorkflowLastLoaded.Content = "Last Loaded at " + DateTime.Now.ToString(CultureInfo.InvariantCulture) + " with " +
                              dataGridWorkflow.Items.Count.ToString(CultureInfo.InvariantCulture) + " total events.";


}

其中引用了以下类对象:

public DataView DisplayHealthIndicator(DateTime startDate, DateTime endDate)
{
    string queryString = "[marlin].[support_retrieve_workflow_history]";

    using (SqlConnection connection = new SqlConnection(GetConnectionString()))
    {
        using (var cmd = new SqlCommand(queryString, connection))
        {
            connection.Open();

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("date_from", startDate.Date);
            cmd.Parameters.AddWithValue("date_to", endDate.Date);

            var reader = cmd.ExecuteReader();

            var dt = new DataTable();
            dt.Load(reader);

            connection.Close();
            return dt.DefaultView;

        }
    }

}

我正在尝试找到一种方法来找到总列的总和,以便将其添加到我的标签中,但没有成功。我可以通过第二个查询来做到这一点,但我想尽可能避免这种情况。我应该如何解决这个问题?

4

1 回答 1

1

您可能必须枚举 中的行DataView,例如:

dataGridWorkflow.ItemsSource = db.DisplayHealthIndicator(DateTime.Now, DateTime.Now);
DataView dv = (DataView)dataGridWorkflow.ItemsSource;
DataRowCollection drc = dv.Table.Rows;
IEnumerator rowEnum = drc.GetEnumerator();
int sum = 0;
while (rowEnum.MoveNext())
{
    sum += (int)((DataRow)rowEnum.Current)[3];//assuming the 'Total' column has index 3.
}
labelWorkflowLastLoaded.Content = "Last Loaded at " + DateTime.Now.ToString(CultureInfo.InvariantCulture) + " with " + sum.ToString(CultureInfo.InvariantCulture) + " total events.";

编辑:我更改了它,以便您应该能够复制并粘贴到您的代码中。

于 2012-08-11T18:52:43.270 回答