user153923
user153923

Reputation:

DataGridView: View Empty Data Grid

In my DataGridView controls, if there is no data to display, the control shows a blank screen.

What our spec calls for is something more like a blank screen in Microsoft Excel.

Is there a setting on the DataGridView control that I should specify for this or am I going to need to manually toss a number of blank rows into a DataTable to insert into there (never tried that)?

If I'd need to do that programmatically, what would be the best way to tell how many rows I should add to an empty DataTable?

Upvotes: 1

Views: 9143

Answers (5)

Niloofar
Niloofar

Reputation: 751

you can make an empty grid as below:

private void showemptygrid()
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(string));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Date", typeof(string));


        for (int i = 0; i < 20; i++)  // add 20 empty rows
        {
            DataRow dr = table.NewRow();
            dr["ID"] = "";
            dr["Name"] = "";
            dr["Date"] = "";
            table.Rows.Add(dr);
        }
        grid_logs.DataSource = table;
        grid_logs.DataBind();
    }

Obviously first you need to define a grid with these columns and set their related datafields.

Upvotes: 1

Tigran
Tigran

Reputation: 62265

Don't see any problem in making something like this:

    for (int i = 0; i < number_of_emptyrows; i++)
    {
        dgv.Rows.Add(new DataGridViewRow());
    }

This is naturally after then you defined all columns in grid.

Upvotes: 1

Praveen
Praveen

Reputation: 1449

I think you can use something like "EmptyDataTemplate" of the GridView and display some static table with columns as you want.

Hope this could be of some help!!

Upvotes: 1

Samich
Samich

Reputation: 30175

I'm afraid but it looks like you will need to populate rows and columns dynamically. Take a look on following posts:

Making a DataGridView look like an Excel sheet

Upvotes: 2

Arun
Arun

Reputation: 2523

Setting DataGridView's Datasource property should show you the grid with atleast the columns (in GridView's heading) even when DataTable is empty. If you could paste the code that you use, it might help to debug.

Upvotes: 2

Related Questions