user3247075
user3247075

Reputation: 293

Gridview show controls (labels, buttons, textfields) even if Datasource is null

How can I show my controls even if my table columns are null using gridview? All I know is ShowHeaderWhenEmpty="true"

Upvotes: 4

Views: 86

Answers (3)

Ren
Ren

Reputation: 775

you can try this:

dataGridView1.Rows.Add(num_rows);

Upvotes: 1

Mandar Patil
Mandar Patil

Reputation: 538

If datasource of gridview is null, you can create a temporery datatable and assign it as a datasuource of gridview.

if (GridView1.DataSource == null)
    {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            DataRow dr = dt.NewRow();
            dr[0] = "";
            dt.Rows.Add(dr);
            GridView1.DataSource=dt;
            GridView1.DataBind();
    }

Upvotes: 1

Wizrd
Wizrd

Reputation: 24

If your data is NULL then there is no data to show. In order to show the controls, you need to have data. I'd try to create a dummy row if the db returns null.

if(db.rows.count < 1)
{ 
    //add a row with dummy values
}

Hope this helps.

Upvotes: 1

Related Questions