dataGridView checkbox

Asked By Esmail
03-Sep-10 12:17 AM
Earn up to 0 extra points for answering this tough question.
This is my code where i want to delete the selected checkbox row

private void button3_Click(object sender, EventArgs e)

{

for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)

{

if (dataGridView1.Rows[i].Cells[0].Value != null)

{

if ((bool)dataGridView1.Rows[i].Cells[0].Value == true)

{

OleDbConnection con = new OleDbConnection();

con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\MMCLDISPATCH.mdb";

con.Open();

string sql = "delete from DispatchTable where ID='"+i+1+"'";

OleDbDataAdapter da = new OleDbDataAdapter(sql, con);

OleDbCommandBuilder cmd = new OleDbCommandBuilder(da);

DataSet ds = new DataSet();

da.Fill(ds);

dataGridView1.Rows.RemoveAt(i);

}

}

}



I am getting this error:

Data type mismatch in criteria expression.
on this line:

da.Fill(ds);

  re: dataGridView checkbox

Reena Jain replied to Esmail
03-Sep-10 01:21 AM
hi,

use this

"delete from DispatchTable where ID='"+dataGridView1.Rows[i].Cells[your cell column number where id is exist].Value+"'"
in place of

string sql = "delete from DispatchTable where ID='"+i+1+"'"

hope this will help

  re: dataGridView checkbox

Esmail replied to Reena Jain
03-Sep-10 02:14 AM
same error is coming

  re: dataGridView checkbox

Reena Jain replied to Esmail
03-Sep-10 02:35 AM
hi,

use the following code because with delete command dataadapter won't work

string sql = "delete from DispatchTable where ID='"+dataGridView1.Rows[i].Cells[your cell column number where id is exist].Value+"'"
 OleDbCommand cmdLocal11 = new OleDbCommand(sql, LocalCon);
                    if (LocalCon.State == ConnectionState.Closed)
                      LocalCon.Open();
                    cmdLocal11.ExecuteNonQuery();

try this it will work
  re: dataGridView checkbox
Reena Jain replied to Esmail
03-Sep-10 02:36 AM
hi,

use the following code because with delete command dataadapter won't work

string sql = "delete from DispatchTable where ID='"+dataGridView1.Rows[i].Cells[your cell column number where id is exist].Value+"'"
 OleDbCommand cmdLocal11 = new OleDbCommand(sql, LocalCon);
                    if (LocalCon.State == ConnectionState.Closed)
                      LocalCon.Open();
                    cmdLocal11.ExecuteNonQuery();

try this it will work
  re: dataGridView checkbox
Esmail replied to Reena Jain
03-Sep-10 04:33 AM
i have tried this also same error is coming
  re: dataGridView checkbox
Reena Jain replied to Esmail
03-Sep-10 06:56 AM
hi,

Might be the cell number you are giving is wrong, count the cell number with 0 and put the appropriate cell number where id is reside.
Create New Account