Combobox displays duplicate items when any item is selected overwiting another item - c#

I am doing a winforms project with C# as code behind in VS 2010.
I have a combobox which is populated from a db table using tableadaptor.fill
Each time I select any value in this combobox, the selected value overwrites another existing item in the combobox and therefore appears twice. As shown below:
Here, I click combobox and select LEP 2013
Now when I click again on the combobox, LEP 2013 overwrites SFT 2013 and appears twice.
Also, there is no code written on the selection of the item in the combobox, only the Fetch button does the next action. This behavior occurs even before the Fetch button can be clicked.
Another observation is that when I select the first item in the combobox, in this case SFT 2013, it will not duplicate the item.
This question is similar to
Combobox displaying duplicate items
and
Combobox displays duplicate items when an item is selected
But their solutions don't work for me. The column of the table that this combobox is bound to, is the primary key of that table, so it will not contain duplicate values.
Any suggestions ?

I removed the existing data binding for the combobox and rebound using a new adapter and the issue went away.
However I still don;t know what the earlier issue was. Somehow, the first item of the combobox's Text was getting replaced by the SelectedItem's text.

I had the same problem except I was using a datatable to populate my combobox. I put dataTable.Clear(); at the beginning of my procedure to clear the table and it solved the problem. You should check your data sources to make sure they are cleared before reloading data into them.

Related

Make WPF DataGrid create new item only after full-row commit

The code I am working on uses a WPF DataGrid to store a table of entries, bound to an ObservableCollection of items, with the possibility for the user to create new items using the blank row at the bottom of the table.
When the user selects a cell in the new row, right as they type their first keystroke, the DataGrid creates a new blank item and inserts it in the ObservableCollection. Once the user hits Enter or takes focus away from the cell, the new text is committed as an edit to the item.
Is there any way to change the behavior of the DataGrid so that it does not create a blank item before the user is done typing? Or, at least, does not add the blank item to the ObservableCollection until after the row is committed.
I'd like to make it so that new items are only added to the ObservableCollection after a full-row commit has occurred, meaning that each new item added to the Collection will already have data in it, instead of being inserted blank and then edited later.
I've searched high and low for an answer, but it appears that no one else is having this problem.
The reason why I want this functionality is that I am implementing an "Undo" feature in this DataGrid, but when new rows get added as blanks and then edited later, that always ends up counting as two changes, meaning the user needs to perform two "Undo" operations to remove a row they have just barely created. And that's not intuitive at all.
Thanks.
In general it's normal behavior of DataGrid.
DataGrid calls Add() for the new collection item when you start editing the row
DataGrid automatically calls Remove() for the item when you hit Escape key or call CancelEdit() in any other way
There's nothing strange to support (ignore in code) collection items with blank/default values. As alternative you may add some proxy collection and commit its changes to main collection when Datagrid saves the Row data. As for me, first way is preferrable.

swapping of columns in a data grid view

i have a c# windows form application, in all tabs inside it, i have a data grid view that display data of a punch machine(ID,IP,Port,Type,Description,Location)
there's no errors, but the problem is sometimes the ID is shown instead of the Description Field,knowing that the ID is hidden in the grid..
So what's the possible problem?
the grid view is filled from a data set through a binding source.
all the grid has the same binding source, all are the same,
and i load the data on the form load.
I have seen that before. I don't know what causes it. It seems to happen only if you hide the first column. I don't know if this is the right way to fix it but I moved the ID column away from the first column. In my case, I put it at the end of the dataGridView and I didn't see this "bug" again.
Hope it helps.
the solution was in a combo box that are binded to the same binding source of the grid..i was filled the "Selected Value" field which is wrong..so simply remove the selected value from the combo box

Dropdown list selected item always set to default value

I wish to pass values of the selected items into a database and however noticed that the selected item is not what is sent into the database you can see in the snap shot below.
during run time the following value is recorded.
Where did it all go wrong with the dropdown lists selected item?
Counting on your intelligence...thanks.
Put your call to load the dropdownlist in
if (!Page.IsPostBack)
{
//LoadDropdownListHere();
}
Try DBNull.Value instead of null.
Edit:
I think you need to specify the type using this overload: http://msdn.microsoft.com/en-us/library/cc491445.aspx
CSMDataSource.InsertParameters.Add("CaseID", DBType.Int32, CaseIDDropDownList.SelectedItem.Text);
I found the problem for the dropdownlist always selecting the first value. It appears the dropdown list is always rebinded anytime i click a button that requires a post pack as such the data tha was binded at page load re binds a gain before the selected item gets picked as such the default first value gets picked all the time. to solve my problem i first disabled enable auto postback on the dropdownlist and in the code behind that is my .cs file during the page load, where i first binded the data to the dropdown list, i used a condition like if(!ispostback) to bind my data. what this does is when the page loads first time the dropdownlist is binded and if i should select an item from the drop down list, the "auto post back" that i disabled earlier on keeps the selected item selected and when i click and button that requires a post to the server, the (!ispostback) prevents the dropdownlist to be binded again before selection. so in effect, the selection is done first and afterwards if the page should load anew, the drop down list is binded again.
i was in a bit of a rush while typing so please bare with my typo errors. do call in anytime for more clarification...peace

C#/WinForm: Force DataGrid not to add duplicate entry in a row

Is there are a way to 'validate' the contents of my data grid in windows form whether it is a duplicate copy or not?
I have a combo box inside my data grid and what I want to do is every time I add a new row, the user selects an item from the combo box. The next time the user adds a new row with the same entry from the previous ones (duplicate), it will not be added to the row.
I was thinking of removing or disabling the item from the combo box (datasource from the database) upon adding to the new row, so the user will not be able to duplicate the record.
What would be the best approach on this problem? Comments and suggestions are welcome. Thank you.
You are planning to do the right thing. Removing the item from the combo is not a bad idea. Or else you can do a validation at the time of adding a new row to check if the item is already there in the grid.
Since the data source for your combo box is from a database why don't you just modify the query not to include items that are in the rows of your table, something like
SELECT item FROM itemList WHERE item not in (SELECT item FROM userAddedRow)
Instead of Going to Database u can perform your check operation in your Dataset.
Perform Check operation From Dataset and if not Exist then send the Insert statement
to the DataBase.

How to add a new row into datagridview using grid only. i.e by showing textboxes and add button in WinForm

I want to add a new row to a datagridview such that when a user clicks on new button , a new is is generated with few textboxes and combo boxes and after filling up all the details he save the info by clicking on save button.
EDIT
I want to do it like it is seen in gridview(Template Fields) in asp.net
I am looking for same kind of functionality.
Since I'm not sure exactly how you want to achieve this.. I would have a hidden panel with your text boxes, ...etc, show the panel when the new button is clicked. After all information is entered into the fields, click the save button. Assuming that you will be inserting this information into a table, after the row is inserted, call the stored procedure to get the desired records from the table being displayed in the grid.
Assuming your _dataGridView.Columns collection is not null and contains a template of the rows you wish to add, it is as simple as something like this:
foreach(var item in _collection)
{
_dataGridView.Rows.Add(item.Foo, item.Bar);
}
In order for this to work, you will have had to design your Columns collection in the VisualStudio designer or programatically add DataGridViewTextBoxColumn objects to the Columns collection.
In the example above, I added two DataGridViewTextBoxColumn objects to the _dataGridView.Columns collection and then populated the datagrid from a List of my object that contained a 'Foo' and a 'Bar'.
EDIT
Have you checked out the DataGridView FAQ? The information about using the DataGridView in unbound mode may help you.
HTH

Categories

Resources