简体   繁体   中英

showing empty rows in datagridview while binding with datatable

I retrieve data from sql server database and filled the data in a datatable(result having 4 rows). Then bound with datagridview. It creating four rows but all are having empty cells.

Binding code

        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;

绑定数据源之前

绑定数据源后

I was having trouble with this too. I was using a SPROC. I had pre-defined/pre-designed columns in my datagridview.

I had forgotten to set the DataPropertyName property for the columns.

I thought it would all just match up because the name of the column matched the name of the column from my datasource, but apparently it's not that smart.. :-(

在此处输入图片说明

So my code looks like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM