Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Settings invisible field values for user created rows in DataGridView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sin Jeong-hun - 08 Mar 2008 11:49 GMT
My application has a DataGridView, and lets users input new rows in
it. It uses the VS.NET generated class mechanisms, so there are three
components like XXXBindingSource, XXXTableAdapter and XXXDataSet.

Everything is fine except that there is a hidden field (meaning a
field exists in the datatable but not displayed on the DataGridView)
and I need to set the value for it programatically for the newly input
rows by user. The value is determined at the moment when the user
creates a new row, since the field represents a category value which
is selected in a TreeView control at the moment. Which event should I
use, and how could I get the newly created row?

I tried XXXBindingSource.CurrentItemChanged, but I think it is not
what I'm looking for. Thank you for reading.
Marc Gravell - 08 Mar 2008 20:35 GMT
One option is to subscirbe to the AddingNew event of the
BindingSource, and set e.NewObject to whatever you want.

Alternatively - what is behind the BindingSource? If it is
BindingList<T>, you can override AddNewCore nad set what properties
you want.

Marc
Sin Jeong-hun - 09 Mar 2008 02:07 GMT
> One option is to subscirbe to the AddingNew event of the
> BindingSource, and set e.NewObject to whatever you want.
[quoted text clipped - 4 lines]
>
> Marc
Thank you for your answer.

The data source behind the DataGridView is a very simple .MDB (Access
database) which has a simple table named XXX (XXX is not the real
name) I tried to handle AddingNew as you recommended, but I don't know
what type of object should I set for the NewObject. Ideally, I don't
want to create a new object myself, I want to receive the object
(which represents the new row) created by the user when the user just
have typed a new row, and set only the value for the hidden field
while leaving all the values input by the user intact.

Anyways, I tried like
            XXXDataSet.XXXRow. r = xxxDataSet.XXX.NewXXXRow();
            r.HiddenField = 2;
            e.NewObject = r;
but failed. I think XXXRow type is incorrect, but what type should I
set for it? Is it uncommon that setting hidden field value for newly
created row by user? I thought it should be common and there should be
already a practice for this.
Marc Gravell - 10 Mar 2008 10:48 GMT
First - note that row creation occurs as soon as you enter the new row
- so the other values might not be available at this point.

OK; two other things that occur:

1: since you are using a typed DataTable, override the OnTableNewRow
method?

           protected override void
OnTableNewRow(DataTableNewRowEventArgs e)
           {
               // use base creation logic
               base.OnTableNewRow(e);
               DataRow row = e.Row;
               // TODO: do something with row
           }

2: Listen to the ListChanged event on the binding-source; in
particular for ItemAdded:

   bindingSource1.ListChanged += bindingSource1_ListChanged;
   void bindingSource1_ListChanged(object sender,
ListChangedEventArgs e)
       {
           if (e.ListChangedType == ListChangedType.ItemAdded)
           {
               object row = bindingSource1[e.NewIndex];
           }
       }

Either of those help any? You can also listen to
ListChangedType.ItemChanged if you want to get the values during
updates - but watch out for recursion - i.e. if you set a value
whenever you detect a value has changed... you can end up going in
circlues.

Marc
Sin Jeong-hun - 10 Mar 2008 16:13 GMT
> First - note that row creation occurs as soon as you enter the new row
> - so the other values might not be available at this point.
[quoted text clipped - 33 lines]
>
> Marc

Thank you Mr. Gravell. The second way seems to be working!
Method 1 doesn't seem to work, I  tried to override OnTableNewRow, but
it didn't fired anyway.
Method 2 was OK. I tried like this;

            if (e.ListChangedType ==
System.ComponentModel.ListChangedType.ItemAdded)
            {
                System.Data.DataRowView rv = xxxBindingSource[e.NewIndex] as
System.Data.DataRowView;
                XXXDataSet.XXXRow r = rv.Row as XXXDataSet.XXXRow;
                r.InvisibleField = 2;
            }
Finally I can go on from there, thanks again.
Marc Gravell - 10 Mar 2008 16:22 GMT
Good to hear; not least because I was quickly running out of ideas ;-p

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.