DataGridView没有填充DataSet

时间:2012-11-20 21:34:50

标签: c# sql winforms datagridview

由于某些奇怪的原因,DataGridView没有填充数据源中的表。我不确定我做错了什么。数据集DOES有一个表格。

private void BtnViewUncommittedDataClick(object sender, EventArgs e)
{
    if (rbtnNewBusiness.Checked)
    {
        try
        {
            UpdateGridForNewBusiness();
            return;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            Logger.LogError(ex.Source, ex.TargetSite.ToString(), ex.Message);
            Enabled = true;
            return;
        }
    }
}

private void UpdateGridForNewBusiness()
{
    dgvDetail.DataSource = Database.GetNewBusinessDetailJobData();
    dgvSummary.DataSource = Database.GetNewBusinessSummaryJobData();
    dgvDetailNYD.DataSource = Database.GetNewBusinessDetailJobDataNyd();
    dgvSummaryNYD.DataSource = Database.GetNewBusinessSummaryJobDataNyd();
    lblDetailRowCountlbl.Text = "";
    lblDetailNYDRowCountlbl.Text = "";
    lblSummaryRowCountlbl.Text = "";
    lblSummaryNYDRowCountlbl.Text = "";
    lblDetailRowCountlbl.Text = "New Business Detail - Row Count:";
    lblDetailNYDRowCountlbl.Text = "New Business Detail NYD - Row Count:";
    lblSummaryRowCountlbl.Text = "New Business Summary - Row Count:";
    lblSummaryNYDRowCountlbl.Text = "New Business Summary NYD - Row Count:";
    lblDetailRowCount.Text = dgvDetail.Rows.Count.ToString(CultureInfo.InvariantCulture);
    lblSummaryRowCount.Text = dgvSummary.Rows.Count.ToString(CultureInfo.InvariantCulture);
    lblDetailNYDRowCount.Text = dgvDetailNYD.Rows.Count.ToString(CultureInfo.InvariantCulture);
    lblSummaryNYDRowCount.Text = dgvSummaryNYD.Rows.Count.ToString(CultureInfo.InvariantCulture);
}

只显示第一个查询方法,因为其他查询方法是相同的:

public static DataSet GetNewBusinessDetailJobData()
{
    using (var conn = new SqlConnection(Settings.Default.ConnectionString))
    {
        using (var cmd = new SqlCommand("spGetNewBusinessDetailJobData", conn) {CommandType = CommandType.StoredProcedure})
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            using (var da = new SqlDataAdapter(cmd))
            {
                using (var ds = new DataSet())
                {
                    da.Fill(ds);
                    return ds;
                }
            }
        }
    }
}

同样,数据被拉,但由于某种原因,DataGridView不会改变。它只是灰色的。

1 个答案:

答案 0 :(得分:0)

我想出了这个问题。

我无法将DataGridView的DataSource设置为DataSet。它必须是DataTable。