Allow empty/no selection in DevExpress' LookUpEdit - c#

I have a form containing a DevExpress LookUpEdit (Windows Forms) which is bound to a list of objects with several displayed properties. The EditValue property is set to another object's property which will receive the selected value.
The user may select any item from the list of objects, but I also want to allow empty selections i.e. the EditValue would become null and the displayed text should be the default [No entry] then.
How could this be accomplished the easiest way?
Currently there's no way to clear the value after it has been set once.

There are two options:
1. User can press Ctrl+Del to clear the value
2. However, this is not intuitive. What I do is add another value to the bound list.
var list = GetOriginalList(); // <- get all possible values
list.Add(new MyItem("[empty]", null)); // <- display name and ID

Try this one :
In form load :
LookUpEditName.Properties.AllowNullInput = true ;
LookUpEditName.Properties.NullText = "No entry";
and use LookUpEditName.EditValue = null; to clear the value

Add Cancel Button to your Lookup Edit and add reset code in Button Click event
Dim editor As LookUpEdit = CType(sender, LookUpEdit)
If editor.Properties.Buttons.IndexOf(e.Button) = 0 Then
YourLookUpEdit.EditValue = DBNull.Value
End If

Related

Combobox.SelectedValue wont select the actual value

I have a problem that I'm struggling with.
I fill a combobox this way:
RDTA_cb_provincias.DataSource = provincias;
RDTA_cb_provincias.DisplayMember = IdiomaBD.ObjCI.ToString().Equals("eu-ES")
? "TTEXNOMBRPROV_EU"
: "TTEXNOMBRPROV_ES";
RDTA_cb_provincias.ValueMember = "TCODIDTERRITORIO";
There's some stuff depending on the language of the user, but i guess that's not important here.
Well, when i want to select depending on the value member i do this:
RDTA_cb_provincias.SelectedValue = provincia2.TCODIDTERRITORIO;
But for some reason it won't show up the actual item I want to show. While debugging i can see that RDTA_cb_provincias.SelectedValue matches provincia2.TCODIDTERRITORIO.
For example, I've set SelectedValue to 51, but when i see the SelectedItem inside the ComboBox once I've changed the value, It shows up an object that has 47 as its TCODIDTERRITORIO.
What can it be? Or how can I bypass this problem (Maybe iterating the whole ComboBox item list until i find the one i want to select?)
Thanks in advance!
You should use SelectedItem instead:
RDTA_cb_provincias.SelectedItem = provincia2;
Or you can use SelectedIndex as follows:
RDTA_cb_provincias.SelectedIndex = RDTA_cb_provincias.FindStringExact(provincia2.TCODIDTERRITORIO)

Combobox cell value data source datagridview

Below in image i have Reason as combo box which has three options .
On selecting first value such as In house Rework, Second column Responsible party which is
also combo box should get populated with list collection and so like on selection
of another value from Reason combo box Responsible party should get populated with
another list collection.
Since i have attempted on end edit of gridview and it's happening to show value but if on next row i change my Rework combo box value the previous value of Responsible party doesn't retain its value.
So how can i achieve to prevent this case.
So any help is appreciated.
Are you trying to bind the other two combo boxes based off the value selected in the reason? If so I would use the CellValueChanged event in the Data Grid's events and you can programmatically build a list and databind it. If this is what you want to do I'll post an example here, if not please clarify so I can be help.
Private Sub dgVendors_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgVendors.CellValueChanged
If dgVendors.Columns(e.ColumnIndex).HeaderText = "Reason" Then
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).DataSource = Nothing
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).DisplayMember = ""
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).ValueMember = ""
ElseIf dgVendors.Columns(e.ColumnIndex).HeaderText = "Responsible Party" Then
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).DataSource = Nothing
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).DisplayMember = ""
CType(dgVendors.Rows(e.RowIndex).Cells(""), DataGridViewComboBoxCell).ValueMember = ""
End If
End Sub
If you want to handle the selection change event of the dropdown, you can handle the EditingControlShowing event of the DataGridView.
There is also an MSDN page which might help you out.
Also, make sure you change the DataSource of the DataGridViewComboBoxCell on the CurrentRow, not of the DataGridViewComobBoxColumn.

