Re: Adding Rows to Data-Bound DataGridView
- From: jp2msft <jp2msft@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 14 Mar 2008 06:08:00 -0700
In my DataTable "Design View" (the .xsd file showing the default
"Fill,GetData()" SQL commands), I created an Insert and an Update SQL command.
Calling them is supposed to return the number of rows affected, but every
time I call them, I get 0 returned.
Can I not just call them outright?
Do I need to set something ahead of time before I call them?
Is there a way to get the reason(s) why the Insert/Update failed? (they are
in a try/catch block, but no error is thrown)
"William Vaughn" wrote:
Chapter 7 discusses these issues. Start on page 468..
No, when you have created a DataSet (strongly typed or untyped) and bound it
to a complex bound control like a DataGridView, once you change the Rows
collection members (add or delete rows, or change values in existing rows)
the binding mechanism posts these changes to the bound control. You might
have to execute EndEdit to get this operation to fire but it's posted
automatically. You should not have to call "AcceptChanges" in 90% of the
cases when handling data. AcceptChanges is used to tell ADO that the changes
marked in the DataTable/DataSet have been processed through independent
means. If you use the DataAdapter or generated TableAdapter Update method,
"accept changes" is invoked for you on a row-by-row basis as ADO walks
through the update process posting client-side changes to the server
database.
There is an entirely different strategy (that I discuss in the book) that
ignores the Update method entirely; it assumes that you have stored
procedures that handle complex updates (multi-table related or cascading
updates/deletes) without having to use ADO to do the low-level action
commands for you. This approach uses INSERT SQL to add rows to the database
and UPDATE and DELETE SQL to change or delete rows. But INSERT does not
affect the local data store--the local DataTable or TableAdapter
rowset--only the server-side data store. Keeping these two rowsets in sync
is the issue. Again, there are several ways to accomplish this including use
of replication.
You might benefit from my day-long workshop at DevTeach in Toronto on May
12th.
hth
--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker’s Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________
"jp2msft" <jp2msft@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:AC088AAF-880A-485F-B7F6-B7C81C9A172F@xxxxxxxxxxxxxxxx
What page is that described in your book?
Is there a cut-and-dry way of showing me how that's done? Your book is
great, but 1000 pages just takes me too long to find what I need. (Are you
planning a Cookbook in the future?)
From what I understand, once the underlying structure has rows added or
modified, then I need to create a DataSet of changes made to the main
DataSet, updated my TableAdapter with these changes, accept the changes
through the main DataSet, then update TableAdapter again.
Does all of that sound confusing to you, too?
How is all of this better than just creating a new SqlConnection with an
INSERT Command? Microsoft did a lot of work with this, but I think it is
making things more difficult than they need to be. Is that just me?
"William Vaughn" wrote:
Yup, if it's data bound you just add a row to the DataTable (or
structure)
to which it's bound.
--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker’s Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________
"jp2msft" <jp2msft@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:9E28E7A7-A2D3-4514-BC96-BD2FAD0F1E43@xxxxxxxxxxxxxxxx
A data-bound DataGridView is wonderfully simple to include with a
Windows
Form.
Now that all the data is there, how do I go about adding a new record?
When
I tried, Visual Studio 2005 Pro gave me an InvalidOperationException,
saying,
"Rows cannot be programmatically added to the DataGridView's rows
collection
when the control is data-bound."
Am I just not able to add a new record ever if I use a data-bound
DataGridView, or do I need to follow some special steps to un-data-bind
it,
add my data, then re-data-bind it?
- Follow-Ups:
- Re: Adding Rows to Data-Bound DataGridView
- From: William Vaughn
- Re: Adding Rows to Data-Bound DataGridView
- References:
- Re: Adding Rows to Data-Bound DataGridView
- From: jp2msft
- Re: Adding Rows to Data-Bound DataGridView
- From: William Vaughn
- Re: Adding Rows to Data-Bound DataGridView
- Prev by Date: Re: SQL Connection in detail
- Next by Date: Re: No update happening
- Previous by thread: Re: Adding Rows to Data-Bound DataGridView
- Next by thread: Re: Adding Rows to Data-Bound DataGridView
- Index(es):
Relevant Pages
|