LaVOZs

The World’s Largest Online Community for Developers

'; c# - Display selected rows in datagridview on notification popup - LavOzs.Com

using c#, winforms, sqlite

datagridview has name, startdate, expirationdate columns

I have an application where the user clicks on a button, the click event will take the expirationdate in one of the columns and minus it from today's date which will then make the row light up if the number is < 61 (It must be less than). Basically it highlights rows where the date is close to today's date. Now I want a popup that shows the "name" columns of the rows that were highlighted but I'm not sure how to proceed. If I'm right I need to put it in the for loop and use an if case maybe?

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string test = ds.Tables[0].Rows[i]["ExpirationDate"].ToString();
            if(test != "")
            {
                DateTime expiraryDate = Convert.ToDateTime(test); //convert string to datetime
                DateTime itoday = DateTime.Today; //get current date
                DateTime today = Convert.ToDateTime(itoday); //convert string to datetime
        //        String daysdiffstring = (expiraryDate - today).TotalDays.ToString();

                int daysdiffstring = expiraryDate.Subtract(today).Days;
                string daysdifffstring = Convert.ToString(daysdiffstring);

                int daysdiff = Convert.ToInt32(daysdiffstring);
                if (daysdiff < 61 && daysdiff >= -1)             //2 months close to expiry dangerous
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                        dataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
                    }
         }

        PopupNotifier popup = new PopupNotifier();
        popup.Image = Properties.Resources.information_icon;
        popup.TitleText = "Notification on ending certificates";
        popup.ContentText = "hello world";                         //replace this
        popup.Popup();

You need to add appropriate code in your for and if statement.

for (int i = 0; i<ds.Tables[0].Rows.Count; i++)
     {
        var nearToExpiryUsers = new List<string>();
        ----
        Your code
        ----

       if (daysdiff< 61 && daysdiff >= -1)      //2 months close to expiry dangerous
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    dataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
                    nearToExpiryUsers.Add(ds.Tables[0].Rows[i]["name"]);
                }
     }

Then you need to iterate through each string in nearToExpiryUsers to create a formatted string(as per your choice) and then set this formatted string to popup.ContextText.

  foreach (var expiredName in expiredNames)
        {
            popup.ContentText += expiredName + Environment.NewLine;
        }
Related
Difference Between Select and SelectMany
How to change row color in datagridview?
Index of Currently Selected Row in DataGridView
DatagridView Select last row
Selecting a row in DataGridView programmatically
How to add a new row to datagridview programmatically
Selecting rows programmatically in DataGridView
Select and display certain rows in DataGridView
DataGridView selected row to display in text boxes
C# DATAGRIDVIEW select rows and display selected rows