Problem Inserting new Column in DataGridView control

J

Joe Cool

I have a DataGridView control on a form. The columns are pre-defined
in design mode. I now need to insert a new column in the middle of the
existing columns. I click on the right arrow in the upper right area
of the control in design mode and the DatawGridView Tasks sub-window
opens. I click on Edit Columns, add the new column, position it and
click on OK. ALL COLUMNS DISAPPEAR!!!

I undid the changes to recover the original form from source control
and tried it a different way. I clicked on Add Column and added the
column. But since a new column added in this manner is added to the
end of the current columns, I click on Edit Column, position the new
column where I want it to be and click on OK, AND ALL OF THE COLUMNS
DISAPPEAR AGAIN!!!

Is this a known bug? I cannot find any references to it with a Google
search. Is there any way around it other than deleting all columns and
re-entereing them all?
 
R

Rich P

Instead of creating columns for your datagridview in the designer just
use a sqlDataAdapter which will create the columns on the fly. Example:

private void btnReadTbl1_Click(object sender, EventArgs e)
{
ds.Clear();
da.SelectCommand.CommandText = "Select * from tbl1";
da.Fill(ds, "tbl1"); //contains 8 columns
dgrv1.DataSource = ds.Tables["tbl1"];
}

private void btnGetImages_Click(object sender, EventArgs e)
{
ds.Clear();
da.SelectCommand.CommandText = "Select * from tblImg";
da.Fill(ds, "tblA"); //contains 3 columns
dgrv1.DataSource = ds.Tables["tblA"];
}

If you click on btnReadTbl1 again - it will remove ["tblA"] since I have
ds.Clear(); (ds is a dataset) and replace it with tbl1 again with all 8
columns from tbl1. (C# from Visual Studio 2008, should be the same for
VS2005).


Rich
 
J

Joe Cool

Instead of creating columns for your datagridview in the designer just
use a sqlDataAdapter which will create the columns on the fly.  Example:

private void btnReadTbl1_Click(object sender, EventArgs e)
{
ds.Clear();
da.SelectCommand.CommandText = "Select * from tbl1";
da.Fill(ds, "tbl1"); //contains 8 columns
dgrv1.DataSource = ds.Tables["tbl1"];

}

private void btnGetImages_Click(object sender, EventArgs e)
{
ds.Clear();
da.SelectCommand.CommandText = "Select * from tblImg";
da.Fill(ds, "tblA");  //contains 3 columns
dgrv1.DataSource = ds.Tables["tblA"];

}

If you click on btnReadTbl1 again - it will remove ["tblA"] since I have
ds.Clear();  (ds is a dataset) and replace it with tbl1 again with all 8
columns from tbl1. (C# from Visual Studio 2008, should be the same for
VS2005).

Rich

*** Sent via Developersdexhttp://www.developersdex.com***

This datagridview is not data bound.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

datagridview 4
DataGridView Column 1
datagridview column names 3
ComboBox in DataGridView 1
DataGridView question... 1
<newbie> DataGridView question 2
DataGridView Link Column 1
Datagridview columns hiding 2

Top