I browsed a lot on StackOverflow and all over the internet but couldn't find anything that could help me. I am putting some information from a SQL database onto a DataGridView, and i am adding an extra column with checkboxes.
private void populatedataGrid()
{
String sql = "SELECT pm.Name, pm.telephone, pm.email, pm.validID, comp.name as `Company` FROM `project managers`as pm JOIN `companies`as comp ON pm.Companies_companyID = comp.companyID where pm.Companies_companyID =" + loginID;
MySqlCommand command = new MySqlCommand(sql, dh.Connection);
try
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = command;
DataTable dbdataset = new DataTable();
adapter.Fill(dbdataset);
BindingSource bSource = new BindingSource();
// DataGridView1 is a different DataGrid, i am working on DataGridView2
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
dataGridView2.DataSource = bSource;
//Method for adding the additional column with checkboxes (ill paste the method below)
addCheckBoxColumn();
//I make it so that only the checkboxes can be edited
foreach (DataGridViewColumn dc in dataGridView2.Columns)
{
if (dc.Index.Equals(5))
{
dc.ReadOnly = false;
}
else
{
dc.ReadOnly = true;
}
}
adapter.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
dh.Connection.Close();
}
}
Here is the method for creating the column with Checkboxes:
private void addCheckBoxColumn()
{
DataGridViewCheckBoxColumn cbCol = new DataGridViewCheckBoxColumn();
cbCol.ValueType = typeof(bool);
cbCol.Name = "Select";
cbCol.HeaderText = "Select";
dataGridView2.Columns.Add(cbCol);
}
And it all works fine for now:
I created a button for testing purposes, which i want when clicked to write the number of rows that have a checked checkBox on label2.
private void button1_Click(object sender, EventArgs e)
{ int counter = 0;
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (Convert.ToBoolean(row.Cells[5].Value))
{
counter++;
}
}
label2.Text = counter.ToString();
}
When i run it and click Button1 i get the following Exception: "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: String was not recognized as a valid Boolean."
Can you give me some help on how to fix this. I wrote a similar thread half an hour ago when i had a different problem but it got Duplicated so I tryed to change my code and now i get this.
Aucun commentaire:
Enregistrer un commentaire