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 / .NET Framework / General / January 2005

Tip: Looking for answers? Try searching our database.

value change in datagrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
amber - 26 Jan 2005 17:09 GMT
Hello,
How can I trigger an event, when a value (cell) in my datagrid changes?
TIA,
amber
W.G. Ryan eMVP - 26 Jan 2005 17:22 GMT
There's a CurrentCellChanged event which should do it for you.

Signature

W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com

> Hello,
> How can I trigger an event, when a value (cell) in my datagrid changes?
> TIA,
> amber
Todd Harvey - 26 Jan 2005 18:12 GMT
hey - that event is when you change cells in the grid, not for when you change the contents of a cell ... I'll post back when I get what she wants, I want the same thing just now.
W.G. Ryan eMVP - 26 Jan 2005 18:18 GMT
Yes Todd you're right.  And that's one very easy way to check if something
has changed or not.  There are a few ways to go about getting the
values -but remember that when you start changing something, the changes
aren't finsihed until the current edit is ended.  That's why if you're
typing something in a cell who's column is sorted, it doesn't start sorting
as you type - it's only when you are done editing that the value will get
sorted -so is it here.

Signature

W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com

> hey - that event is when you change cells in the grid, not for when you change the contents of a cell ... I'll post back when I get what she wants,
I want the same thing just now.
W.G. Ryan eMVP - 26 Jan 2005 18:26 GMT
Amber - after reading Todd's post I realized that my post may have been
confusing.  YOu can use CurrentCellChanged to determine when you have moved
to another cell.  Based on the way editing in a grid works - the edit
doesn't take until endcurrentedit.  Depending on what you ultimately want to
do -there are a few different approaches.  RowChanging on the underlying
datatable is another way to make that determination

Signature

W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com

> Hello,
> How can I trigger an event, when a value (cell) in my datagrid changes?
> TIA,
> amber
amber - 26 Jan 2005 19:21 GMT
Thanks,
What I'm actually trying to do is set a variable ('isDirty') to true, if the
value in any cell in my datagrid changes.
Maybe I'm going about this in the wrong way?
Amber
W.G. Ryan eMVP - 27 Jan 2005 15:22 GMT
An easy way to do this is to check the underlying DataSet's HasChanges
property - this way regardless of how the data gets changed, you can
recognize it.

Signature

W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com

> Thanks,
> What I'm actually trying to do is set a variable ('isDirty') to true, if the
> value in any cell in my datagrid changes.
> Maybe I'm going about this in the wrong way?
> Amber
Todd Harvey - 27 Jan 2005 16:01 GMT
you guys are super active on this board! you've got like 5 responses since yesterday afternoon on this

I actually got to work what Amber's original request was - I got the OnChange to fire for the underlying text box control that is in the control array of the datagrid. And if that's not confusing, it probably should be.

When I changed a text box - or when I typed anything into my text box, it fired my little OnChange for every single character typed in, which IMHO became an almost useless event handler, and I started to re-consider W.G.Ryan's original response (and to regret my response, and to hope that he didn't start a flame war on me). The OnCellChanged is probably the best place to handle a change to the data.

Here's a brief snippet from MSDN:
   Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
       Dim myString As String = "CurrentCellChanged event raised, cell focus is at "
       ' Get the co-ordinates of the focussed cell.
       Dim myPoint As String = DataGrid1.CurrentCell.ColumnNumber.ToString() + "," + DataGrid1.CurrentCell.RowNumber.ToString()
       ' Create the alert message.
       myString = myString + "(" + myPoint + ")"
       ' Show Co-ordinates when CurrentCellChanged event is raised.
       MessageBox.Show(myString, "Current cell co-ordinates")

   End Sub

---------------
Here's the ugly thing to associate an event with the underlying control:
       ' how to add the event listener for all the text boxes?
       For i As Integer = 0 To Me.dgEquipment.Controls.Count - 1
           Dim ctl As System.Windows.Forms.Control = Me.dgEquipment.Controls.Item(i)
           System.Console.Write(ctl.GetType().Name)
           System.Console.Write("    ")
           System.Console.Write(ctl.Text)
           System.Console.WriteLine("")
           If (ctl.GetType().Name = "DataGridTextBox") Then
               AddHandler ctl.TextChanged, AddressOf txtQuantity_NullTextChanged
               'OnTextChanged
           End If
       Next

   Private Sub txtQuantity_NullTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQuantity.NullTextChanged
       System.Windows.Forms.MessageBox.Show(" changed ")
   End Sub

I gave up on this approach in favor of W.G.Ryan's ... but you could associate any event you wanted, I suppose. And you could associate it only with certain columns, if you wanted.

This thread is turning out to be fruitful for me (I didn't know about the IsDirty thing , getting to the underlying controls was handy, and in the course of this I found some really cool stuff about putting all kinds of user controls in the data grid: http://www.codeproject.com/cs/miscctrl/WindowsDataGridColumns.asp#xx983919xx  (and their associated events)

But it is funny that while I did what Amber said she wanted, I didn't like it.
W.G.Ryan answered what she didn't ask, and I'm using it.
Amber actually meant something other than what she said, W.G. took care of that too, and it helped everybody.
And maybe more.
Todd Harvey - 27 Jan 2005 21:55 GMT
I have spent way too much time on this, but I have a grid with
Unit Price, Quantity, Cost, and I want it to automatically update
cost = price * quantity
as the user enters quantity.

I have it working.

It is extremely ugly (I capture events for grid controls, I grab the text box I'm entering into, and anyway, it works.)
Perhaps if it were not a bound grid, it would be easier. Message me if you are interested. todd_harvey@A_t@tennessee.edu

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.