Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey

I have a windows form application(See below) in which I have a datagridview.

I have linked the datagridview to a sql database.

I added a timer for my application to update the datagridview with new records from the database.

Now I would like to display a message (alert) to the user only when new rows are added to the datagridview and it should not stop the win form application from running.

How can I do this please?

Thanks

This is my windows form application so far:

C#
{

    public partial class Form1 : Form
    {
        Timer timer = new Timer();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
 
            this.CarUpdateTableAdapter.Fill(this.mySQLDataSource.CarUpdate);

            this.dataGridView1.Sort(this.dataGridView1.Columns[18], ListSortDirection.Descending);
            dataGridView1.AutoResizeColumns();

            timer.Interval = 300000;
            timer.Tick += new EventHandler(UpdateDataGridView);
            timer.Start();

        }

        private void UpdateDataGridView(object sender, EventArgs e)
        {
            this.CarUpdateTableAdapter.Fill(this.mySQLDataSource.CarUpdate);
        }


    }
}
Posted

C#
private void grd_movement_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
       {
           MessageBox.Show("Your Message here");// it is better to show u r message in a label .....

       }
 
Share this answer
 
Comments
xirokx 21-Jul-14 9:06am    
thanks for your response,

where in my code do I place your method?

surely I have to check first if rows are being added and only call your method if new rows are being added?

pls clarify

thanks
Its a event ...Just select the datagridview and go to properties and select event tab.....
There u will see
RowAdded event ....double click it and add the code
 
Share this answer
 
Comments
xirokx 22-Jul-14 3:45am    
public partial class Form1 : Form
{
Timer timer = new Timer();


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.CarUpdateTableAdapter.Fill(this._SAC_OTPDataSet.CarUpdate);

this.dataGridView1.Sort(this.dataGridView1.Columns[18], ListSortDirection.Descending);
dataGridView1.AutoResizeColumns();


timer.Interval = 60000;
timer.Tick += new EventHandler(UpdateDataGridView);
timer.Start();
}



private void UpdateDataGridView(object sender, EventArgs e)
{



this.CarUpdateTableAdapter.Fill(this._SAC_OTPDataSet.CarUpdate);


}

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
MessageBox.Show("New Cars Added:");
}

}

okay so my code looks like this....

On Form Load the messageBox is first shown 4-5 times, even if its not adding new records so I keep pressing accept to see datagridview. This is not how it should work.

My aim is for a messageBox to be displayed ONLY when the datagridview is refreshed as the database it gets its values from is updated every 5-6mins and sometimes there is new data and sometimes not. In the event there is new data I would like a messagebox to show up.

Please can you correct my code to enable this to happen?

Thanks in advance
xirokx 22-Jul-14 5:11am    
Ahh, to solve this I went into Form1.Designer.cs file and removed the line:

this.dataGridView1.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.dataGridView1_RowsAdded);

I then added the same line to my Form1.cs file, which checks for new records NOT when the form loads but every 4-5minutes afterwards and alerts user if new records have been added.

Thanks for your help, really appreciate it.
It is an event it will be called automatically when new rows are added.....

by using the following code u can drop the event and enable the event for row added

..................
So when datagridview is loaded for the first time make sure to drop the event.soon after loading
enable the even again
..................
//event Drop

C#
this.dataGridView1.RowsAdded -= new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.dataGridView1_RowsAdded);





//event enable

C#
this.dataGridView1.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.dataGridView1_RowsAdded);
 
Share this answer
 
v2
Comments
VICK 23-Jul-14 0:34am    
Use "Improve Solution" option at bottom of your first solution to edit it, rather than posting multiple solutions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900