问题描述
我有一个hardtime尝试从数据集的一个实例获取一个datagridviewrow对象,我已经以编程方式添加。我以这种方式做到这一点,就是使用与其他构造函数相同的方法,该构造函数在窗体的datagridview中的选定行中工作。我的问题是datagridview .rows.count总是为0。阅读ASME_AllowableStress.Rows.Count我得到2600行的完整表。我不知道我丢失或声明错误,没有获得datagridview行添加。列似乎已添加。
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#?),并尝试将表转储到列表(T)或将其附加到组合框而不是结果。
最后,我做了一个全新的形式,复制了绝对必要的代码,...它的工作。两种形式之间的区别在于我处理Me.Load的函数中调用了 InitializeComponent();我认为这是从程序中自动放置的(我使用的是Visual Basic 2010 Express),但这可能是在我无意识的时刻被放置的。我把它拿出来,一切都按照需要工作。
为什么这个(额外的,我没有自定义构造函数)调用InitializeComponent()导致绝对没有数据显示(我也无法在表中添加行),我不知道,因为在msdn上搜索InitializeComponent没有帮助。
无论如何,我希望这个修复自己的问题;请让我们知道,如果是,否则!
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();
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!
这篇关于DataGridView设置为DataSet时为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!