-1

I have this problem, when the table is empty, the GridView does not show the row headers i use .net framework 4 and internet Explorer,Can you help me? this is the code:

 this is code   
     private void loadTCOR27()
             {
                 SqlDataAdapter adapter = new SqlDataAdapter();
                 DataSet ds = new DataSet();
                 try
                 {
                     string sql = null;
                     gridViewTCOR27.Visible = true;
                     table1.Visible = false;
                     string connectionString = SestanteWeb.Global.rCRVigServer.leggiStringaConnessioneSicurezzaSqlClient();
                     sql = "select * FROM [PUMA2_FINANZIARIAFAMILIARE].[dbo].[TCOR27]";
                     SqlConnection connection = new SqlConnection(connectionString);
                     connection.Open();
                     SqlCommand command = new SqlCommand(sql, connection);
                     adapter.SelectCommand = command;
                     adapter.Fill(ds);
                     adapter.Dispose();
                     command.Dispose();
                     connection.Close();
                     // gridView1.DataSource = ds.Tables[0];
                     gridViewTCOR27.DataSource = ds.Tables[0];
                     gridViewTCOR27.DataBind();

                 }
                 catch (Exception ex)
                 {


                 }
                 finally
                 {
                     adapter.Dispose();

                 }
             }
MrTony78
  • 35
  • 1
  • 7
  • 3
    First off, you're swallowing the exception. Comment out the try/catch/finally blocks and close your adapter manually. Then see if it throws an error. – IrishChieftain Jan 26 '18 at 12:07
  • 1
    Maybe the problem is not in the GridView, but in the code. You have an empty `catch` block, so if there is an error you will not see it. – VDWWD Jan 26 '18 at 12:07
  • 1
    @IrishChieftain it seems we think alike ;) – VDWWD Jan 26 '18 at 12:08
  • 1
    You're disposing of the adapter (twice), command and connection objects in your try block? – IrishChieftain Jan 26 '18 at 12:12

1 Answers1

2

Set the property ShowHeaderWhenEmpty to true in your Gridview declaration. May be it will help you.

Here is the reference to this property. Ref

vivek
  • 1,595
  • 2
  • 18
  • 35
  • thanks, but the problem persists because I do not see the footer, so I can not add new records – MrTony78 Jan 26 '18 at 13:48
  • There is also a similar property for the footer too named `ShowFooter`.Also make that to true. – vivek Jan 26 '18 at 13:49
  • 1
    @MrTony78 "the problem persists because I do not see the footer, so I can not add new records" hmm, this sounds like a different problem to me. If you can't see the footer, and/or you can't add new records, then trivially that's not the same as not being able to see the header. – ADyson Jan 26 '18 at 13:57
  • sorry but I thought that showing the header also showed the footer – MrTony78 Jan 26 '18 at 14:09
  • solved:https://stackoverflow.com/questions/3437581/show-gridview-footer-on-empty-grid – MrTony78 Jan 26 '18 at 14:49