[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

Disable automatic changes to design of DataGridView when underlying DataTable schema is modified

Am am building my first VB.NET 2.0 Windows Forms app. The BindingSource object seems very nice, however I am having a problem that hopefully has a simple answer.

I have a DataGridView control using a BindingSource as the DataSource. So far so good.

I picked which columns to display in the DataGridView, changed the ColumnType and other properties of the bound columns and had a nice looking form.

Then I modified the underlying DataTable to add another column.

The DataGridView automatically was updated to include all of the columns in the DataTable. My previous work was lost.

This has happened a few times with different DataGridView controls that are pointing to various BindingSources.

Now I make a backup of the project before making changes to the DataSet and can restore the form's files from the backup. That doesn't seem like a good long-term solution.

I looked at all of the Properties in the DataGridView. Nothing jumped out at me as a way to disable this automatic updating.

What am I missing?

John
0
JohnDesautels
Asked:
JohnDesautels
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
Bob LearnedCommented:
John,

Can all the changes to the DataTable be made before it is bound to the DataGridView?

Bob
0
 
NBSO_ISSCommented:
One thing you could do is set up the style of your datagrid and bind it to the table in the codebehind.

I usually set up my datagrids on the HTML side of my source page...

<asp:datagrid id="grdList" runat="server" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000066"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
      <Columns>
         <asp:BoundColumn DataField="Name" ItemStyle-Wrap=False HeaderText="Requesting User" />
      <asp:BoundColumn DataField="ShortDescription" HeaderStyle-Width="800" HeaderStyle-Wrap = false HeaderText="Description Of Work" />
      </Columns>
</asp:datagrid>

the many attributes of <columns> can be found here...
http://dotnetjunkies.com/Article/F25D02EA-7763-4C96-AA27-260388F848D9.dcik
0
 
JohnDesautelsAuthor Commented:
Bob  -

That's the intent, but scema changes happen during the development process that require changes to the table structure.

Is there no way to shut this off?

NBSO_ISS - This is Windows Forms, not ASP.NET.

John
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Bob LearnedCommented:
John,

>>The DataGridView automatically was updated to include all of the columns in the DataTable. My previous work was lost.

Did this happen at design-time or run-time?  If it was run-time then you can set DataGridView.AutoGenerateColumns = False.

Bob
0
 
NBSO_ISSCommented:
Sorry John, I got my forums mixed up.
0
 
JohnDesautelsAuthor Commented:
Design time.
0
 
JohnDesautelsAuthor Commented:
NBSO_ISS - No worries.
0
 
Bob LearnedCommented:
John,

I don't do much design-time property changes to the DataGridView, and I don't think that there is a way from stopping the grid from doing what you described.  That it another reason why I like to configure the grid with code.

Bob
0
 
JohnDesautelsAuthor Commented:
Bob,

Thanks for your thoughts. In VS 2003 I avoided the "wizard" type stuff for the same reason. It seemed more flexible with VS 2005 so I thought I would give it a try.

Perhaps I'll have to rethink using the new tools.

I'll leave this question open for a little while in case someone has a magical solution.

John
0
 
Bob LearnedCommented:
I am always up for a little magic ;)  Just don't hold your breath.

Bob
0
 
SanclerCommented:
Like Bob, I don't use the wizards much.  But your description didn't tie in with my recollection, so I've just done a little test.  I made a three column table in my test dataset, then created a VB.NET 2005 project and used Add New Data Source to bring the table into that, selecting the three fields/columns.  I dragged the resulting table onto my form and did some customisation on the resulting DataGridView: I removed one column, changed both remaining column widths and changed the header text on one of them.  I ran it.  OK.

I saved it, then went back to the database and added a column.

I then went back to the DataSource and clicked Configure Data Set With Wizard.  I opened up the table and selected the extra field.  It appeared in the dataset, but my pre-customised datagridview remained as it was: both in design view and when I ran it.

I will add - in parenthesis - that it wasn't quite as simple as I've described it.  One reason, but not the only reason, why I don't use the wizards much is because Express, or at least my installation of it, is very clunky when it comes to moving data about like that.  It's always reporting errors, even though there aren't any, but I've learned to work my way round those.  And I had to do that in the above exercise.  I don't think it has any bearing on the issue, but I thought I'd better mention it for completeness.  It meant, in fact, that there was a shut-down and start up of VB.NET between re-configuring the dataset and me checking what was now in it and checking the datagridview.

What are you doing that is different from that?  You do say "changed the ColumnType", and I didn't include that in my test, but I cannot off-hand see why that should make a difference.  So I'm wondering whether it's some detail about how you are using the wizards, rather than the wizards themselves, which is accounting for the problem.

Roger
0
 
JohnDesautelsAuthor Commented:
Roger,

Thanks for your testing.

I've done some more testing, too.

The best work around seems to be:

1.  Save the form with the DataGridView.

2.  Change the DataTable.

3.  Ignore all of the error messages and the fact that the form with the DataGridView can no longer be rendered in the design environment.

4.  Close the tabs with the form's code.

5.  Reopen the form in design view.

I think my problem was that I han't saved the form prior to tweaking the DataTable. Visual Studio made changes to the code that rendered the datagrid. I didn't have a saved pre-change copy of the code to "roll back" to.

Although this isn't a "magical" solution, it does demystify what was going on.

Thanks!

John
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now