ComboBox.SelectedItem no longer relevant? - c#

Good Afternoon,
I am using a ComboBox in visual studio to determine whether a user can use a text field below it on a form.
The Combobox "ReasonBox" is bound to a datasource and sql query that selects the allowed "Reasons" to choose from.
Before I had the dynamic selections I was using:
private void ReasonBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (ReasonBox.SelectedValue != null) `
if (ReasonBox.SelectedItem.ToString() == "Other")
{
{ ReasonTextBox.Enabled = true; }
{ ReasonTextBox.BackColor = Color.White; }
}
}
to enable writing to the TextBox when "Other" was selected.
Unfortuantely now I am unable to figure out how to make this happen with my databound ReasonBox. Any Ideas?
Edit: Thanks for the help guys, I think i've found the reason:
ReasonBox_SelectedIndexChanged
Appears to not be triggering when I change the selection. I'll investigate further in the morning :)

Try SelectedText
You can find the documentation here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext%28v=vs.110%29.aspx

try this
ReasonBox.Text == "Other"
Text property gets or sets the text associated with this control.

Try
"ReasonBox.Text" == "Other"
OR
ReasonBox.Items[ReasonBox.SelectedIndex].ToString() == "Other"
Insted of
ReasonBox.SelectedItem.ToString() == "Other"

Related

How to reset combobox?

I have a combobox with dropdown style and 2 text boxes. I want to add a condition that if the string of either of the 2 text boxes are not null than the combobox should get reset if any item is selected from it.
I'm using combobox.SelectedIndex=-1 in my if clause but it does not work I guess because I'm using it in a wrong event.
Make sure that both your textboxes are using the TextChanged event, and then point them to the same method. If both boxes are not null, then the combobox will reset. If you want it to be one or the other, just changed the && to ||
private void TextBox_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
{
comboBox1.SelectedIndex = -1;
}
}
Try This
combobox.Items.Clear();
or
combobox.DataSource = null;
I hope you managing you Text_Changed event well , cause you have not posted that code
Same as clearselection in c#
the command for WF is
combobox.ResetText()

How can I make a column of check boxes in the ObjectListView behave like radio buttons?

I'm using an ObjectListView and I'm having a heck of a time. Admittedly, I don't have a lot of experience doing things in a Model-View structure, and on top of that, the source forge page for the ObjectListView page is down.
I have a boolean column that is set as a check box in the OLV, and I would like it to behave like a radio button. My problem is that I can't capture the click event for the check boxes in the OLV. My underlying model is a File (class), which holds a list of Fields (class) which has 3 properties. One of the properties of the Fields class is this checkbox. So, presumably, the field knows when it's been clicked, but it doesn't know about the File, which should do the checking to see if it has any other fields with this property set.
Can anyone point me in the right direction?
Thank you.
What about this?
olv.ItemChecked += delegate(object sender, ItemCheckedEventArgs e)
{
var item = e.Item as OLVListItem;
if (item != null && e.Item.Checked)
{
var objects = ObjectListView.EnumerableToArray(olv.Objects, true);
objects.Remove(item.RowObject);
olv.UncheckObjects(objects);
}
}
Here's what I came up with:
private void tlvFields_SubItemChecking(object sender, BrightIdeasSoftware.SubItemCheckingEventArgs e)
{
foreach(var field in f.Fields)
{
if(field.PK)
{
field.PK = false;
}
}
tlvFields.SetObjects(_files);
}
It took me awhile to figure out how to mess with the models under the view. Once I figured that out, this was easier.

Unable to delete data from database

I encountered a problem whereby when the user clicked on the delete button, Nothing happens and when I insert breakpoint to check, The selectLocStation is null. Why is it happening? Can anyone kindly solve my doubts about it?
Here are the codes for you to see my Delete codes. Appreciate any helps that were offered.
private void btnDel_Click(object sender, EventArgs e)
{
using (satsEntities Setupctx = new satsEntities())
{
int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue);
var DeleteRTiming =
(from delLocStation in Setupctx.requiredtimings
where delLocStation.RequiredLocationStationID == selectLocStation
select delLocStation).SingleOrDefault();
if (DeleteRTiming != null)
{
Setupctx.DeleteObject(DeleteRTiming);
Setupctx.SaveChanges();
cbLocStation.SelectedIndex = -1;
this.Edit_TS_Load(null, EventArgs.Empty);
MessageBox.Show("Selected Required Timing And " +
"The Location Station Has Been Deleted.");
}
}
}
This is the codes that were used to bind.
private void Edit_TS_Load(object sender, EventArgs e)
{
using (satsEntities Setupctx = new satsEntities())
{
var DeleteRT = (from DelRT in Setupctx.requiredtimings
join locationstationname ls in Setupctx.locationstationnames on DelRT.RequiredLocationStationID equals ls.locationstationID
select ls.locStatname).Distinct().ToList();
foreach (var locstationData in DeleteRT)
{
cbLocStation.Items.Add(locstationData);
}
}
}
How can selectLocStation be null ? it is of type int, are you getting any exception or you mean your object DeleteRTiming is null ? probably its the DeleteRTiming which is null
The simple answer to that the record you are looking in the database against selectLocStation is not there.
You need to put a break point and see what is being held by 'selectLocStation` and then check the database manually if the record exists in the database.
when you bind your control, you must set it in ! IsPostback and persist you with ViewState
If(! IsPostBack)
{
BindYourControl(); //For just first load.
}
Persist with
EnableViewState = true;
In order to don't erase your selected value
If this is a silverlight application then you might want to use
cbLocStation.SelectedItem
Or it is a ASP.Net site and you rebind the data on every pageLoad
Either way you should have an exception because you are trying to convert a null value.
See the method that you have used to bind the combo box
cbLocStation.Items.Add(locstationData);
This will create the combobox with "ValueField" and "TextFiled" with value of the variable "locstationData"
So when you try to get selected value from cbLocStation.SelectedValue ,it will always contain name and so you cannot parse it to integer.
There is another overload for the "Add" Which will accept Value(Id) and Text(Text to display)
cbLocStation.Items.Add(new ListItem(locstationData.locStatname, locstationData.locStatID));
Try to change the binding portion like this

