问题描述
有人可以告诉我为什么运行此代码后为什么我得到空白行吗?
Could somebody tell me why I'm getting blank rows after running this code?
...
dataGridView.AutoGenerateColumns = false; //must be false, else getting additional columns from SQL
dataGridView.DataSource = dataSet.Tables[0].DefaultView;
也尝试过
dataGridView.Update();
但不起作用。
行数可以,但是为什么我会得到空白行?
Row count is ok, but why do I get blank rows?
我正在使用Winforms。
I'm using Winforms.
推荐答案
数据集中包含什么?
也许您的数据集中包含的数据表没有任何行。我将AutoGenerateColumns保留为true,只是手动隐藏不想看到的列。我从未使用过AutoGenerateColumns = false。但是,如果没有更多代码,将很难进行诊断。尝试交换这两个语句(首先是.DataSource)。
Maybe the DataTable contained in your DataSet does not have any rows. I would leave AutoGenerateColumns to true and just manually hide what columns you don't want to see. I have never used AutoGenerateColumns = false. However, without more code, it is going to be hard to diagnose. Try swapping those two statements (.DataSource first).
AutoGenerateColumns可能对相应的绑定源(DataSet中的DataTable)没有影响。
AutoGenerateColumns may have no effect on the corresponding binding source (DataTable from DataSet).
DataSet必须为由DataAdapter填充:
DataSet needs to be filled by DataAdapter:
// Example
DataAdapter = new SqlDataAdapter();
DataAdapter = CreateInventoryAdapter();
DataAdapter.TableMappings.Add("Table", "GARAGE");
DataAdapter.Fill(myDataSet);
myDataView = myDataSet.Tables[0].DefaultView;
dataGridView1.DataSource = myDataView
这篇关于设置DataGridView.DataSource后获取空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!