views:

942

answers:

5

Hello,

Could somebody tell me why I'm getting blank rows after runnign this code??

...
dataGridView.AutoGenerateColumns = false; //must be false, else getting additional coulumns from sql.
dataGridView.DataSource = dataSet.Tables[0].DefaultView;

Also tried

dataGridView.Update();

but not working.

Row count is ok, but why blank rows? How could I solve this?

P.S.: I'm using win forms.

Best Regards.

+2  A: 

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

tardomatic
Do you have an short example of how to add bound columns?
Jooj
See Sorin Comanescu's post below. Thats the way to do it.
tardomatic
+4  A: 

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.

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:

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.

Sorin Comanescu
My code does the same like yours. Only that I set datagridview columns in the designer and they match with my tables column names.
Jooj
Then, if you're binding the grid using the designer and getting additional columns from sql is your only concern, you could simply make the undesirable columns invisible using the designer.
Sorin Comanescu
I could make them invisible but the additional columns header texts are different from the other columns made with designer. p.s.: This is for a mulitlanguage application.
Jooj
I'm not sure I'm getting it right... As far as I understand, you have a set of columns, of which some are invisible while the other are visible. For the invisible columns the header text doesn't really matter. For the visible columns you could set the displayed header text programatically, based on the current language. Not seeing where the problem is but again, I might be missing something. Maybe a more extended code sample could help.
Sorin Comanescu
Sorin above you can see an extended description of my problem.
Jooj
I belive your code would work, but I need to let the pre-defined columns in the designer because my multilanguage support is based on form definitions and automaticly reads/writes object and them properties...
Jooj
OK, I see. Then go the other way: set the grid's DataSource at design time (this should automatically add the columns to the grid) and use the designer to customize each column (visibility, header text, etc).The only code you would still have to write would be to populate the dataset.
Sorin Comanescu
A: 

Ok a more extended sample:

I made in the designer some columns:

column names: customerID and customerFirstName

column headerText: Ident. and First name

then I get some data from sql table...

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.

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

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

all rows below Ident and First name are empty and all other below customerID and customerFirstName are ok.

Now I want that the rows below customerID and customerFirstName would be under Ident and First name and columns customerID and customerFirstName should be hidden.

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.DataSource = dataSet.Tables[0].DefaultView;

Jooj
Have you filled your DataSet? DataAdapter = new SqlDataAdapter(); DataAdapter = CreateInventoryAdapter(); DataAdapter.TableMappings.Add("Table", "GARAGE"); DataAdapter.Fill(myDataSet); myDataView = myDataSet.Tables[0].DefaultView; dataGridView1.DataSource = myDataView
0A0D
A: 

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

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

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
0A0D
Guys thanks for your help, but I don't want to spend any more time to this. My extended code also works fine, so don't want to change it any more.Best regards
Jooj
Can you mark an answer as your closest accepted answer? Thanks
0A0D
A: 

Finally 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!!

Then also duplicated columns will hide.

Thanks to all.

regards.

Jooj