none
datagridview shows correct number of rows - but all empty RRS feed

  • Question

  • Hello - I'm tasked in maintaining and extending an existing client-server-application that uses a datagridview as its main point of information to the user.

    The data gets pulled server-side, then sent over using WCF. After some formatting finally gets displayed in said datagridview.

    Problem: I had to implement a different WCF connection - thus changed the source for the data. And the structure.

    Using debugs and logs I see that "my data" successfully arrive behind the scenes - only the datadridview shows all empty rows. I followed several hints to solve the issue - converted from array to ArrayList - a single Datarow is an object of 11 strings (and a single ToString() Method for logging / debugging ).

    My various attempts all fail

    • assign ArrayList direct as DataSource to the dGV 
    • assign ArrayList as DataSource to a BindingSource - which serves as DataSource to the dGV

    Result every time .. a number of empty rows that match the source data

    Any further hints how I can get my data into the datagridview 

    Tuesday, December 3, 2019 4:09 PM

Answers

  • If each DataGridViewColumn do you set the DataPropertyName ?

    Another thing, place a breakpoint on the line which assigns the DataSource to the DataGridView, when hit hover over the data source be it an ArrayList or not, do you see content? If so then it would seem the DataPropertyName would be an issue or another setting for the columns.

    I would also recommend using a class which represents incoming data, loop through the data, populate a List<T> where T is the class then assign the DataPropertyName for each column and set the DataSource for the DataGridView to the List<T>.

    Does this make sense?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by eagle_275 Wednesday, December 4, 2019 12:34 PM
    Tuesday, December 3, 2019 6:56 PM
    Moderator
  • I do - although not with this fancy dialog but using 

    // 
    // index
    // 
    this.index.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
    this.index.DataPropertyName = "index";
    this.index.FillWeight = 25.38072F;
    this.index.HeaderText = "index";
    this.index.MinimumWidth = 10;
    this.index.Name = "index";
    this.index.ReadOnly = true;
    

    the Headertext is later overwritten by one from a DLL_resource to allow for easy translation

    All 11 columns have a unique DataPropertyName - and the strings in the object wear the same name as the DataPropertyName

    Everything else I do already as you mention. Initially it was a generic List <T> - then an array of T - now the ArrayList which only contains T-objects ... 

    Using your method with the breakpoint I can see that DataSource contains indeed my data (2 empty rows, 2 filled) only "somehow" the datagridview doesn't display except for the number of rows 

    • Marked as answer by eagle_275 Wednesday, December 4, 2019 8:20 AM
    • Edited by eagle_275 Wednesday, December 4, 2019 8:28 AM
    Wednesday, December 4, 2019 7:59 AM
  • To my surprise the final solution was rather simple ...

    before setting the new datasource - it's necessary to set the datasource once to null before setting it to the new source .. which I didnt expect.

    • Marked as answer by eagle_275 Wednesday, December 4, 2019 12:04 PM
    Wednesday, December 4, 2019 12:03 PM
  • Have to add up on my initial question - 

    why the heck does the Datagridview get a rowCount of 18 - if only 17 rows of data are in the datasource - which I saw during tests using breakpoint(s) - I see 17 rows divided by | and I assume one further row that's completely empty (not even dividers between columns - but nonetheless kicks my remaining code in the knee (a check if my presumably unprocessed data have the same number of rows as the dGV (which fails .. 17 <> 18 ... 

    Any clues where that 18th row can come from ?

    ResultBindingSource.DataSource = null;
    dGV.DataSource = null;
    ResultBindingSource.DataSource = dbCom.KaData;
    dGV.DataSource = ResultBindingSource;
    dGV.Enabled = true;

    thats essentially the code that deals with inserting the datasource - I can't detect any line of code that would add another line - and as I said

    the BindingSource reports Count=17 but RowCount is somehow 18 ??


    Wednesday, December 4, 2019 4:19 PM

