设置为 DataSet 时 DataGridView 行为空设置为、行为、DataSet、DataGridView

2023-09-06 14:06:02 作者:与可爱私奔

我很难从我以编程方式添加的数据集的实例中获取 datagridviewrow 对象.我这样做是为了使用与我的其他构造函数相同的方法,该构造函数从表单上 datagridview 中的选定行工作.

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.

我的问题是 datagridview.rows.count 总是 0 .阅读ASME_AllowableStress.Rows.Count"我得到了 2600 行的完整表格.我不确定我遗漏了什么或声明错误没有添加 datagridview 行.列似乎已添加.

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();

推荐答案

由于我看不到你的其余代码,我只能假设;但是,我今天在 VB.net 中遇到了完全相同的问题(我猜那是 C#?),并且尝试将表格转储到 List(of T) 或将其附加到组合框而不是没有结果.

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.

最后,我制作了一个全新的表单,复制了绝对必要的代码,然后……它成功了.这两种形式的区别在于我在处理 Me.Load 的函数中调用了 InitializeComponent();我认为它是从程序中自动放置的(我使用的是 Visual Basic 2010 Express),但它可能是我在心不在焉的时候放置的.我把它拿出来,一切正常.

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.

为什么这个(额外的?我没有自定义构造函数)调用 InitializeComponent() 导致绝对没有数据显示(我也无法以编程方式向表中添加行),我不知道,因为正在搜索msdn 上的 InitializeComponent 没有帮助.

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!