Gridview controls (labels, buttons, text fields), 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"

+4
source share
3 answers

If the gridview data source is NULL, you can create a temporary datatable and designate it as a gridview data source.

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

If your data is NULL, then there is no data to display. To display controls, you need to have data. I would try to create a dummy string if db returns null.

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

, .

+1

:

dataGridView1.Rows.Add(num_rows);
+1

Source: https://habr.com/ru/post/1527843/


All Articles