All replies

  • If each DataGridViewColumn do you set the DataPropertyName ?

    Another thing, place a breakpoint on the line which assigns the DataSource to the DataGridView, when hit hover over the data source be it an ArrayList or not, do you see content? If so then it would seem the DataPropertyName would be an issue or another setting for the columns.

    I would also recommend using a class which represents incoming data, loop through the data, populate a List<T> where T is the class then assign the DataPropertyName for each column and set the DataSource for the DataGridView to the List<T>.

    Does this make sense?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by eagle_275 Wednesday, December 4, 2019 12:34 PM
    Tuesday, December 3, 2019 6:56 PM
    Moderator
  • If you can create a simple sample that recreates the problem then you are guaranteed to get an answer. Something like the following. This is not the same as yours; it fails to create a DataGridView but probably because the columns are auto-generated. See what happens when the dgvdefine members are made properties (as in the commented code).

    public partial class Form1 : Form
    {
        List<dgvdefine> definesds = new List<dgvdefine>();
        BindingSource definesbs = new BindingSource();
    
        public Form1()
        {
            InitializeComponent();
            //
            dgvdefine d;
            d = new dgvdefine();
            d.Name = "One";
            d.Comment = "First";
            definesds.Add(d);
            d = new dgvdefine();
            d.Name = "Two";
            d.Comment = "Second";
            definesds.Add(d);
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            definesbs.DataSource = definesds;
            dataGridView1.DataSource = definesbs;
        }
    }
    
    public class dgvdefine
    {
        //public string Name { get; set; }
        //public string Comment { get; set; }
        public string Name;
        public string Comment;
    }
    




    Sam Hobbs
    SimpleSamples.Info

    Tuesday, December 3, 2019 9:10 PM
  • I do - although not with this fancy dialog but using 

    // 
    // index
    // 
    this.index.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
    this.index.DataPropertyName = "index";
    this.index.FillWeight = 25.38072F;
    this.index.HeaderText = "index";
    this.index.MinimumWidth = 10;
    this.index.Name = "index";
    this.index.ReadOnly = true;
    

    the Headertext is later overwritten by one from a DLL_resource to allow for easy translation

    All 11 columns have a unique DataPropertyName - and the strings in the object wear the same name as the DataPropertyName

    Everything else I do already as you mention. Initially it was a generic List <T> - then an array of T - now the ArrayList which only contains T-objects ... 

    Using your method with the breakpoint I can see that DataSource contains indeed my data (2 empty rows, 2 filled) only "somehow" the datagridview doesn't display except for the number of rows 

    • Marked as answer by eagle_275 Wednesday, December 4, 2019 8:20 AM
    • Edited by eagle_275 Wednesday, December 4, 2019 8:28 AM
    Wednesday, December 4, 2019 7:59 AM
  • To my surprise the final solution was rather simple ...

    before setting the new datasource - it's necessary to set the datasource once to null before setting it to the new source .. which I didnt expect.

    • Marked as answer by eagle_275 Wednesday, December 4, 2019 12:04 PM
    Wednesday, December 4, 2019 12:03 PM
  • Have to add up on my initial question - 

    why the heck does the Datagridview get a rowCount of 18 - if only 17 rows of data are in the datasource - which I saw during tests using breakpoint(s) - I see 17 rows divided by | and I assume one further row that's completely empty (not even dividers between columns - but nonetheless kicks my remaining code in the knee (a check if my presumably unprocessed data have the same number of rows as the dGV (which fails .. 17 <> 18 ... 

    Any clues where that 18th row can come from ?

    ResultBindingSource.DataSource = null;
    dGV.DataSource = null;
    ResultBindingSource.DataSource = dbCom.KaData;
    dGV.DataSource = ResultBindingSource;
    dGV.Enabled = true;

    thats essentially the code that deals with inserting the datasource - I can't detect any line of code that would add another line - and as I said

    the BindingSource reports Count=17 but RowCount is somehow 18 ??


    Wednesday, December 4, 2019 4:19 PM
  • Have to add up on my initial question - 

    why the heck does the Datagridview get a rowCount of 18 - if only 17 rows of data are in the datasource - which I saw during tests using breakpoint(s) - I see 17 rows divided by | and I assume one further row that's completely empty (not even dividers between columns - but nonetheless kicks my remaining code in the knee (a check if my presumably unprocessed data have the same number of rows as the dGV (which fails .. 17 <> 18 ... 

    Any clues where that 18th row can come from ?

    ResultBindingSource.DataSource = null;
    dGV.DataSource = null;
    ResultBindingSource.DataSource = dbCom.KaData;
    dGV.DataSource = ResultBindingSource;
    dGV.Enabled = true;

    thats essentially the code that deals with inserting the datasource - I can't detect any line of code that would add another line - and as I said

    the BindingSource reports Count=17 but RowCount is somehow 18 ??


    RowCount will be one higher when there is a blank row at the bottom of th DataGridView, if there is no blank row the count will be the same.

    private void button1_Click(object sender, EventArgs e)
    {
        Console.WriteLine(_bindingSource.Count);
        Console.WriteLine(dataGridView1.RowCount);
    
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Wednesday, December 4, 2019 5:06 PM
    Moderator
  • eagle_275, did that answer your question?

    Did you see my reply indicating that the problem could be that the fields are not properties?



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, December 4, 2019 9:52 PM