ComboBox showing System.Data.DataRowView after changing selection - c#

I have been looking at all the other questions similar to this one and they just don't seem to help me with my particular problem.
I am using a Combobox with the following properties:
The purpose of the Combobox is simple, it is taking all the values of an unique column in a table, and present them as options. The column's name is "nim".
Upon initialization, the combobox loaded just fine:
The problem occurs after I changed the selected item to the 2nd one in the list, and tried changing it again:
When I tried selecting System.Data.DataRowView, this error appeared:
I have been playing around with the code to no avails. I didn't write any code concerning the combobox. I just assign the DataSource, DisplayMember, and ValueMember from the properties window manually.
The only code concerning combobox is these:
private void comboNIM_SelectedIndexChanged(object sender, EventArgs e)
{
//selectedNIM = ((DataRowView)comboNIM.SelectedItem).Row["nim"] as String;
selectedNIM = comboNIM.SelectedValue.ToString();
}
Any help will be really appreciated! Thank you!

So..., I have found a solution for this particular problem.
I deleted the ComboBox, and then created a new one. Then I just assign the properties programmatically.
cb.DisplayMember = 'nim';
cb.ValueMember = 'nim';
cb.DataSource = mahasiswaBindingSource;
Apparently, leaving the properties window unedited solved the problem!

Related

Get value from selected combox and put it on textbox c#

Mabuhay!
Hi! I search this code here but unfortunately didnt get what I am looking for. Any help on this?
I got this form and have a combo box which i uses dataset so I can get the value of description on this table. And I have a textbox after that but I wanted to get the value of labor cost based on what I selected on combo box.
heres for my combobox
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the '_10daliriPayrollDataSet.Description' table. You can move, or remove it, as needed.
this.descriptionTableAdapter.Fill(this._10daliriPayrollDataSet.Description);
}
for my laborcost.Text value
private void description_SelectedIndexChanged(object sender, EventArgs e)
{
laborcost.Text = description.SelectedItem.ToString();
}
I got this error;
System.Data.DataRowView
Any help on this?
Thanks,
Chris
try
description.SelectedItem.Text this will do
The correct way of retrieving the item text is the GetItemText method.
It works for any item, including the selected one. The later can be retrieved from the SelectedItem property.
In your case
laborcost.Text = description.GetItemText(description.SelectedItem);
Please note that SelectedText and SelectedValue properties (suggested by some commenters) have different semantics and cannot be used for this purpose.
Try this,
description.SelectedValue.ToString();
You need to define DataValue member and Text Member field on your combobox. Its binding with DataRowView.
So just add two line after
this.descriptionTableAdapter.Fill(this._10daliriPayrollDataSet.Description);

Items in WinForms ComboBox not updating when DataSource values change

I have a ComboBox bound to a List via a DataSource. For some reason, when the datasource items change, the items in the combo box don't seem to automatically update. I can see in the debugger the datasource contains the correct items.
There are lots of answers on StackOverflow about this, but most are either unanswered, don't work for me, or require changing from using Lists to BindingLists which I cannot do this instance due to the volume of code which uses methods BindingLists don't have.
Surely there must be a simple way of just telling the ComboBox to refresh it's items? I can't believe this doesn't exist. I already have an event which fires when the Combo needs to be updated, but my code to update the values has no effect.
Combo declaration:
this.devicePortCombo.DataBindings.Add(
new System.Windows.Forms.Binding("SelectedValue",
this.deviceManagementModelBindingSource, "SelectedDevice", true,
DataSourceUpdateMode.OnPropertyChanged));
this.devicePortCombo.DataSource = this.availableDevicesBindingSource;
Code to update the combobox:
private void Instance_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "AvailableDevices")
{
// Rebind dropdown when available device list changes.
this.Invoke((MethodInvoker)delegate
{
devicePortCombo.DataSource = AvailableDevicesList;
devicePortCombo.DataBindings[0].ReadValue();
devicePortCombo.Refresh();
});
}
}
You are not binding the DataGridview's DataSource to same BindingSource object in your case this.availableDevicesBindingSource which bound first time. but later you are binding to different object AvailableDevicesList. again you are using another binding source for SelectedValue i.e this.deviceManagementModelBindingSource.
use one BindingSource only, may solve your issue

WPF Listview sort works only one time, and then nothing happen