selected changed used to find data in another datagridview store previous value

Using MS Visual Studio and C#.net 4.0.
So, just completed my other part of my program that checks for duplicates which finally works "many thanks to all that helped". Showed my boss who liked it but then asked "if he was to select the results of the datagridview that shows the part numbers with duplicate values, is it then possible to highlight the maindatagridview that is equal to the result selected".
Now first of all I understand what he means, but wording this has been rather difficult and as such searching for some examples to get me started has been very difficult.
Now although I don't have any code I can show the code I currently have.
The first thing I did was to identify an event handler on the datagrid that could detect what row is selected, I'm going to use "selectionchanged".
UPDATE:: ok I though I would show you what I meant by re-using my code.
Please note that the code is very similar but is only a starting point, I may incorporate the previous method in with the new one.
private void MyErrorGrid_SelectionChanged(object sender, EventArgs e)
{
string getPartSelected;
getPartSelected = MyErrorGrid.CurrentCell.Value.ToString();
foreach (DataGridViewRow row in ParetoGrid.Rows)
{
var cellValue = row.Cells["Keycode"].Value;
if (cellValue != null && cellValue.ToString() == getPartSelected)
{
ParetoGrid.Rows[row.Index].DefaultCellStyle.BackColor = Color.Red;
}
}
}
As you can see this works, but there are some problems. It highlights But doesnt un-highlight, so I think I need to store the previous selected? (not sure this is the best way).
Also need to Add navigation, as highlighting is not good enough for the user. At the moment ive added in selected = true but again when the selection changes i need to use the previous value.
Ok this feels like I'm cheating, but here's what I did to solve the problem.
private void MyErrorGrid_SelectionChanged(object sender, EventArgs e)
{
string getPartSelected;
getPartSelected = MyErrorGrid.CurrentCell.Value.ToString();
foreach(DataGridViewRow allrow in ParetoGrid.Rows)
{
ParetoGrid.Rows[allrow.Index].DefaultCellStyle.BackColor = Color.Empty;
ParetoGrid.Rows[allrow.Index].Selected = false;
}
//might need a do while
foreach (DataGridViewRow row in ParetoGrid.Rows)
{
var cellValue = row.Cells["Keycode"].Value;
if (cellValue != null && cellValue.ToString() == getPartSelected)
{
ParetoGrid.Rows[row.Index].DefaultCellStyle.BackColor = Color.Red;
ParetoGrid.Rows[row.Index].Selected = true;
}
}
}
As you can see it basically clears all before entering the next change, so it works but without using a previous value.
If anyone has a better solution feel free to answer
The only thing is when there is lots of rows, although it selects and highlights it doesnt scroll the datagridview is there a similar method to do this?

DataGridViewComboBox - How to allow any value?

I'm having some trouble with VisualStudio 2010 C# Winforms.
I have created a DataGridView with an unbound column that is of type DataGridViewComboBoxColumn. The column works fine, except unlike a normal ComboBox, I can't seem to type in just any value. I am forced to pick a value from the list.
Is there a property I need to set or another type I can use that will allow me to enter any value in the cell in addition to providing a list to pick a value from?
Thanks!
I don't think there is a property that will allow this, but I found an answer here that worked with a small modification.
Try adding the following 2 event handlers, here assuming a column named comboBoxColumn:
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox c = e.Control as ComboBox;
if (c != null) c.DropDownStyle = ComboBoxStyle.DropDown;
}
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == comboBoxColumn.Index)
{
object eFV = e.FormattedValue;
if (!comboBoxColumn.Items.Contains(eFV))
{
comboBoxColumn.Items.Add(eFV);
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = eFV;
}
}
}
DataGridViewComboBoxColumn is designed rather for select from possible values - not for typing data. If you want to add any data, you should do it programmatically for desired DataGridViewComboBoxCell:
((DataGridViewComboBoxCell)dataGridView1[0,0]).Items.AddRange(new string [] {"A","B","C"});

Categories

Resources