C# 设置 DataGridView.DataSource 后获取空白行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1749278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:28:37  来源:igfitidea点击:

Getting blank rows after setting DataGridView.DataSource

c#winformsdatagridviewdatasource

提问by Jooj

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; 

Also tried

也试过

dataGridView.Update();

but not working.

但不工作。

Row count is ok, but why do I get blank rows?

行数没问题,但为什么我得到空行?

I'm using Winforms.

我正在使用 Winform。

采纳答案by Jooj

What is contained in your DataSet?

您的数据集中包含什么?

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).

可能包含在您的 DataSet 中的 DataTable 没有任何行。我会将 AutoGenerateColumns 保留为 true,然后手动隐藏您不想看到的列。我从未使用过 AutoGenerateColumns = false。但是,如果没有更多代码,将很难诊断。尝试交换这两个语句(首先是 .DataSource)。

AutoGenerateColumns may have no effect on the corresponding binding source (DataTable from DataSet).

AutoGenerateColumns 可能对相应的绑定源(DataSet 中的 DataTable)没有影响。

DataSet needs to be filled by DataAdapter:

DataSet 需要由 DataAdapter 填充:

// Example
DataAdapter = new SqlDataAdapter();
DataAdapter = CreateInventoryAdapter();
DataAdapter.TableMappings.Add("Table", "GARAGE");

DataAdapter.Fill(myDataSet);
myDataView = myDataSet.Tables[0].DefaultView;
dataGridView1.DataSource = myDataView

回答by tardomatic

Also, if you are using AutoGenerateColumns= false, make sure you have added some bound columns, or you will get a blank row for each record

此外,如果您使用 AutoGenerateColumns=false,请确保您添加了一些绑定列,否则您将获得每条记录的空白行

回答by Sorin Comanescu

Try something along these lines:

尝试以下方法:

grid.AutoGenerateColumns = false;

DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "Prop1";
col.HeaderText = "Property 1";
grid.Columns.Add(col);

col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "Prop2";
col.HeaderText = "Property 2";
grid.Columns.Add(col);

grid.DataSource = dataSet.Tables[0].DefaultView;

Prop1 & Prop2 should match your table's column names. Property 1 & Property 2 is the header text to be displayed.

Prop1 和 Prop2 应该与您的表的列名匹配。属性 1 和属性 2 是要显示的标题文本。

EDIT:

编辑:

From the example you gave it looks like you're combining bound columns with unbound columns.

从您给出的示例中,您似乎将绑定列与未绑定列组合在一起。

Do this:

做这个:

1.Remove any columns added using the designer 2.Add this code:

1.删​​除使用设计器添加的任何列 2.添加以下代码:

grid.AutoGenerateColumns = false;

DataGridViewColumn colID = new DataGridViewTextBoxColumn();
colID.DataPropertyName = "customerID";
colID.HeaderText = "Ident.";
grid.Columns.Add(colID);

DataGridViewColumn colName = new DataGridViewTextBoxColumn();
colName.DataPropertyName = "customerFirstName";
colName.HeaderText = "First name";
grid.Columns.Add(colName);    
grid.DataSource = dataSet.Tables[0].DefaultView;

HTH.

哈。

回答by Jooj

Ok a more extended sample:

好的一个更扩展的样本:

I made in the designer some columns:

我在设计师中做了一些专栏:

column names: customerID and customerFirstName

列名: customerID and customerFirstName

column headerText: Ident. and First name

列标题文本: Ident. and First name

then I get some data from sql table...

然后我从sql表中获取一些数据...

sql = "select customerID, customerFirstName From customer;";

dataGridView.AutoGenerateColumns = false;
dataGridView.DataSource = dataSet.Tables[0].DefaultView;

and the sql table columns are the same like column names in dataGridView.

并且 sql 表列与 dataGridView 中的列名相同。

