Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview and it has 5 rows and I have bind it from database.

I am sending an email and i want to show dynamically in satus column only "Sent".

I dont want to update and insert any value in database. I want to do it from code only.

What I have tried:

I have tried so far.

<pre>public void sendMail()
        {
            foreach (DataGridViewRow rows in grdsend.Rows)
            {   
                    var subject = rows.Cells[0].Value.ToString();
                    var content = rows.Cells[1].Value.ToString();
                    var from = rows.Cells[2].Value.ToString();
                    var senderPassword = rows.Cells[3].Value.ToString();
                    var senderID = rows.Cells[4].Value.ToString();
                    var to = rows.Cells[5].Value.ToString();
                    var date = rows.Cells[6].Value.ToString();                

               var status = rows.Cells[7].Value = "Sending";
                // Now i want status variable value  "Sending" to show in datagridview cell



                MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                    mail.To.Add(to.ToString());
                    mail.Subject = subject.ToString();
                    mail.Body = content.ToString();
                    mail.From = new MailAddress(from.ToString());
                    SmtpServer.Port = 587;
                    SmtpServer.EnableSsl = true;
                    SmtpServer.Credentials = new  System.Net.NetworkCredential(from.ToString(), senderPassword.ToString());
                    SmtpServer.Send(mail);
                    MessageBox.Show("Mail sent");
                }
             
            }
Posted
Updated 22-Feb-17 19:38pm
v3

1 solution

Instead of using your
C#
MessageBox.Show("Mail sent");
, you could add an unbound column to the DatagridView and update that if the mail send is successful - you dont actually test for this at the moment, maybe you need a try...catch block around the Mail Send or refactor the mail send to a function that returns a bool if the email send is successful

these links may be useful for adding an unbound column to your bound datagridview

How to: Add an Unbound Column to a Data-Bound Windows Forms DataGridView Control[^]

C# DataGridView - Include Both Bound and Unbound Data - YouTube[^]
 
Share this answer
 
Comments
Adityakumar2318 23-Feb-17 2:47am    
"Unable to cast object of type 'System.Windows.Forms.DataGridViewRow' to type 'System.IConvertible'."}

int loopCount = 0;

foreach (DataGridViewRow rows in grdsend.Rows)
{
int iRow = Convert.ToInt32(rows);


}
// Unable to convert
//Getting error {"Unable to cast object of type 'System.Windows.Forms.DataGridViewRow' to type 'System.IConvertible'."}
Garth J Lancaster 23-Feb-17 3:54am    
I've got no idea how this code relates to the question you first asked, or, what I suggested you do - add an unbound column ...

int iRows = Convert.ToInt32(rows);

is obviously incorrect, but, you havnt explained what its relevance is/what you're trying to do here - we cant see your screen, thought processes etc, so you have to be clear, logical and concise in your explanations
Adityakumar2318 2-Mar-17 6:31am    
Thanks. My issue is resolved.
Adityakumar2318 3-Mar-17 5:14am    
foreach (DataGridViewRow rows in dgvAutoManualMailDetails.Rows)
{

string from = rows.Cells[0].Value.ToString();
string to = rows.Cells[2].Value.ToString();
string subject = rows.Cells[3].Value.ToString();
string content = rows.Cells[4].Value.ToString();
string password = rows.Cells[1].Value.ToString();
//Status

dgvAutoManualMailDetails.Rows[0].Cells[5].Value = "Mail Sending!!!";
//dgvAutoManualMailDetails.Rows[0].Cells[5].Value = "Sending..";

bool check = IsManualMailSend(subject, content, from, to, password);
if (check == true)
{
// status "sent";
dgvAutoManualMailDetails.Rows[0].Cells[5].Value = "Sent";
}

I am getting null value in this variable dgvAutoManualMailDetails.Rows[0].Cells[5].Value . I am assigning Sending and sent so i should get those values.
Adityakumar2318 3-Mar-17 5:23am    
When i am putting foreach loop dgvAutoManualMailDetails.Rows[0].Cells[5].Value = "Sent"

I am getting the actual value.

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