Stephen Falken
Stephen Falken

Reputation: 141

Winforms DataGridView Showing Blank Rows

The code below is returning an empty datagridview. The gridview's column headers were added via the designer.

Using a breakpoint in the ShowGrid() method at the dataAdapter.Fill(tblStats); statement, I've ascertained that the datatable is filled with the correct data.

But when the form appears, the gridview is empty except for the column headers. The grid cells are created with a white background (not greyed out, as if the datasource were null), but the values are not there.

What am I missing?

    private void ShowGrid()
    {
        string dbConnStr = "Data Source=localhost;Initial Catalog=StatData;Persist Security Info=True;User ID=USERID;Password=PASSWORD";
        SqlConnection dbConn = new SqlConnection(dbConnStr);
        SqlCommand dbCmd = new SqlCommand();
        dbCmd.CommandType = CommandType.Text;
        dbCmd.CommandText = "SELECT * FROM Statistics";
        dbCmd.Connection = dbConn;
        dbConn.Open();
        SqlDataAdapter dataAdapter = new SqlDataAdapter(dbCmd.CommandText, dbConn);
        DataTable tblStats = new DataTable();
        dataAdapter.Fill(tblStats);
        dgvStats.DataSource = tblStats;
    }

Upvotes: 2

Views: 4961

Answers (1)

Stephen Falken
Stephen Falken

Reputation: 141

In the visual designer, once I used DataPropertyName to mirror the columns of the database it was no longer a blank dgv.

For originally, I had only filled in the Name property, and did not fill in the DataPropertyName which led to a dgv which was empty. Once DataPropertyName was filled in, it worked.

Upvotes: 7

Related Questions