Dummy Item in ComboBox - user-interface

I've reached the time for a design decision on how to indicate 'none selected' in a data bound ComboBox. I wish to apply this to all future occurrences where a ComboBox needs this. One cannot set SelectedIndex to -1 on data bound combos, nor can one set SelectedValue to null.
Commonly suggested solutions are to add a dummy row to the combo, but without knowledge of the objects bound to rows, a combo cannot reliably create a dummy object in such a way as to display the 'none selected' message. I don't want to add another item on the data source, as this will compromise the list for other clients that don't use a dummy object.
What other options are there? BTW, I'm using a Telerik RadComboBox, but this scenario is not specific to the Telerik control.

You can define the "empty item" in the markup, and append any data-bound items:
<asp:DropDownList DataSourceID="..." AppendDataBoundItems="true" ...>
<asp:ListItem Value="-1" Text="None"></asp:ListItem>
</asp:DropDownList>
The key is to specify AppendDataBoundItems="true" to append the data-bound items to any items that were specified directly in the markup.
This works for the standard ASP.NET DropDownList but also for the Telerik RadComboBox.

Add a new item before you databind and set AppendDataboundItems = true;
cbo.AppendDataboundItems = true;
cbo.Items.add(new ListItem("None", "-1");
cbo.DataSource = x;
cbo.DataBind();

Hope I no property like AppendDataBoundItems in ThickClient (window App)
Better insert dummy row in your datasource table in Zeroth index.
DataRow dr = dtsource.NewRow();
dr["username"] = "--New User---";
dr["Userid"] = 0;
dtsource.Rows.InsertAt ((dr),0);
cmbToUser.DataSource = dtsource;

IMO this is where data binding falls flat on its face. On non-databound controls this is really easy--simply add the dummy item to the combobox before manually adding the other items.
To reliably do this with all types of comboboxes that are databound you'll need to add the item to your dataset--something which violates the separation of presentation and function that databinding is supposed to bring you in the first place.
More often than not, databinding saves you time up until a point. When you start hacking things to overcome the shortcoming of databinding you're not saving time anymore.
My recommendation for this is usually to re-evaluate whether databinding is the right solution.
/Rant over

Related

ASP.NET MVC Multiple Sequential Posts - Data Not Populating

I have a fairly complex form on my ASP.NET MVC Page. It works like this...
On default load (no HTTP post), it has two select boxes, bound to the Model, and a submit button.
On submit (HTTP post), the values in the select boxes are used to pull some data from the DB. That data is then added to the model instance that was posted, and the instance is used to issue another View(myModelInstance) call.
At this point, things look good. My select boxes retained the selected values, and the new elements based on the DB data are displaying.
If I do another post now, whether I change data or not, the original values selected in the select boxes are null within the Action method of my controller, but the values for the DB data textboxes are not.
The only thing I am really doing special here is using the index notation within Razor to be able to edit parts of my Model that are within an IEnumerable...as in the following example...
#for (int i = 0; i < ViewBag.SomeArbitraryNumber; i++)
{
<input type="text" name="myModel.MyEnumerable[#i].MyValue"
value="#(Model.MyEnumerable[i].MyValue)" size="2" />
}
This indexed notation usage only happens after the original select box values have been posted.
UPDATE:
It seems that the major contributor here is the special index notation. If I eliminate it, it works fine. Also, if I include additional hidden fields to match the two select boxes, and also use the indexed notation for those, the second post works.
Is it possible that your browser is caching the original select boxes, and they were never populated by your second view? In MVC you have to populate the select boxes every time they are displayed. In Webforms, viewstate does this for you automagically, but in MVC it is up to you to make sure they are populated.

Treeview - highlighting multiple items

In Powerbuilder, Does anyone here know how am i going to highlights multiple items on a treeview? Selecteditem function is not doing that. Im looking for a behavior just what Selectedrow id doing.
Thanks!
I've not tried this myself but there is an example on CodeXchange (look for Multi-select TreeView Control).
I think the treeview style datawindow might be easier to do this with.
If not, I found this on the newsgroup, don't know if it helps:
The following script in the
selectionchanged event will implement
a very basic multiselect in the
treeview:
treeviewitem ltvi_item
if KeyDown(KeyControl!) then
This.GetItem(oldhandle, ltvi_Item)
ltvi_Item.Selected = True
This.SetItem(oldhandle, ltvi_Item)
end if
A problem is deselecting the
previously selected items. You would
have to scan through ALL items do look
for the selected ones (once the user
clicks an item without holding down
the control key)
https://groups.google.com/forum/#!msg/powersoft.public.powerbuilder.objects/B2ulHBrSnrs/itwhNBjlyOoJ
One approach to this is to use the CheckBoxes attribute on the treeview, then check the treeviewitem's StatePicture (Unchecked = 1, Checked = 2).
Good luck,
Terry.

Pop up a custom form with custom data on click of a cell in jqgrid

I have a grid control displaying data about a Company. I want one column to be "Employee". On click on the any cell of "Employee" column I want to pop up the one Form called "Employee Details" and would like to feel the data.
How can I do this?
As I understand the modal form on click of a jqgrid cell deals with data only related to that row. I want to show different data on the pop up form, i.e. other than the grid data.
Pl help.
Shivali
Probably you can use unobtrusive links in the column (see this and this answers). The advantage of this technique is that instead of typical links you can define any custom action on click on the link. The look of the link can be defined by CSS, but it seems to the that the link can show the user better as other HTML elements, that the user can click on it.
Alternative you can use a button in the column with respect of the custom formatter, but with the same technique as described before you can free define which action will be done on the click.
Inside of the click event with the parameter 'e' you have e.currentTarget as the DOM element of <a> for the link or <input> or <button> if you use buttons in the grid column. To find the row id you can use var row = $(e.currentTarget).closest("tr.jqgrow") or var row = $(e.target).closest("tr.jqgrow") to find the <tr> element. The row id will be row[0].id.

ComboBox in windows forms Datagridview control

I have a datagridview on a windows form. It has clolumn [EmployeeNumber, EmployeeName and EmployeeDepartment].
I want to be able to edit values directly in the grid but i want EmployeeDepartment to be a combobox column, such that on cell edit, a combobox shows up with available options for the employee department. Has any one been able to implement such functionality?
The DataGridView actually has a DataGridViewComboBoxColumn. If you go to the column designer, and add a new column you'll notice you can select one of a few types. It behaves exactly as you have described - on edit, it appears. The DataGridView uses things called editing controls, and has various events for swapping the control into the cell on edit.
Either way, if all you want is a drop-down, that's available out of the box.
To populate the combo box with items, in the column editor you will notice that it has an Items property for manually setting items, or you can use the data binding properties: DataSource, DisplayMember, ValueMember. Data-binding has issues when an expected value is not in the items collection, you will get a lot of cell errors; but they can be avoided.
Some helpful links:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/e8e2d9eb-fe39-42ab-9a18-2194dac54675/

How to bind test data to a SketchFlow ComboBox?

I haven't been able to successfully bind some test data to a SketchFlow ComboBox. I added a sample data source and created a collection with some basic string values. There are a number of tutorials on how to bind to a ListBox in SketchFlow and that works fine, just not finding anything for a ComboBox.
One thing that caught me out a few times is that you need to have your data in "list mode" before you drag it on to the control (combo box or list box).
It seems to work as I expected, so perhaps I'm not understanding your issue correctly, but I'll give it a shot! I created a sample DataSource with a couple of strings in it, added a ComboBox to the layout root and then just dragged the Collection from the DataSource and dropped it on the ComboBox.
Keep in mind that if you drag the DataSource itself, you'll only be setting the DataContext property of the ComboBox and that's not enough to get your items to display. You also need the ItemsSource bound to the Collection of the DataSource and an ItemTemplate or a DisplayMemberPath to tell the ComboBox how to display your items.
Additionally, if you use the drag and drop method of binding, it will use the ItemTemplate approach for you, which may or may not be what you want as it will generally create a StackPanel and display all fields from the row in your Collection per item in the ComboBox. You can remove the ItemTemplate and set DisplayMemberPath to whichever field you wish to have displayed from your data source's collection.

Resources