|
-
May 23rd, 2009, 06:13 AM
#1
DataGridView bound to DataTable (display & update problem with checkbox column type)
( 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:
Code:
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.
Code:
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?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|