Not able to set dropdownlist at page load

I am getting first time this kind of problem
I am setting value of dropdownlist at page load from dataset
But this is auto setting 0 index .....
My code :
ddlEbitda.SelectedValue = objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
I checked at immediate windows ...
objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
"Between 40-60 %" ( come value from data set is right)
ddlEbitda.SelectedValue
"Up to 10 Million Dollar " ( this is default value setting )
I tried many different code to solve this problem :
ddlEbitda.ClearSelection();
string Ebitda = objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
ddlEbitda.Items.FindByValue(Ebitda).Selected = true;
string Ebitda = objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
ddlEbitda.SelectedIndex = ddlEbitda.Items.IndexOf(ddlEbitda.Items.FindByValue(Ebitda));
But still not able to solve this problem ....truth is I am not able to understand what is problem .....
This should work
string value=objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
ddlEbitda.Items.FindByValue(value).Selected = true;
But make sure when you are binding the dropdown with DataSource, at that time acq_ebitda should be DataValueField.
If it is DataTextField then you should try something like this
ddlEbitda.Items.FindByText(value).Selected = true;
Ensure that the DropDownList is populated/bound inside this code block in the Page_Load method
If (!IsPostBack)
{
}
and immediately after set the selected item.
ddlEbitda.SelectedValue
Make sure whether your dropdown list is correctly bound to the dataset and data is populated . specially make sure the value you are assigning is in the item list. you can easily check with quick watch in VS so you can make sure the value you are assigning is inside the dropdown items. because the way you are doing is OK

DatagridView Highlight Event - WINFORM C#

I have a combobox which is connected to the database so I populate the value of my combobox based on what's in my database. my combobox is another FORM from the datagrid. So here's I want to achieve.
form1 = datagrid (based on the database)
form2 = combobox (based on the database)
I want that If I highlight a certain row (My selection mode = fullrowselect) and press a button the comboBox will automatically point to that row.
for ex.
datagrid
name: Joe (highlighted)
*user clicks the button whch in my case is edit
*load edit form
comboBox.SelectedIndex is = highlighted row (which the user clicks)
I can show you my code if it helps. thanks :))
THANKS! :))
You can try to set in the following ways, you can pass the value Joe to the other form via a parameter in the constructor. This could be then used to select you required value in the ComboBox
comboBox2.SelectedIndex = comboBox2.Items.IndexOf("Joe");
comboBox2.SelectedText = "Three"; // or SelectedValue depending on how you are binding
EDIT
Avoid accessing the grid directly from the other form, expose the value required as a property or better pass it to the new form as parameter.
Joe could be the value of the cell like dataGridView2.CurrentRow[0].FormattedValue and pass this to the new form constructor like new Form2(object datagridvalue). Then use the value in the form later on.

How to disable dropdown list value in the current form that was selected in the previous page

I am having 2 web formsand will have a drop down list on those web forms. If i select a value from drop down and click on ok i will get tranfer to next page. In that page i will have a drop down with the same values in the previous form . What i need is i would like to disable the selected value in the previos form and would like to display the remaining normal
I dont know your exact scenario but i would suggest that you look at whether you can maybe achieve your needs using a single form instead of two.
If that is not an option :-
If you are POSTING the form on click of OK, you can retrieve the value of the original page using PreviousPage. Refer eg below.
I am not quite sure what you mean by "disable the selected value in the previos form and would like to display the remaining normal" but once you retrieve this value, then you can manipulate the data in your current page any way you want.
DropDownList oldValue = (DropDownList)PreviousPage.FindControl("DropDownOldValue");
oldValue.SelectedValue - should then give you the value selected on the previous page
Got the answer
DropDownList oldvalue = (DropDownList)PreviousPage.FindControl("DropDownList1");
string str = oldvalue.SelectedValue.ToString();
ListItem i = DropDownList1.Items.FindByValue(str);
i.Attributes.Add("style", "color:gray;");
i.Attributes.Add("disabled", "true");

Categories