C# .NET - dataGridView checkbox
Asked By Esmail
03-Sep-10 12:17 AM
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);
Reena Jain replied to Esmail
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
Esmail replied to Reena Jain
Reena Jain replied to Esmail
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
Reena Jain replied to Esmail
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
Esmail replied to Reena Jain
i have tried this also same error is coming
Reena Jain replied to Esmail
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.

run? (B) What is reflection? (A) What are different types of JIT? (B) What are Value types and Reference types? (B) What is concept of Boxing and Unboxing ? (B) What is Sliding expiration? (I) What is cross page posting? 93 (I) How do we access viewstate value of this page in the next page ? (I) Can we post and access view state DB provider. In ADO.NET you communicate with the database through a data adapter (an OleDbDataAdapter, SqlDataAdapter, OdbcDataAdapter, or OracleDataAdapter object), which makes calls to an OLE DB provider or the we use to connect to a data source via an OLE-DB provider, such as OleDbConnection, OleDbCommand, etc. These objects inherit from the common base classes, and so have the same properties of connection object ? They are used to connect a data to a Command object. An OleDbConnection object is used with an OLE-DB provider use of dataadapter ? These are objects that the DataSet, or pushes the changes in the DataSet back into the data store. > An OleDbDataAdapter object is used with an OLE-DB provider > A SqlDataAdapter object uses Tabular Data Services New SqlCommand(“SelectByEmployee”) objCommand.Parameters.Add( “@FirstName ”, Data.SqlDbType.NVarChar, 200) objCommand.Parameters.Item( “@FirstName”).Value = txtEmployeeName.Text.Trim() End If In the above sample not much has been changed only
showing) the values I copied into them. . . The other problem is, that I use a OleDbCommandBuilder to update the data in the database, and now, with this changes, I don't can populate the data from the database to my DataGridView, and I later use an OleDbCommandBuilder to save user changes back to the database. In addition, I want my DataGridView to other question is, Will I still be able to use the UpdateCommand created by the OleDbCommandBuilder to update the data in the database? Any help will be appreciated. Thanks in advance, Aldo. public frmGrid() { InitializeComponent(); System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider = . . . \ Db1.mdb; Jet OLEDB:Database Password = xxx;"); System.Data.OleDb OleDbCommand oleDbCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM Customers;"); oleDbCommand.Connection = conn; ds = new System.Data.DataSet(); daOleDb = new System.Data
running the form, the DGV shows the data, (3 rows, 3 cols). -. I change the value in row 1, col 2 = > nothing happens. -. I change the value in row 1, col 2 and row 2, col 2 = > Change in database only for row 1, col 2 -. I change the value in row 1, col 2 and row 2, col 2 and row 3, col 2 the database. . . Thanks in advance for any help, Aldo. public frmGrid() { InitializeComponent(); System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D: \ Visual Studio 2008 \ Projects \ . . .Db1.mdb; Jet OLEDB:Database Password = x;"); System.Data.OleDb.OleDbCommand oleDbCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM Customers;"); oleDbCommand.Connection = conn; / / DataSet, OleDbDataAdapter, OleDbCommandBuilder were declared as static. ds = new
private const String PARAMS_FAILED = "Could not attach parameters." ; private static Type[] _connectionTypes = { typeof (OdbcConnection), typeof (OleDbConnection), typeof (OracleConnection), typeof (SqlConnection)}; private static Type[] _dataAdapterTypes = { typeof (OdbcDataAdapter), typeof (OleDbDataAdapter), typeof (OracleDataAdapter), typeof (SqlDataAdapter)}; private static Type[] _parameterTypes = { typeof (OdbcParameter), typeof (OleDbParameter), typeof (OracleParameter), typeof SqlParameter)}; private static Type[] _commandTypes = { typeof (OdbcCommand), typeof (OleDbCommand), typeof (OracleCommand), typeof (SqlCommand)}; private IDbConnection _connection; / / connection used by the class private IDbTransaction _transaction Dispose(); } } / / Dispose / / / <summary> / / / Gets / sets the provider type to use for the data access. This value is an / / / enumerated list / / / < / summary> virtual public ProviderType Provider { get { return _provider; } set { _provider = value; } } / / Provider / / / <summary> / / / Gets / sets the connection string to use for the connection / / / < / summary> virtual public String ConnectionString { get { return _connectionString; } set { _connectionString = value; } } / / ConnectionString / / / <summary> / / / Gets / sets the timeout for the command / / / < / summary> virtual public int CommandTimeout { get { return _commandTimeout; } set { _commandTimeout = value; } } / / CommandTimeout / / / <summary> / / / Gets / Sets the connection to use for data access / / / < / summary> virtual public IDbConnection _connectionString); _connection.Open(); } else if ( _connection.State ! = ConnectionState.Open) _connection.Open(); return _connection; } set { _connection = value; } } / / Connection / / / <summary> / / / Gets / sets the transaction to use for the operations / / / < / summary> virtual public IDbTransaction
OleDbDataAdapter Fill hangs using MS Access Hello. I am having a hanging problem with the following block. However, sometimes the LoadTbl method will never return hanging indefinitedly. Under the hood, the OleDbDataAdapter.Fill is being used to load the disconnected dataset which is the item that hangs 3 threads of msjet40.dll (version 4.0.8618.0) come and go each time OleDbDataAdapter.Fill is called. When the hang occurs, the 3 jet threads appear, but are never will persist.< / param> public MSAccessOleDbPM(string databaseNameValue) { this.databaseName = databaseNameValue; this.lastErrorMsg = ""; this.dbConnect = new OleDbConnection(ConnectionString(false)); } / / / <summary> / / / Opens a database connection. / / / < / summary> / / / <param name = "exclusive"> True if you require false; } bool success = false; / / assume failure try { if(sql = = null | | sql = = "") { sql = "Select * From " + tblName; } OleDbDataAdapter adapter = Adapter(sql); adapter.Fill(dataSet, tblName); success = true; } catch(Exception e) { this.LastErrorMsg = e public bool ExecuteNonQuery(string sql) { bool success = false; / / assume failure try { if(this.OpenDb(false)) { OleDbCommand cmd = new OleDbCommand(sql, this.DbConnect); cmd.ExecuteNonQuery(); success = true; } } catch(Exception e) { this.LastErrorMsg = e.Message; } finally returns> public bool DropTable(string tableName) { bool success = false; / / assume failure if(this.OpenDb(false)) { OleDbCommand dropCmd = new OleDbCommand("DROP TABLE " + tableName, this.dbConnect); try { dropCmd.ExecuteNonQuery(); success = true; } catch(InvalidOperationException