handling lookup tables with deleted records and databound controls - c#

I have a table Resource with a field Type. Type is a lookup into the table ResourceType.
So for instance ResourceType might have:
1: Books
2: Candy
3: Both
And Resource might have
1: Tom's Grocery, 2
2: Freds News, 3
It would display as: Tom's Grocery Candy
Now lets say I am using a databound combobox for the resource type and the third record is deleted from ResourceType, we of course get an error when Fred's News is displayed. I could simply put a marker in (perhaps an asterisk), indicating that it has been deleted, rather than actually delete it. It shows up as **Both* in the textbox portion of the combo and I am content.
However, I would not want it to show up as an option in the dropdown. Is this too much to ask from databound fields? Must I write my own code to load the drop down?

Add a bit Deleted column to the lookup table. When you delete a type, set Deleted = 1. When you pull back ResourceTypes, only pull out ResourceTypes where Deleted = 0 and then bind to the dropdown.
Edit:
How are you getting the dataset that you're binding to the dropdownlist? Are you using drag and drop datasets? I really haven't worked with datasets like that in years, but I'm pretty sure you can change the Get sql to what you need it to be.

OK, I already have most of that built into the solution - but I must be binding wrong. If I only pull back records where deleted = 0, I get exceptions - I'm guessing because the textbox can't be filled from the record set.
I guess I'm not sure how to bind the textbox display to one dataset - the one with all the records - and the dropdown to a filtered dataset.

Simply adding a deleted column was not enough - there had to be a way to see a deleted record in the text portion of a combo box while at the same time filtering out deleted records in the drop down.
In the end, I wrote a custom user control to handle this.

Related

How do I set the content of a label to a database table cell in WPF?

I have built a program in WPF using Visual Basic that allows users to select from a series of items. The list of items may change as I add more items or remove older ones. Originally, I would just add and remove these items in the project code, and rebuild the executable.
Over time, however, the list of items grew and became tedious and impractical to maintain in the code. It occurred to me that it would be easier to store the items in a database table which the program can read, and the list can just be updated by adding and removing items from this database.
I have the database built, and I've added it as a Data Source. I have the data connection set up and everything is ready to go.
What I need to know is how to set the Content property of a label to a cell from a table in the database. I want to do this in the codebehind, not in markup (XAML).
My guess is that I will need to set up a sort of query with a for loop to find the cell I want.
The name of the database is ItemsDB. It contains a table called ItemsTable, and the fields in the table have a unique ID (key) with an AutoNumber data type. The column containing the data I want is named ItemLabel.
So, to sum it all up, I want a label to show the content from a single cell in the database, specified in the codebehind. Either C# or VB is fine.
EDIT
I guess I should've mentioned that the labels themselves actually function as buttons, and I don't really want to display a ListView if I don't have to. I know it's not helpful to not have any code posted, but I don't even know where to start, so there's no code to post.
Took a while longer than I expected, hope this is still useful. So, what you want to do is iterate through a collection and create a new Label for each row.
You will need a container for the Labels, I used WrapPanel, but you can use any Panel you want. I'm replacing your Data Source with a simple DataTable, you'll get the point from this:
foreach (DataRow row in YourItemsTable.Rows)
{
Label newLabel = new Label();
newLabel.Content = row["ItemLabel"].ToString();
// If you need some events, attach the handlers here
yourParentContainerPanel.Children.Add(newLabel);
}
This should be all you need.

Is two data sources for a DataGridView.ComboBoxColumn possible?

I have a DataGridView with several TextBoxColumns and one ComboBox column called 'combo' that holds the client type. The problem is that I'd like to show both the currently selected client-type value along with the dropdown client-type list to validate future changes by the user. In SQL Server, I have a DB with two table columns, 'client_type_dropdown.name' and 'clients.client_type'. The 'client_type_dropdown.name' column is a validation list. The 'clients.client_type' column contains the current client type for clients in the database. Is there a way to show in 'combo' both 'client_type_dropdown.name' and 'clients.client_type' , i.e., one source for the ComboBoxColumn dropdown and a different source for the textbox part of 'combo'? Or do I need to have two columns in my grid?
I appreciate your help.
I'm using a third party grid, but I usually handle this by setting the combo drop down style to DropDown instead of DropDownList. This will allow your original database value to display, even if it isn't in the list.
This also allows free typing of values into the combo field, so the trick after that is to validate the user input to make sure it matches a value in the list before you allow them to save updated values. You could play around with the LimitToList property of the combo to possibly save you doing the validation manually, but with most controls I have worked with it will give you more grief than help.

Binding gridview from sql table but this binding not for particular table it keeps on changing depending on user input

