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?

Re: Adding Rows to Data-Bound DataGridView by William

William
Wed Mar 12 19:25:34 CDT 2008

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@discussions.microsoft.com> wrote in message
news:9E28E7A7-A2D3-4514-BC96-BD2FAD0F1E43@microsoft.com...
>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?


Re: Adding Rows to Data-Bound DataGridView by jp2msft

jp2msft
Thu Mar 13 08:44:03 CDT 2008

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@discussions.microsoft.com> wrote in message
> news:9E28E7A7-A2D3-4514-BC96-BD2FAD0F1E43@microsoft.com...
> >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?
>

Re: Adding Rows to Data-Bound DataGridView by William

William
Thu Mar 13 12:48:04 CDT 2008

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@discussions.microsoft.com> wrote in message
news:AC088AAF-880A-485F-B7F6-B7C81C9A172F@microsoft.com...
> 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@discussions.microsoft.com> wrote in message
>> news:9E28E7A7-A2D3-4514-BC96-BD2FAD0F1E43@microsoft.com...
>> >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?
>>


Re: Adding Rows to Data-Bound DataGridView by Miro

Miro
Thu Mar 13 14:02:42 CDT 2008

Hey,

You updated your 2008 tour schedule.

Ive been peaking on your webstie for months to see where you will be in
2008.

Might see you there.

Cheers'

Miro

"William Vaughn" <billvaNoSPAM@betav.com> wrote in message
news:F8CF795B-0116-47C8-A504-457E3AF706C1@microsoft.com...
> 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@discussions.microsoft.com> wrote in message
> news:AC088AAF-880A-485F-B7F6-B7C81C9A172F@microsoft.com...
>> 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@discussions.microsoft.com> wrote in message
>>> news:9E28E7A7-A2D3-4514-BC96-BD2FAD0F1E43@microsoft.com...
>>> >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?
>>>
>


Re: Adding Rows to Data-Bound DataGridView by Miro

Miro
Thu Mar 13 14:06:38 CDT 2008

I might have to scratch that. ( just checked the website )

For beign a begininner as much as I am...I cannot justify a cost of 500 yet
for 1 day.

But Great book!

Miro

"Miro" <miro@beero.com> wrote in message
news:ueW6mzThIHA.6084@TK2MSFTNGP06.phx.gbl...
> Hey,
>
> You updated your 2008 tour schedule.
>
> Ive been peaking on your webstie for months to see where you will be in
> 2008.
>
> Might see you there.
>
> Cheers'
>
> Miro
>
> "William Vaughn" <billvaNoSPAM@betav.com> wrote in message
> news:F8CF795B-0116-47C8-A504-457E3AF706C1@microsoft.com...
>> 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@discussions.microsoft.com> wrote in message
>> news:AC088AAF-880A-485F-B7F6-B7C81C9A172F@microsoft.com...
>>> 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@discussions.microsoft.com> wrote in message
>>>> news:9E28E7A7-A2D3-4514-BC96-BD2FAD0F1E43@microsoft.com...
>>>> >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?
>>>>
>>
>


Re: Adding Rows to Data-Bound DataGridView by jp2msft

jp2msft
Fri Mar 14 08:08:00 CDT 2008

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@discussions.microsoft.com> wrote in message
> news:AC088AAF-880A-485F-B7F6-B7C81C9A172F@microsoft.com...
> > 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@discussions.microsoft.com> wrote in message
> >> news:9E28E7A7-A2D3-4514-BC96-BD2FAD0F1E43@microsoft.com...
> >> >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?
> >>
>

Re: Adding Rows to Data-Bound DataGridView by William

William
Fri Mar 14 10:49:31 CDT 2008

This sounds like the Update method was called with no work to do. This is
often caused by calling AcceptChanges or by not calling EndEdit before the
Update (which posts changes from the bound control to the underlying
DataTable).

--
__________________________________________________________________________
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@discussions.microsoft.com> wrote in message
news:B5A8E91B-E7FD-48D2-9E28-BC14873616E5@microsoft.com...
> 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@discussions.microsoft.com> wrote in message
>> news:AC088AAF-880A-485F-B7F6-B7C81C9A172F@microsoft.com...
>> > 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@discussions.microsoft.com> wrote in message
>> >> news:9E28E7A7-A2D3-4514-BC96-BD2FAD0F1E43@microsoft.com...
>> >> >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?
>> >>
>>


Re: Adding Rows to Data-Bound DataGridView by jp2msft

jp2msft
Fri Mar 14 15:51:01 CDT 2008

Mr. Vaughn,

Here is what I had to do, that made things work for me. Please correct me if
I did it the "Boneheaded Way":

I created an InsertRecord and a ModifyRecord for my TableAdapter in the .xsd
Designer. When I need to modify or insert a record, I call one of these
methods on the TableAdapter.

Also, I have to select the current row from (or insert a new row into) the
DataSet. With this row, I update my information with the same information
that I used in my TableAdapter.

On the row, I AcceptChanges.

On the TableAdapter, I Update.

This seems to be the only way I've found to ensure that changes that appear
on the screen also persist to my SQL database.

Goodness! I didn't realize I had to save these changes in two (2) places!

Again, if I am doing this completely wrong, or if you can see a smoother way
of accomplishing what I am doing, please let me know.

Regards,
Joe

"William Vaughn" wrote:

> This sounds like the Update method was called with no work to do. This is
> often caused by calling AcceptChanges or by not calling EndEdit before the
> Update (which posts changes from the bound control to the underlying
> DataTable).
>
> --
> __________________________________________________________________________
> 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@discussions.microsoft.com> wrote in message
> news:B5A8E91B-E7FD-48D2-9E28-BC14873616E5@microsoft.com...
> > 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@discussions.microsoft.com> wrote in message
> >> news:AC088AAF-880A-485F-B7F6-B7C81C9A172F@microsoft.com...
> >> > 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@discussions.microsoft.com> wrote in message
> >> >> news:9E28E7A7-A2D3-4514-BC96-BD2FAD0F1E43@microsoft.com...
> >> >> >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?
> >> >>
> >>
>

Re: Adding Rows to Data-Bound DataGridView by Cor

Cor
Sat Mar 15 07:19:03 CDT 2008

JP,

This cannot be the solution because accept changes is to PREVENT that rows
will be updated.

This not for you, because I have seen this written by Bill more times in
this message thread, but for those who because of your answer will have
doubt because of your message in the knowledge of Bill or otherwise take
your solution as true.

Cor


Re: Adding Rows to Data-Bound DataGridView by jp2msft

jp2msft
Mon Mar 17 08:39:00 CDT 2008

Thanks Cor,

I'm begining to think now that I should *not* have been working with
DataBound Datasets, but I'm getting pressure to release this so I don't have
time to re-write it.

"Cor Ligthert[MVP]" wrote:

> JP,
>
> This cannot be the solution because accept changes is to PREVENT that rows
> will be updated.
>
> This not for you, because I have seen this written by Bill more times in
> this message thread, but for those who because of your answer will have
> doubt because of your message in the knowledge of Bill or otherwise take
> your solution as true.
>
> Cor
>

Re: Adding Rows to Data-Bound DataGridView by Cor

Cor
Tue Mar 18 00:27:21 CDT 2008

You probably need the EndEdit, in fact is that something as "accept the
changes from the DataGridView", but that has Bill as well written before.

Cor