DataGridView Rows Empty when set to DataSet

2019-05-30 10:16发布

问题:

Im having a hardtime trying to get a datagridviewrow object from a instance of a dataset i have programmatically added. I have done it this way as to use the same methods as my other constructor which works from a selected row in the datagridview on the form.

My problem is that the datagridview.rows.count is always 0 . Reading the "ASME_AllowableStress.Rows.Count" i get the full table at 2600 rows. Im not sure what i am missing or declared wrong to not get the datagridview rows added. The columns appear to be added.

public Material(int mat_id)
    {
        this.ID = mat_id;
        MaterialDataSet materialDataSet = new MaterialDataSet();
        MaterialDataSetTableAdapters.ASME_2009_AllowableStressTableAdapter  aSME_2009_AllowableStressTableAdapter = new MaterialDataSetTableAdapters.ASME_2009_AllowableStressTableAdapter();
        MaterialDataSetTableAdapters.TableAdapterManager tableAdapterManager = new MaterialDataSetTableAdapters.TableAdapterManager();
        aSME_2009_AllowableStressTableAdapter.Fill(materialDataSet.ASME_2009_AllowableStress);
        DataGridView materialDataGridView = new DataGridView();
        DataView myView = materialDataSet.Tables[0].DefaultView;

        materialDataGridView.DataSource = myView;      


        MessageBox.Show(Convert.ToString(materialDataSet.ASME_2009_AllowableStress.Rows.Count));
        MessageBox.Show(Convert.ToString(materialDataGridView.Rows.Count));
        DataGridViewRow row = new DataGridViewRow();

回答1:

Since I can't see the rest of your code, I can only assume; however, I had that exact same problem today in VB.net (I'm guessing that's C#?), and playing around with trying to dump the table into a List(of T) or attach it to a combobox instead got no results.

In the end, I made a whole new form, copied the absolutely necessary code over, and... it worked. The difference between the two forms is that I had a call to InitializeComponent() in my function that handles Me.Load; I think it was put there automatically from the program (I'm using Visual Basic 2010 Express), but it may have been placed by me in a moment of absent-mindedness. I took it out and everything works as desired.

Why this (extra? I don't have a custom constructor) call to InitializeComponent() causes absolutely no data to show up (I also could not programatically add rows to the table), I have no idea, as searching for InitializeComponent on msdn didn't help.

Anyway, I hope this fixes your own problem; please let us know if it does or doesn't!