none
1st row blank in DataGridView RRS feed

  • Question

  • After adding columns to a DataGridView I add values. When The DataGridView is displayed there are two rows, the first row is blank.  I checked the row count before adding values, 1.   I'm not adding any rows before insertion, yet the first row is blank after insertion.  The only solution I have is to remove the first row after insertion.  Is there another way?

    Tuesday, August 20, 2019 8:04 PM

Answers

  • I was doing the data retrieval writing the code myself, I literally forgot about using a data source.  I set up the data source and its working.  No idea why its not working with the code I posted.
    Wednesday, August 21, 2019 9:28 AM

All replies

  • Hi,

    Could you provide your code that create columns and add rows?

    Here is my demo, it works normally.

        private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
            acCode.Name = "Col1";
            acCode.HeaderText = "Col1";
            dataGridView1.Columns.Add(acCode);
    
            string[] row1 = new string[] { "Col1Value"};
            dataGridView1.Rows.Add(row1);
        }

    Result,

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 21, 2019 7:31 AM
    Moderator
  • This is my code

    DataGridView Test= new DataGridView();
                    this.Controls.Add(Test);
    DataGridViewColumn Column1= new DataGridViewColumn();
                Column1.Name = "Column1";
                Column1.DefaultCellStyle.Font = new Font("Times New Roman", 12);
                DataGridViewCell Column= new DataGridViewTextBoxCell();
                Col1 .Style.BackColor = Color.White;
                Column1.CellTemplate = Col1;
                Test.Columns.Add(Column1);
    
                DataGridViewColumn Column2= new DataGridViewColumn();
                Column2.Name = "Column2";
                Column2.DefaultCellStyle.Font = new Font("Times New Roman", 12, FontStyle.Bold);
                Column2.DefaultCellStyle.Format = "dd/mm/yyyy";
    DataGridViewCell Col2= new DataGridViewTextBoxCell();
                ins.Style.BackColor = Color.White;
                Column2.CellTemplate = Col2;
                Test.Columns.Add(Column2);
    
                DataGridViewColumn Column3 = new DataGridViewColumn();
                Column3 .Name = "Column3 ";
                Column3 .DefaultCellStyle.Font = new Font("Times New Roman", 12, FontStyle.Bold);
                Column3 .DefaultCellStyle.Format = "dd/mm/yyyy";
                DataGridViewCell Col3 = new DataGridViewTextBoxCell();
                Col3 .Style.BackColor = Color.White;
                Column3 .CellTemplate = Col3;
                Test.Columns.Add(Column3);
    
                DataGridViewColumn Column4 = new DataGridViewColumn();
                Column4.Name = "Column4";
                Column4.DefaultCellStyle.Font = new Font("Times New Roman", 12, FontStyle.Bold);
                Column4.DefaultCellStyle.Format = "dd/mm/yyyy";
                DataGridViewCell Col4= new DataGridViewTextBoxCell();
                Col4.Style.BackColor = Color.White;
                Column4.CellTemplate = Col4;
                Test.Columns.Add(Column4);
    
                DataGridViewColumn Column5= new DataGridViewColumn();
                Column5.Name = "Column5";
                Column5.DefaultCellStyle.Font = new Font("Times New Roman", 12, FontStyle.Bold);
                Column5.DefaultCellStyle.Format = "dd/mm/yyyy";
                DataGridViewCell Col5= new DataGridViewTextBoxCell();
                Col5.Style.BackColor = Color.White;
                Column5.CellTemplate = Col5;
                ComplianceData.Columns.Add(Column5);
    
                DataGridViewColumn Column6= new DataGridViewColumn();
                Column6.Name = "Column6";
                Column6.DefaultCellStyle.Font = new Font("Times New Roman", 12, FontStyle.Bold);
                Column6.DefaultCellStyle.Format = "dd/mm/yyyy";
                DataGridViewCell Col6= new DataGridViewTextBoxCell();
                Col6.Style.BackColor = Color.White;
                Column6.CellTemplate = Col6;
                Test.Columns.Add(Column6);

    Wednesday, August 21, 2019 8:29 AM
  • Hi,

    How did you add new rows? The code you provided works fine. First row is not empty.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 21, 2019 8:38 AM
    Moderator
  • I was doing the data retrieval writing the code myself, I literally forgot about using a data source.  I set up the data source and its working.  No idea why its not working with the code I posted.
    Wednesday, August 21, 2019 9:28 AM