The result I get when dataGridView.AutoGenerateColumns = false;is a two column dataGridView with headerText Ident. | First name.

我得到的结果dataGridView.AutoGenerateColumns = false;是一个带有 headerText 的两列 dataGridView Ident. | First name

When I set dataGridView.AutoGenerateColumns = true;then I get dataGridView columns like this: Ident. | First name | customerID | customerFirstName.

当我设置dataGridView.AutoGenerateColumns = true;然后我得到DataGridView列如下: Ident. | First name | customerID | customerFirstName

all rows below Identand First nameare emptyand all other below customerIDand customerFirstNameare ok.

下面的所有行IdentFirst nameareempty以及所有其他的下面customerIDcustomerFirstNameare ok

Now I want that the rows below customerIDand customerFirstNamewould be under Identand First nameand columns customerIDand customerFirstNameshould be hidden.

现在我希望下面的行customerIDcustomerFirstName将在IdentFirst name和列下customerID并且customerFirstName应该被隐藏。

I wrote this code and it works:

我写了这段代码,它有效:

 DataTable dTable = dataGridView.GetTable().Tables[0];

 foreach (DataRow dataRow in dataGridView.GetTable().Tables[0].Rows)
 {
    int n = dataGridView.Rows.Add();
    foreach (DataColumn dataColumn in dTable.Columns)
    {
       dataGridView.Rows[n].Cells[dataColumn.ColumnName].Value = dataRow[dataColumn.ColumnName].ToString();
    }
 }

But why DataGridView dosen't do this for me with this code:

但是为什么 DataGridView 不使用以下代码为我执行此操作:

dataGridView.DataSource = dataSet.Tables[0].DefaultView;

dataGridView.DataSource = dataSet.Tables[0].DefaultView;

回答by Jooj

I found the problem.

我发现了问题。

I designed columns in the VS datagridview designer. Not the column name, but the column DataPropertyName must match with fields in database.

我在 VS datagridview 设计器中设计了列。不是列名,而是列 DataPropertyName 必须与数据库中的字段匹配。

Then also duplicated columns will hide.

然后重复的列也将隐藏。

回答by Ryan Roper

I know it has been a long time since you posted this, but I just had the same problem and figured out the answer so I thought I would post it so others could benefit.

我知道你发布这篇文章已经很长时间了,但我遇到了同样的问题并找到了答案,所以我想我会发布它以便其他人可以受益。

The reason your columns were blank is because you also need to set the DataPropertyName of each of the columns in the designer to match your database field names. Just setting the design name isn't enough.

您的列为空的原因是您还需要在设计器中设置每个列的 DataPropertyName 以匹配您的数据库字段名称。仅仅设置设计名称是不够的。

Hope that helps someone.

希望能帮助某人。

回答by Murtuza

Me.dgvList.AutoGenerateColumns = False

Me.dgvList.AutoGenerateColumns = False

    Dim col = New DataGridViewTextBoxColumn()
    col.DataPropertyName = "VisitNo"
    col.HeaderText = "Sr. No."
    dgvList.Columns.Add(col)

It works

有用

回答by Muhammad Fahim

Try adding this code before loading the Grid

在加载网格之前尝试添加此代码

dataGridView.ColumnCount = 0;

回答by Josué Zatarain Espinosa

So I had a custom implementation where I give the users the ability to save the excel-like filters of the columns.

所以我有一个自定义实现,让用户能够保存列的类似 excel 的过滤器。

I loaded results into the grid, but nothing appeared since the excel-like filter on a column couldn't match anything.

我将结果加载到网格中,但没有出现任何内容,因为列上的类似 excel 的过滤器无法匹配任何内容。

回答by SAQ

I know it's late but for my case solution was not customizing columns of datagridview. Just add a blank datagridview & bind it.

我知道已经晚了,但对于我的案例,解决方案不是自定义 datagridview 的列。只需添加一个空白的 datagridview 并绑定它。