By Greg McGuffey - 12/4/2006
I have a DataGridView that is loaded with data from a readonly SQL view (actually via a SF BO). However, I do need to allow edits. I'd like to just react to the edit event, do my own thing (update data myself), cancel the editing that the datagridview attempts (which will fail), then update the gridview so it shows the new data. I'm sure this is easy, but I can't seem to get a handle on it. Ideas?
|
By StrataFrame Team - 12/4/2006
If you want the business object to be able to copy data back into the database, you can create stored procedures to handle the inserting/updating/deleting of records from the business object. Most likely, you'll want to use the GetDataTable() method of the business object rather than the FillDataTable and programmtically set the return value of the method to the DataSource of the DataGridView. This way, you can circumvent the BusinessBindingSource and business object and just work with the data within the DataTable.
|
By Greg McGuffey - 12/4/2006
If you want the business object to be able to copy data back into the database, you can create stored procedures to handle the inserting/updating/deleting of records from the business object
This would be to update all columns in the BO, which is based on the view right? I don't think that is what I want here, but its good to know.
What I really want to do is just run a certain stored procedure when the use selects a value in a combo box in the DataGridView. The data in the view is just to help the user select a value in that one column. I can't figure out what event might work here. Any ideas?
|
By StrataFrame Team - 12/4/2006
Are you trying to figure out how to populate the combo box from data? or are the values static?Are you just trying to figure out what event to handle so that you can run your sproc? I believe you'll want to use the CellValueChanged event for that. The event args will tell you the cell that changed, and you can use the value of that cell to run your sproc.
|
By Greg McGuffey - 12/4/2006
Ben Chase (12/04/2006) Are you trying to figure out how to populate the combo box from data? or are the values static?
Are you just trying to figure out what event to handle so that you can run your sproc? I believe you'll want to use the CellValueChanged event for that. The event args will tell you the cell that changed, and you can use the value of that cell to run your sproc.
The combobox population is no problem, I'm doing it manually as there are only three items to choose form.
OK, here are the problems I'm running into. If the column with the combobox is bound, then it tries to update the underlying datasource, which is no good (its readonly). I've tried adding another column, made IT the combobox, turned on VirtualMode for the grid and load it based on the data form the view (using CellValueNeeded), which is now in a hidden column. When I do this the CellValueChanged runs much latter than I'd like, like when I move to a new row or exit the form and it only runs because the value is being changed BACK to the original...sigh.
Let me provide more specifics:
The view is something like:
Select s.State, m.Info, m.masterID From tblMaster as m Left Outer Join tblSpecific as s on m.masterID = s.masterID
I'm trying to update s.State with the grid
I've a BO to this view, I add the BO, a BusinessBindingSource, A DataGridView to the form, then set the bindingsource of the grid to the BusinessBindingSource. I hide the State and MasterID columns, add a new column that is combobox column and set the grid to VirtualMode=true. I then load the unbound column to the State column in the CellValueNeeded event of the grid.
What I want to have happen is when the users selects a value from the combo, I grab that event and run a sproc. Hope this is clearer.
|
By Greg McGuffey - 12/4/2006
OK, I've figured out how get the stored procedure to run...CellValuePushed is the trick...it didn't work earlier because I hadn't wired up my procedure to it!
Now, I am having significant problems updating the datagridview to display the new value. If I just do the sproc, then it just looks up the value in the hidden column, and displays that (the value in the db IS changed though). I tried refilling the BO, but that put me in an infinite loop (it somehow fires the CellValuePushed event, which then tries to refill BO, which fires CellValuePushed event...)! I tried clearing the BO, but that reran the stored procedure (I'm not making this up...really).
So, do does one update a datagridview to have it show the latest data.
I really wish the app I was building has at least one simple bloody form for me to build
|
By Greg McGuffey - 12/4/2006
I also tried setting the DataSource to a datatable and I get an error, something about reenterant call to SetCurrentCellAdressCore function. This is all in the CellValuePushed event handler.
|
By StrataFrame Team - 12/5/2006
You might have to do something like change the bound column in the CellValuePushed event handler... so that the column you just used to select the sproc is no longer bound to the grid... I don't know.
|
By Greg McGuffey - 12/5/2006
If I set the datasource of the grid to a datatable retrieved from a GetDataTable function in the BO, is the table still linked back to the source data?
If it isn't, then I could change the bound column, but the change wouldn't attempt to propagate back to the db.
|
By StrataFrame Team - 12/5/2006
No, if you bind the return value of the GetDataTable to the grid, the grid will not be bound to the business object.
|
By Greg McGuffey - 12/5/2006
OK, I got it to work!
I used a DataGridViewButtonColomn. The rules involved were too complicated to just allow the user to select a value anyway. In the CellContentClick event handler, I can call the stored procedure (via a BO of course), and then update the BO no problems.
If I had to allow the user to select a value, I'd probably try configuring the readonly BO to allow updates via a stored procedure.
That was a tough one! Thanks!
|
By StrataFrame Team - 12/5/2006
Not sure that I helped much, but I might have made you look in a few new directions... glad you got it working
|
|