Send SMS to Checked (Selected) Rows of DataGridView in Windows Application using C# and VB.Net

Last Reply 6 months ago By pandeyism

Posted 6 months ago

Any idea how can I send sms to for each selected customer using checkbox in datagridiview one by one (if sms sent successfully then send to next one)

Here is code i was using 

For Each row As DataGridViewRow In DataGridView2.Rows
    If SendSMS("https://www.sms.com/ucph.php", "POST", "msg=Test&num=914444.Contains("Successfully") Then
Continue For
Else 
    MsgBox("Problem occured while sending SMS", MsgBoxStyle.Exclamation, "Erro")
Exit For
End If 
Next

 

Posted 6 months ago Modified on 6 months ago

Hey satabeach,

Please refer below sample.

Namespaces

using System.Data;

VB.Net

Imports System.Data

Code

C#

private void Form1_Load(object sender, EventArgs e)
{
    this.BindDataGridView();
}

private void BindDataGridView()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ContactNumber", typeof(int)),
        new DataColumn("Name", typeof(string)),
        new DataColumn("Messaage",typeof(string)) });
    dt.Rows.Add(123456, "John Hammond", "Hey");
    dt.Rows.Add(235489, "Mudassar Khan", "Hello");
    dt.Rows.Add(345556, "Suzanne Mathews", "Hmmmm");
    dt.Rows.Add(466677, "Robert Schidner", "Whats'up");
    this.dataGridView1.DataSource = dt;
    DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
    checkBoxColumn.HeaderText = "";
    checkBoxColumn.Width = 30;
    checkBoxColumn.Name = "checkBoxColumn";
    dataGridView1.Columns.Insert(0, checkBoxColumn);
}

private void Send(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
        if (isSelected)
        {
            string contactNumber = row.Cells[1].Value.ToString();
            string message = row.Cells[3].Value.ToString();
            if (!SendSMS(message, contactNumber))
            {
                MessageBox.Show("Problem occured while sending SMS", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
private bool SendSMS(string message, string number)
{
    //Send SMS Code that return the status.
    bool status = false;
    return status;
}

VB.Net

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.BindDataGridView()
End Sub

Private Sub BindDataGridView()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(2) {New DataColumn("ContactNumber", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Messaage", GetType(String))})
    dt.Rows.Add(123456, "John Hammond", "Hey")
    dt.Rows.Add(235489, "Mudassar Khan", "Hello")
    dt.Rows.Add(345556, "Suzanne Mathews", "Hmmmm")
    dt.Rows.Add(466677, "Robert Schidner", "Whats'up")
    Me.dataGridView1.DataSource = dt
    Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
    checkBoxColumn.HeaderText = ""
    checkBoxColumn.Width = 30
    checkBoxColumn.Name = "checkBoxColumn"
    dataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub
Private Sub Send(ByVal sender As Object, ByVal e As EventArgs) Handles btnTransfer.Click
    For Each row As DataGridViewRow In dataGridView1.Rows
        Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)
        If isSelected Then
            Dim contactNumber As String = row.Cells(1).Value.ToString()
            Dim message As String = row.Cells(3).Value.ToString()
            If Not SendSMS(message, contactNumber) Then
                MessageBox.Show("Problem occured while sending SMS", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End If
    Next
End Sub

Private Function SendSMS(ByVal message As String, ByVal number As String) As Boolean
    'Send SMS Code that return the status.
    Dim status As Boolean = False
    Return status
End Function