I have a table called TableExplorer which contains other table's names and their respective column names.
For example: table Customer may have 5 columns but in TableExplorer I may mention only 2 column names out of 5.
User send me the table name in query string, my job is to find that table name whether it is present or not in TableExplorer which I mentioned initially. If the user mentioned table is present then I should bind that table to a gridview and that gridview should have functionality like edit, delete, update. Finally any operation like edit, delete or update made by user should reflect in the respective table as mentioned by the user.
Database used : SQL Server 2008 R2
Programming language : C#, ASP.NET
Thanks in advance for helping.
Are you using WebForms? I'm far from being a WebForms expert but this is what I'd do:
You create different pages with GridView control, one for each table that you might want to bind, eg. Customer table. You create the CRUD operation for this GridView, Insert, Update, Delete. You create parameters for all the columns that the user might need, some of them could be invisible.
The user goes to his selection page, he choses a table and a list of columns and press Submit his request. Whenever you receive the query string you do 2 things:
Identify what table your user is asking and load the right page, if available. Maybe you can load only the specific GridView control in a specific using Ajax. Or maybe you can simple load another page.
Get the list of columns that the user wants to see in his grid from the querystring. Before showing the grid to him you keep these columns visible and hide/remove from the gridview every other column. You must pass this parameter to your insert/update methods as well.
I think this is a good solution to start, assuming you don't have a list of hundreds of tables, I wouldn't try to create something completely dynamic.

DataGrid problems. Using DataSet.Merge with "Edit Template"

I am trying to debug an issue with a WinForm application.
The application uses a DataGridView bound to a DataSet.
The user can select a row and click "Edit" and an Edit Form appears so the user can edit the row and save his changes.
If the DataGridView is sorted by one or more columns and the user edits a row more than once and changes that rows position in the DataGrid (by changing one of the sorted columns TWICE), the DataGridView gets out of sync and I get Exception errors.
Here is how the code is written:
When the user clicks a row and hits edit (the user may only select one row), that row is passed to the EditFom. The edit Form creates an empty DataSet and loads that Row. When the user is done, the calling form uses DataSet.Merge to merge those changes back into its DataSet.
Here are a couple of scenarios that tell me it is womething to do with the "merge" and sorting:
(Note, I never change the actual "sorting", I am talking about changing the value of one of the sorted by columns in one of the rows.)
If the user edits a row directly in the Grid and changes the value of one of the columns being used to sort, then changes it again, etc. The row moves around the Grid, like it should and when the user clicks save, everything saves fine.
If the user edits a row (directly in the Grid OR via the "template") and doesnt change any of the "sort" columns, just makes an edit (therby setting the "State" of the row to Modified, right...), THEN the user edits the row using the Template (which goes through the seperate DataSet and merges the data back when done), and changes one of the columns being sorted (when done, the grid should resort and the row should "move"), the row doesn't get REPAINTED, it stays in the same position in the grid that it was in before the template edit. (Although, the data saves fine and you can refresh the grid and it sorts properly.) Evidently, the DataGrid isn't being notified that the RowChanged in this scenario (or it is, but it isn't doing anything about it).
If the user edits a row directly in the Grid, change a sort column (the row moves), then edits the row using the template and changes that column again, we get the same results as 2, above. The data saves fine, but when the user leaves the edit template, the grid doesn't resort.
(This is the one that causes the Exception error.)
If the user edits a row using the template and changes a sort column and leaves the template, the datagrid resorts and the row moves properly. Then, if the user edits the SAME row using the Template Edit and changes the sort column AGAIN. When the user leaves the edit template, the datagrid doesn't resort and when the user saves the data and the grid refreshes, we get an error:
System.InvalidOperationException: DataTable internal index is corrupted: '5'.
And sometimes, we get an error complaining the primary key is missing.
Any advice on how to "debug" this? I feel like it has something to do with the "internediate" dataset the edit template uses and the "Merge". It is like the DataGrid datasource is getting out of sync with the DataGrid Rows.
I should have done some Google'ing before posting this. It seems to be a common error and I found a HUGE post about this on the MS site. Unfortunately, I haev tried most of their suggestions, but most likely I will find my answer there. I just wanted to make a note of this, in case anyone is interested:
http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/18544cd3-1083-45fe-b9e7-bb34482b68dd

DataGridView binding after replacing column with comboboxcolumn

I'm in C# Express 2010 and SQL Server Express 2008, making a winforms front end for simple table editing. User picks a table from a combobox, and a datagridview is populated with that table. There's a Submit and Reload button at the bottom of the form. It's loosely based on Microsoft's example for binding a DGV to a database.
So, I have this datagridview that is populated by a data adaptor. After it's been populated, I go through the resulting table and replace any columns with foreign keys with comboboxcolumns so it's easier for the user.
This bit all works fine. The comboboxcolumn shows up, with the correct data in it etc etc. I've set the Headertext, DataProprtyName, and Name properties to match the column it has replaced.
It DOES seem to have broken my 'submit' button that does the update command. Basically, if I change the value in one of the comboboxcolumns, I get a concurrency violation. If I change a value in any other column, it silently fails (when the data is reloaded into the DGV, the updated value vanishes).
Any ideas what the problem could be here?
If you need to see code just let me know. There's rather a lot of it as I'm a newbie and have probably done things in a horrendously messy way!
Thanks in advance.
Never mind - I figured it out - I had copy/pasted a procedure and forgotten to create a new DataAdaptor, so it was using the same one as the one used to populate the DGV, hence things went A over T...

Categories