I have a ListView in a WPF window, that I am trying to sort by clicking on the columns.
To learn how to do this, I followed this link: http://www.wpf-tutorial.com/listview-control/listview-how-to-column-sorting/
I have created the method GridViewColumnHeader_Cick as follow:
private void GridViewColumnHeader_Click(object sender, RoutedEventArgs e)
{
GridViewColumnHeader column = (sender as GridViewColumnHeader);
string sortBy = column.Tag.ToString();
searchResultListView.Items.SortDescriptions.Clear();
searchResultListView.Items.SortDescriptions.Add(new SortDescription(sortBy, ListSortDirection.Ascending));
}
It works the first time I click on a column header, but if I click on another column header after, nothing happen. I tried to execute it step-by-step, but I could not find anything that would cause this.
Check if you didn't copy-paste columns with the same tag in xaml.
You have to change the sort direction. You are hard-coding ascending sort into the sort description.

ComboBox got new SelectedValue on SelectionChangeCommitted but doesn't got new SelectedText

I have this code
private void FrmNovedadMedidas_SelectionChangeCommitted(object sender, EventArgs e)
{
ComboBox c = (ComboBox)sender;
CargarMedidasPorIdTipoMedida(Convert.ToInt16(c.SelectedValue));
this.txtBoxNombreTipoMedida.Text = c.SelectedText;
}
in c.SelectedValue got the new value of the selection (the one that the user has selected in the Combo).
But in c.SelectedText I got the old value of the ComboBox (I mean, the one that was before the user change the selection).
Is there any property that can give me the new Selected Text?
I want to avoid to search in the DataSet binded to the ComboBox everytime.
I've read this but doesn't work, I don't have CommitEdit() in ComboBox
edit:
c.Text also gives me the old one
I seem to remember this situation having to do with the DropDownStyle of the ComboBox.
Can you please try different styles and see if the Text property is set to the new value inside SelectionChangeCommited ?
As per your comment, it seems using DropDownList style solves the issue.
Cheers
I found something.
c.GetItemText(c.SelectedItem)
Is there a directly properties, post it please.
Thanks for readme anyway.
Try the SelectedIndexChanged event on the ComboBox versus the SelectionChangeCommited event. Then use c.Text to get the value the user just selected.
c.SelectedValue() returns null for me.
c.GetItemText(c.SelectedItem) works for me though. Changing the dropdownstyle wasn't an option.

InvalidOperationException on Databinding when removing last item in list

I'm getting the following message when I try to remove the last item in a datagridview.
DataBinding cannot find a row in the list that is suitable for all bindings.
I have my binding setup as follows.
ExtendedBindingList<MyClass> bl = new ExtendedBindingList<MyClass>(GetDataFromDB());
BindingSource bs = new BindingSource();
bs.DataSource = bl;
dgv.DataSource = bs;
ExtendedBindingList is just something simple I threw together to implement sorting and filtering and some basic state persistence. dgv is the DataGridView. GetDataFromDB returns a List of MyClass.
The issue only arises when I try to remove the last item from the datagridview using:
bs.RemoveCurrent();
which works at all other times. My only hint for a solution is to remove all bindings and reapply them but this doesn't seem like an ideal solution.
EDIT
The exception only gets thrown after the BindingList successfully removes the last item in question. It get's thrown in external code so I can't tell exactly what is throwing it.
So, here I am, asking SO for some help :).
Thanks in advance,
Justin
Here is how I remove selected row from a grid:
private void btnDelete_Click(object sender, EventArgs e)
{
if (grid.CurrentRow == null) return;
var selectedItem = grid.CurrentRow.DataBoundItem as PartGroup;
if (selectedItem != null &&
UIHelper.ShowQuestion("Are you sure you want to delete selected row?") == System.Windows.Forms.DialogResult.Yes)
{
groups.Remove(selectedItem);
}
}
groups is my BindingListEx(Of T).
Hope it helps.
[Sorry, not really an answer but I feel this is valuable since no answer was given.]
I was getting the exact same situation using .NET Compact Framework 2.0. Testing traced it to the point where a NumericUpDown.DataBindings.Add() was used to bind the control to the source. After this point, using RemoveCurrent() would produce the error if the item was the last one in the source. Prior to that binding (or if it was skipped), the error would never appear.
Other controls were bound to this same source -- TextBox and ComboBox -- but they did not cause this behavior. Only the NumericUpDown control.

Categories