Hi Everyone,
I was working with DataGridView tool in Visual Studio. I have managed to display data from my database in DataGridView. Now I have to Select some certain rows and store them in such a way, so that I can Use a loop on them and process each row one by one.

Basically I have to select Phone Numbers of Contacts and SEND SMS to all the selected contacts.

Please help. Thanks

You can use the datagridview.SelectedRows property to retreive a collection of the currently selected rows.
Alternatively, you can add an unbound CheckBox column to the datagriview. Then itereate through all rows to check the value of teh checkbox column.

This is fine, But how would i process the Selected Values, Absolutely I have used SelectedRows property, but don't know how to process these values, can you please specify a bit of code.

I have to call a function SendSMS(port,phone_number); in every iteration, and I have to use the value of selected row's column(number) as argument to phone_number

This is fine, But how would i process the Selected Values, Absolutely I have used SelectedRows property, but don't know how to process these values, can you please specify a bit of code.

I have to call a function SendSMS(port,phone_number); in every iteration, and I have to use the value of selected row's column(number) as argument to phone_number

That depends on how you are itereating through the SelectedRows.
If you are using a for loop (ie for(x = 0; x<SelectedRows.Length; x++) then you need to use the counter of the loop as an index for the SelectedRows collection.
If you are using a foreach loop (ie foreach(DataGridViewRow row in dgv.SelectedRows) then you will have a GataGridViewRow item that represents the selected row at each iteration.

Once you have access to the current row you can access the rows cells (ie row.Cells["number"].Value or dgv.SelectedRows[x].Cells["number"]

Here's an excerpt from one of my programs - maybe this will help also.

log.LogMessageToFile("---> Laptop checkout started <---");
DataGridViewRow row = dataGridView_Laptops_Available.SelectedRows[0]; //get selected row
int laptop_id = Convert.ToInt32(row.Cells[0].Value);  //use primary key to determine item
log.LogMessageToFile("Laptop ID set to " + laptop_id.ToString());

Form_CheckOut form_CheckOut = new Form_CheckOut();
form_CheckOut.Owner = this;
form_CheckOut.ShowDialog();

if (form_CheckOut.DialogResult.Equals(DialogResult.OK))
{
    log.LogMessageToFile("Checking out Laptop ID: " + laptop_id.ToString());
    log.LogMessageToFile("Updating Database");
    db_update.Laptop_Checkout(laptop_id, form_CheckOut.due, form_CheckOut.checkout, form_CheckOut.user, true, Environment.UserName, form_CheckOut.notes);
    db_update.cmd.ExecuteNonQuery();
    log.LogMessageToFile("Database Updated - Laptop ID: " + laptop_id.ToString() + " has been checked out");

    _laptop.setData(db_update, laptop_id);
    xl.UpdateFile(_laptop, true);

    Populate_Laptops();
}
This article has been dead for over six months. Start a new discussion instead.