Solved

Windows Form - Changes in DataGrid cause Button to change

Posted on 2006-11-21
9
339 Views
Last Modified: 2008-03-17
I have a simple form (code below).  What I would like to have happen is if the user makes changes to the information in the datagrid, then btnUpdate would change color.  For example, the user changes the vendor address from 123 Main St to 456 Elm St.  As soon as the user begins typing the change, btnUdate changes color to bring to the user's attention that they Update button needs to be clicked before exiting the program.

Is this possible?


Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SqlDataAdapter1.Fill(DsVendors1)

    End Sub

    Private Sub btnClearDataSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearDataSet.Click
        DsVendors1.Clear()

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SqlDataAdapter1.Fill(DsVendors1)

    End Sub


    Private Sub btnUpdate_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If DsVendors1.HasChanges Then
            SqlDataAdapter1.Update(DsVendors1)
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

   ****** Private Sub DataGrid1_?????????(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.???????
        If DsVendors1.HasChanges Then
            btnUpdate.BackColor = Color.Red
            btnUpdate.ForeColor = Color.White
        End If
  *******  End Sub
End Class
0
Comment
Question by:painted
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 13

Assisted Solution

by:newyuppie
newyuppie earned 300 total points
ID: 17988477
i think the dataset haschanges wont reflect it has changes until you exit the cell in the datagrid, and you EndEdit on the bindingsource.
if you want to change the color of the button as soon as the user starts typing, maybe handle the CurrentCellDirtyStateChanged event (if you are using vb2005):

Sub dataGridView1_CurrentCellDirtyStateChanged( _
    ByVal sender As Object, ByVal e As EventArgs) _
    Handles dataGridView1.CurrentCellDirtyStateChanged

    If dataGridView1.IsCurrentCellDirty Then
        btnUpdate.BackColor = Color.Red
            btnUpdate.ForeColor = Color.White
    End If
End Sub
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17988797
If you're not on 2005, you'll need something like the sample code downloadable from this

http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q1018q.

The actual download link is

http://www.syncfusion.com/faq/winforms/Files/ForumTextChangedInDataGrid.zip

Roger
0
 
LVL 2

Author Comment

by:painted
ID: 17988898
Sorry for not noting this before.  I am using VS2005
0
 
LVL 2

Author Comment

by:painted
ID: 17989092
newyuppie,
Thanks for the code, but I think I'm in over my head here.  

I dimmed dataGridView1 As DataGridView.  Now I am getting an error "Handle clause requires a with event variable defined in the containing type or one of it's base types".

Can you help with this or lead me to a tutorial or sample?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:newyuppie
ID: 17989187
dim withevents datagridview1 as datagridview
0
 
LVL 2

Author Comment

by:painted
ID: 17989915
ok, the error is gone, but it's still not working.  Any other ideas?

Thanks,
donna
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 17990767
I know its not what you asked but if you add the following it will update before the for is closed if the user hasn’t clicked the update button.

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If DsVendors1.HasChanges Then
            SqlDataAdapter1.Update(DsVendors1)
        End If
    End Sub
0
 
LVL 34

Accepted Solution

by:
Sancler earned 200 total points
ID: 17991791
donna

newyuppie's code should work.  You need to put it in the CurrentCellDirtyStateChanged sub for your EXISTING datagridview, and change the reference in it from dataGridView1 to the name of your EXISTING datagridview.  Your question didn't actually show the name of your datagridview - unless its name is DataGrid1.  It was the use of that as a name that made me think you might be using VB.NET 2003, as the standard control in that is a DataGrid.  But in VB.NET 2005 the standard control is a DataGridView.  They are quite different animals, and the latter has the facility that newyuppie describes whereas the former does not.

Roger
0
 
LVL 2

Author Comment

by:painted
ID: 18020798
Thanks Sancler and newyuppie!  Works like a charm.  Took me a couple of tries to get this right, but the code and explanation got me on track.  Thanks for all you help.

donna
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Red error squiggly on vb.net 7 27
Programmatically signing Word macros 4 61
Put window Form inside tab page 10 26
Run a batch file when a Crystal Report is opened 5 15
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now