【问题标题】:showing empty rows in datagridview while binding with datatable与数据表绑定时在 datagridview 中显示空行
【发布时间】:2016-05-26 10:46:05
【问题描述】:

我从 sql server 数据库中检索数据并将数据填充到数据表中(结果有 4 行)。然后与datagridview绑定。它创建了四行,但都有空单元格。

绑定代码

        dt = newconnection.selectcommand("Select row_number() over(order by Join_Date) as sno , Employee_Name as empname,  REPLACE(CONVERT(CHAR(11), Join_Date, 106) ,' ','-') as doj,'" + billing_month + "' as \"billingmonth\" ," + noofdaysmonth + "  as daysofmonth ,'" + attndperiod + "'  as period, Rate as rate, 0 as billabledays from Employee_For_Customer where Customer_Name = '" + CmbCustomer.Text + "'");           
        DgvEmployee.AutoGenerateColumns = false;
        DgvEmployee.DataSource = dt;

【问题讨论】:

  • 检查here
  • @shadow 我做了同样的事情......当检查 datagridview.datasource 它显示所有行时。但它在控件中显示空单元格。
  • 删除预先设计的列,然后重试。

标签: c# sql-server datagridview datatable


【解决方案1】:

我也遇到了这个问题。我使用的是 SPROC。我的 datagridview 中有预定义/预设计的列。

我忘记为列设置 DataPropertyName 属性。

我认为这一切都会匹配,因为列的名称与我的数据源中的列的名称匹配,但显然它不是那么聪明.. :-(

所以我的代码如下所示:

private TimeSheetEntities db = new TimeSheetEntities();

private void button1_Click(object sender, EventArgs e)
{
  var recs = db.usp_ADMIN_SelectExistingSites();
  dgExistingSites.AutoGenerateColumns = false;
  dgExistingSites.DataSource = recs.ToList();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    相关资源
    最近更新 更多