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);
Related
I need two combo box cell in my data grid view that when we select item in first one the second binding source should be change to something
for example :
we select a bank name from the first combo box the second combo box item should be the items that belong to that bank
My binding combo box is properly working but in any event when I try to handle the changing the second datasource it have given me error like image below,
datagridview error datagridviewcomboboxcell not validate how can it be?
error datagrid view image
[1]: https://i.stack.imgur.com/u9yMO.png
DataGridView's default error handling seems to work on assumption that it cannot assume anything. So, normally it's quite safe to just override it.
myGrid.DataError += myGrid_DataError;
private void myGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
// ignore
}
Also gives you a place where to write actual error handling, should it become necessary.
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!
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.
Coding on Visual C# since a few days ago.
Trying to access the elements in a DataGrid using the following code
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
textBox2.Text = this.dataGridView1.SelectedCells().Value;
}
Throws the next exception
Non-invocable member 'System.Windows.Forms.DataGridView.SelectedCells'
cannot be used like a method.
What's the problem with SelectedCells then? What's the best practice?
EDIT: I guess I'll just convert the member to string and see how it goes.
Selected Cells is a property not a method.
DataGridView.SelectedCells Property
How to: Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control
From Msdn: DataGridView.SelectedCells - Gets the collection of cells selected by the user.
Test to see if SelectedCell.Count = 1 (only one cell was selected) then textBox2.Text = SelectedCells(0).Value
Alternativelly try using the CurrentCell property.
I just created a ComboBox in my VS2008. I put four items for it, One, Two, Three and Four. When I run it, nothing displayed by default. I need to select one to display. How can I make it displaying the second item by default at very beginning without my selection? I tried by put a number inside the DisplayMember and ValueMeme property but it doesn't work.
thanks,
Use the SelectedIndex property:
private void MyForm_Load(object sender, EventArgs e)
{
ComboBox1.SelectedIndex = 0;
}
You can set the SelectedIndex property of the combobox. Setup work like this is often done in the Form's Load event.