I'm creating a list to add the items from the first column if the checkboxcolumn button is checked in that row. When the message box pops up asking if I'm sure if I want to delete, the values it displays is "DataGridViewRow {Index = 0}" instead of the value of the first column. Is there a type conversion I'm missing?
private void button1_Click(object sender, EventArgs e)
{
List<DataGridViewRow> selectedRows = (from newRow in DGV1.Rows.Cast<DataGridViewRow>() where Convert.ToBoolean(newRow.Cells["Delete"].Value) == true select newRow).ToList();
var message = string.Join(Environment.NewLine, selectedRows);
if (MessageBox.Show("Are you sure you want to delete users:\n" + message, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
for (int x = 0; x < DGV1.RowCount - 1; x++)
{
if (Convert.ToBoolean(DGV1.Rows[x].Cells[0].Value))
{
foreach (DataGridViewRow newRow in selectedRows)
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM JoshUserTable WHERE UserID = @UserID", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UserID", newRow.Cells["UserID"].Value);
cmd.ExecuteNonQuery();
DGV1.Rows.Remove(newRow);
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire