Add new row from DataGrid to dataset

G

Guest

Hi All,
I have dataGrid and as its datasource is dataset. When I edit ne row, I have
to click on the next (or previous) row to this (new)row will be add into the
dataset. That is problem , if user add new row and immediate save the
dataset. in this scenario the new row don't add into the dataset.

Any sugestion?

Thanx

Honza
 
C

Cor Ligthert

Jan,

One of the most asked questions in relation to ADONET.

The data in the datagrid is pushed down to the datasource after a rowchange.
You can force that with (here showed for the first table)

BindingContext(ds.Tables[0]).EndCurrentEdit();

I hope this helps

Cor
 
G

Guest

Dear Cor,
thank you for you heko. But I didn't follow your think. I don't know How can
I use this method:
EndCurrentEdit();

Thank you again Cor,


Jan
 
S

Siju Jacob via DotNetMonster.com

Dear friend
use BindingContext(ds.Tables[0]).EndCurrentEdit();

thanks,
Siju
 
C

Cor Ligthert

Jan,

What is the problem with it, you need to set that before the place you want
to get information from a datarow according the last changed datagridrow
without that you did a row change in the datagrid.

BindingContext(ds.Tables[0]).EndCurrentEdit();

Where I wrote that ds.Table[0] is in this case the first table. However can
be of course any table or dataview.

What is the problem,
I don't see it.

Cor
 
G

Guest

Cor,
when i used this

BindingContext(ds.Tables[0]).EndCurrentEdit();

I received error:
error CS0118: 'System.Windows.Forms.Control.BindingContext' denotes a
'property' where a 'method' was expected


So I tried to use this:

BindingContext[ds.Tables[0]].EndCurrentEdit();

it's look much better, because compiler doesn't show any error, but new row
doesn't save into dataset.....


I know, mistakes is on my side, but i don' t know where.....


Thanks

Jan
 
C

Cor Ligthert

Jan,

BindingContext[ds.Tables[0]].EndCurrentEdit();

This is not the first time I make this mistake and forget that it is a
collection of bindingcontextes.

Sorry

Cor
 
G

Guest

BindingContext[ds.Tables[0]].EndCurrentEdit();

This does not work for me either. I think the DataBinding must have to be
explicitly set somewhere, but I don't see where. Ideas?
 
C

Cor Ligthert

Jan,

When the user does not click on the little pencil to confirm the update,
than it will never be done. I was involved in a long thread in another
newsgroup because that was not done.

Cor
 
G

Guest

Cor: I wonder if you are thinking of web forns, not windows forms.

I had better luck using the name of the table "data_path." :

BindingContext[dsPaths,"data_paths"].EndCurrentEdit() ;

It saved the row, but did not capture the data from the last cell. It worked
better when I forced the column to change:

dgPaths.CurrentCell = new DataGridCell(dgPaths.CurrentCell.RowNumber,
1-dgPaths.CurrentCell.ColumnNumber);
BindingContext[dsPaths,"data_paths"].EndCurrentEdit() ;

This is impossibly clumsy. The language designers must have had some
non-hack solution in mind to such an ordinary situation.
 
D

Dmytro Lapshyn [MVP]

Hi,

You are on the right track, but instead of moving grid focus to another
cell, I'd suggest that you use the grid's EndEdit() method.
 
G

Guest

You are on the right track, but instead of moving grid focus to another
cell, I'd suggest that you use the grid's EndEdit() method.

Well, you know, I thought of that, but

dgPaths.EndEidt() ;

gives a compile error of
"Windows.Systems.Forms.DataGrid.EndEdit is inaccessible due to its
protection level."

despite the fact that EndEdit is offered as an option by Intellisense.
 
D

Dmytro Lapshyn [MVP]

Derive a control from the datagrid and expose this protected method through
a public helper method.
The helper method can determine the current row and current column style
before calling the protected EndEdit().
 
G

Guest

Derive a control from the datagrid and expose this protected method through
a public helper method.
The helper method can determine the current row and current column style
before calling the protected EndEdit().

The method using the column style and row is not protected, so helper is not
required, and, I think won't work for other reasons. I wrote this:

private void AcceptText(){
DataGridColumnStyle dgc =
dgPaths.TableStyles[0].GridColumnStyles[dgPaths.CurrentCell.ColumnNumber] ;
dgPaths.EndEdit(dgc,dgPaths.CurrentCell.RowNumber,false ) ;
BindingContext[dsPaths,"data_paths"].EndCurrentEdit() ;
}

It seems to do the trick. On to the next problem.
 
D

Dmytro Lapshyn [MVP]

That's actually the right way of committing editing in progress.
BTW, I was really surprised to know there is another protected overload of
EndEdit().

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


eye5600 said:
Derive a control from the datagrid and expose this protected method
through
a public helper method.
The helper method can determine the current row and current column style
before calling the protected EndEdit().

The method using the column style and row is not protected, so helper is
not
required, and, I think won't work for other reasons. I wrote this:

private void AcceptText(){
DataGridColumnStyle dgc =
dgPaths.TableStyles[0].GridColumnStyles[dgPaths.CurrentCell.ColumnNumber]
;
dgPaths.EndEdit(dgc,dgPaths.CurrentCell.RowNumber,false ) ;
BindingContext[dsPaths,"data_paths"].EndCurrentEdit() ;
}

It seems to do the trick. On to the next problem.
 

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

Top