我从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;
答案 0 :(得分:0)
我也遇到了麻烦。我正在使用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();
}