【问题标题】:Datagridview can't display datatableDatagridview无法显示数据表
【发布时间】:2014-03-13 19:07:29
【问题描述】:

我有这个类(AddRecord),它有一个返回数据表的方法:

public DataTable SelectRec(ComboBox cb, DateTimePicker dtp1, DateTimePicker dtp2)
{
    SqlDataAdapter sqladpt = new SqlDataAdapter();
    SqlConnection conn = new SqlConnection(connectionStrings);
    DataTable dt = new DataTable();

    try
    {
        conn.Open();
        SqlCommand myCmd = conn.CreateCommand();
        myCmd.CommandType = CommandType.StoredProcedure;
        myCmd.CommandText = "SelectAccountRecord";
        myCmd.Parameters.AddWithValue("@date1", dtp1.Text);
        myCmd.Parameters.AddWithValue("@date2", dtp1.Text);
        myCmd.Parameters.AddWithValue("@newspaper", cb.Text);
        myCmd.ExecuteNonQuery();
        sqladpt.SelectCommand = myCmd;
        sqladpt.Fill(dt);
        sqladpt.Dispose();
        //DataSource = ds.Tables["[a]"].DefaultView;
    }
    catch { }

    return dt;
}

所以我在 other 中调用此方法以在我的 datagridview 中显示数据表的内容,如下所示

AddRecord ad = new AddRecord();
dgvRecorOverview.DataSource= ad.SelectRec(cbNewspaperRO, dtpFromDate, dtpToDate);

请帮忙!

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    在分配DataSource 后,您已为您的网格提供DataBind()

    喜欢这个

    dgvRecorOverview.DataBind();
    

    【讨论】:

      【解决方案2】:

      您必须设置DataGridView.AutoGenereateColumns = true; 以根据您设置为DataSource 的DataTable 自动创建列。检查您填写的DataTable (dt) 是否具有预期的行和列。如果DataTable 为空,那么显然您在网格中看不到任何数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-16
        • 1970-01-01
        • 2011-07-31
        • 2016-10-12
        • 1970-01-01
        相关资源
        最近更新 更多