在使用数据表

时间:2016-05-26 10:46:05

标签: c# sql-server datagridview datatable

我从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;

Before binding datasource

After binding datasource

1 个答案:

答案 0 :(得分:0)

我也遇到了麻烦。我正在使用SPROC。我在datagridview中有预定义/预设计的列。

我忘记为列设置DataPropertyName属性。

我认为这将完全匹配,因为列名与我的数据源中的列名匹配,但是显然不是那么聪明。.:-(

enter image description here

所以我的代码如下:

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();
}