Sethu Raman
Sethu Raman

Reputation: 143

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;

Before binding datasource

After binding datasource

Upvotes: 1

Views: 2122

Answers (1)

Ads
Ads

Reputation: 2184

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.. :-(

enter image description here

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

Upvotes: 4

Related Questions