exterminator
May 23rd, 2009, 06:13 AM
( Visual C# 2008, .Net framework 3.5 sp1)
The title line should be explanatory but still the details:
1. I have a DataGridView control on a Window Form.
2. AutoGenerateColumns property set to False.
3. Columns added in design time and each columns' DataPropertyName set to specific column name in DataTable.
4. DataTable populated as:
DataTable dataTable = new DataTable();
OleDbConnection conn = DBConnection.getConnection();
string ct = @"SELECT column1, column2 FROM table";
OleDbDataAdapter da = new OleDbDataAdapter(ct, conn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.Fill(dataTable);
(Feel free to suggest any corrections in code above, if something looks dodgy)
5. column 2 is supposed to be a boolean/a flag. So, I choose 2nd column in datagridview to be of check-box type type (at design time).
There are 2 problems:
a) What do I need to do so that a value true in DB for column 2 shows check-box as ticked (it isn't as of now while displaying data)?
b) What do I need to do to have the data updated into DB?
a) might help me find answer to b) but currently, I am doing the following for b) which doesn't seem to be working. I came across variety of expcetions that I searched for and one thing led to another but still I seem far from the solution. I will just throw it infront of you to help me how to achieve what I want to. This is just displaying a two column table on the Form in DataGridView and saving any updates on 2nd column value into the DB.
string ct = @"SELECT column1, column2 FROM table";
OleDbConnection conn = DBConnection.getConnection();
OleDbDataAdapter da = new OleDbDataAdapter(ct, conn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
/*
da.UpdateCommand = new OleDbCommand();
da.UpdateCommand.CommandText =@"update table set column2 = ? where column1 = ?";
da.UpdateCommand.Connection = conn;
OleDbParameter p1 = da.UpdateCommand.Parameters.Add("@p1", OleDbType.Integer);
p1.SourceColumn = "column2";
p1.SourceVersion = DataRowVersion.Original;
OleDbParameter p2 = da.UpdateCommand.Parameters.Add("@p2", OleDbType.VarChar);
p2.SourceColumn = "column1";
p2.SourceVersion = DataRowVersion.Original;
*/
da.Update(dataTable);
I commented out some of the code above since it didn't seem to work (propagate the updates to the db). I had added it since otherwise I was getting the exception: "Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information." (There are no keys in table at the moment, and I don't need one too).
Any suggestions how to achieve the above?
The title line should be explanatory but still the details:
1. I have a DataGridView control on a Window Form.
2. AutoGenerateColumns property set to False.
3. Columns added in design time and each columns' DataPropertyName set to specific column name in DataTable.
4. DataTable populated as:
DataTable dataTable = new DataTable();
OleDbConnection conn = DBConnection.getConnection();
string ct = @"SELECT column1, column2 FROM table";
OleDbDataAdapter da = new OleDbDataAdapter(ct, conn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.Fill(dataTable);
(Feel free to suggest any corrections in code above, if something looks dodgy)
5. column 2 is supposed to be a boolean/a flag. So, I choose 2nd column in datagridview to be of check-box type type (at design time).
There are 2 problems:
a) What do I need to do so that a value true in DB for column 2 shows check-box as ticked (it isn't as of now while displaying data)?
b) What do I need to do to have the data updated into DB?
a) might help me find answer to b) but currently, I am doing the following for b) which doesn't seem to be working. I came across variety of expcetions that I searched for and one thing led to another but still I seem far from the solution. I will just throw it infront of you to help me how to achieve what I want to. This is just displaying a two column table on the Form in DataGridView and saving any updates on 2nd column value into the DB.
string ct = @"SELECT column1, column2 FROM table";
OleDbConnection conn = DBConnection.getConnection();
OleDbDataAdapter da = new OleDbDataAdapter(ct, conn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
/*
da.UpdateCommand = new OleDbCommand();
da.UpdateCommand.CommandText =@"update table set column2 = ? where column1 = ?";
da.UpdateCommand.Connection = conn;
OleDbParameter p1 = da.UpdateCommand.Parameters.Add("@p1", OleDbType.Integer);
p1.SourceColumn = "column2";
p1.SourceVersion = DataRowVersion.Original;
OleDbParameter p2 = da.UpdateCommand.Parameters.Add("@p2", OleDbType.VarChar);
p2.SourceColumn = "column1";
p2.SourceVersion = DataRowVersion.Original;
*/
da.Update(dataTable);
I commented out some of the code above since it didn't seem to work (propagate the updates to the db). I had added it since otherwise I was getting the exception: "Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information." (There are no keys in table at the moment, and I don't need one too).
Any suggestions how to achieve the above?