BindingSource AddNew() and initializing values - c#

i have a ComponentOne FlexGrid bound to a bindingsource and the bindingsource is bound to a bindinglist collection.
The user clicks an insert button. I call AddNew() on the BindingSource. in the AddingNew() event, i want to initialize the properties in the bindingsource. usually if i want to access the data underlying the grid row i do this
MemberSkill skill = (MemberSkill)MemberSkillBS.Current
skill.SocSecNo = currentMember.SocSecNo;
but when i do this in the AddingNew() event, Current is still pointing to the row with the focus on the grid. how can i access the new item i added to the binding source and initialize it?

The new item becomes the current item after the AddNew has been called.
In your Insert button handler you do:
private void buttonInsert_Click(object sender, EventArgs e)
{
MemberSkill newItem = MemberSkillBS.AddNew() as MemberSkill;
if (newItem != null)
{
MemberSkillBS.Add(newItem);
}
...
}
and in your AddingNew handler you do:
private void MemberSkillBS_AddingNew(object sender, AddingNewEventArgs e)
{
MemberSkill skill = new MemberSkill
{
SocSecNo = MemberSkillBS.Current.SocSecNo
};
e.NewObject = skill;
}

Related

Calling method on SelectionChanged ListBox event with the selected item as parameter

I'm trying to call a method which fills a table with data according to the ListBoxItem that has been selected.
// Setting the ListBoxItems
myListBox.ItemsSource = list;
// Calling the method when the ListBox's selection changes
myListBox.SelectionChanged += LbItem_Select;
The above snippet can't work because the LbItem_Select event handler doesn't get as a parameter the item that is currently selected.
This is the event handler:
private void LbItem_Select(object sender, RoutedEventArgs e)
{
var lbItem = sender as ListBoxItem;
lbItemContent = lbitem.Content.ToString();
// fill the table according to the value of lbItemContent
}
How could I achieve this?
When you work with events, sender is the object related to the event. myListBox.SelectionChanged is an event of the ListBox. So sender is the ListBox, not the item. Try with this:
private void LbItem_Select(object sender, RoutedEventArgs e)
{
var listBox = sender as ListBox;
// Or use myListBox directly if you have the ListBox available here
var item = listBox.SelectedItem;
// Do whatever with the item
}

How to move List View Item from one list to another with drag and drop? UWP C#

I have many List Views in an UWP application and I would want to be able to move one item from one List View to another List View
I know about the AllowDrop or CanDragItems properties and that you need to handle some events for drag and drop to work, although I just don't know how to do it.
If you want to add ListView controls by clicking Add button and move items between ListView controls, please check the following code as a sample. The following code is different from the offical sample, it use ObservableCollection to complete the drag and drop operation. You could drag an item from source ListView item to target ListView, and also drag an item from original target ListView to original source ListView.
You could click the Add button twice and then two ListView with two items are added. You can drag any item from any ListView control to another. If you want to keep the dragged item in the source ListView control, just comment the code dragCollection.Remove(dragedItem as string); .
For example:
private ObservableCollection<string> dragCollection;
private ObservableCollection<string> dropCollection;
private object dragedItem;
private ListView dragListView;
private ListView dropListView;
……
private void AddButton_Click(object sender, RoutedEventArgs e)
{
ListView listView = new ListView();
listView.CanDragItems = true;
listView.CanDrag = true;
listView.AllowDrop = true;
listView.ReorderMode = ListViewReorderMode.Enabled;
listView.CanReorderItems = true;
listView.ItemsSource = new ObservableCollection<string>() { "item1","item2" };
listView.DragItemsStarting += ListView_DragItemsStarting;
//listView.DropCompleted += ListView_DropCompleted;
listView.DragEnter += ListView_DragEnter;
listView.Drop += ListView_Drop;
listView.DragOver += ListView_DragOver;
listView.BorderBrush = new SolidColorBrush(Colors.Red);
listView.BorderThickness = new Thickness(1);
stackPanel.Children.Add(listView);
}
private void ListView_DragOver(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Move;
}
private void ListView_Drop(object sender, DragEventArgs e)
{
dropListView = sender as ListView;
if(dropListView!=null)
{
dropCollection = dropListView.ItemsSource as ObservableCollection<string>;
if (dragedItem != null)
{
dropCollection.Add(dragedItem as string);
//If you need to delete the draged item in the source ListView, then use the following code
dragCollection.Remove(dragedItem as string);
dragedItem = null;
}
}
}
private void ListView_DragEnter(object sender, DragEventArgs e)
{
e.AcceptedOperation = (e.DataView.Contains(StandardDataFormats.Text) ? DataPackageOperation.Move : DataPackageOperation.None);
}
private void ListView_DropCompleted(UIElement sender, DropCompletedEventArgs args)
{
var listView = sender as ListView;
if (listView != null)
{
dropListView = listView;
dropCollection = listView.ItemsSource as ObservableCollection<string>;
if(dropListView==dragListView)
{
return;
}
}
}
private void ListView_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
{
var listView = sender as ListView;
if(listView!=null)
{
dragListView = listView;
dragCollection = listView.ItemsSource as ObservableCollection<string>;
if (dropListView == dragListView)
{
return;
}
if(e.Items.Count==1)
{
dragedItem = e.Items[0];
e.Data.RequestedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
}
}
}
For more information about dragging and dropping, you could refer to the document(https://learn.microsoft.com/en-us/windows/uwp/design/input/drag-and-drop).
Any concerns about the code, please feel free to contact me.
To implement dragging, you must set CanDragItems on the source ListView and AllowDrop on the target ListView. Then, you must handle DragItemsStarting event on the source list. Within this handler you can store the dragged data inside the DragItemsStartingEventArgs.Data property. Afterwards, you handle Drop event on the target list and retrieve the stored item values from the DataPackage using DragEventArgs.DataView.
To see all the moving parts of this in action, I recommend the official UWP samples for drag & drop which are available on GitHub. The first scenario of this sample show dragging items from and to a ListView including reordering support.

Updating the existing DataGridViewComboBoxColumn Items collection

We have DataGridViewComboBoxColumn in which we have four values which are fixed. During run-time when the event dataGridView1_EditingControlShowing occurs we are trying to append new items to DataGridViewComboBoxColumn.
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
combo.DropDown += new System.EventHandler(ComboBox1_DropDown);
}
}
private void ComboBox1_DropDown(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (comboBox.Items != null)
{
List<String> elementname = Elements();
foreach (string s in elementname)
{
if (!comboBox.Items.Contains(s))
{
comboBox.Items.Add(s);
}
}
}
}
I am getting this exception :
Can you please suggest how to add the values to the existing DataGridViewComboBoxColumn in Items collection.
You are adding into the Items of editing control but not into the Items of the ComboBoxColumn, which will eventually be used for validation.
To get easy access to the hosting DGV we use the special type DataGridViewComboBoxEditingControl.
Now we will add the new choices to both Items collections now in the ComboBox1_DropDown event: :
DataGridViewComboBoxEditingControl comboBox = (DataGridViewComboBoxEditingControl)sender;
DataGridView dgv = comboBox.EditingControlDataGridView;
if (comboBox.Items != null && dgv != null)
{
DataGridViewComboBoxColumn dcbc =
(DataGridViewComboBoxColumn) dgv.Columns[dgv .CurrentCell.ColumnIndex];
List<String> elementname = Elements.ToList();
foreach (string s in elementname)
{
if (!comboBox.Items.Contains(s))
{
comboBox.Items.Add(s);
dcbc.Items.Add(s);
}
}
}
Note:
The new Items will not be persistet unless you code for it.
So if you have set fields to new values and saved them, you must also save and reload those new values before re-loading the DGV or else the values will not be in the Column's list of values and throw the DataError again!
The usual place to store them would be a DataTable in your DBMS, but any other external storage may also be used like an XML file or maybe dynamic resources etc.. But the DBMS is the most natural choice imo.

Getting data from event args in ListViewItem click handler

So i have a some ListView populated with data. And i have item click-handler. It looks so
var listview = FindViewById<ListView>(Resource.Id.myList);
listview .ItemClick += OnItemClick;
// populating listview items
MyModel[] someDataArray = GetData();
listview .Adapter = new MyAdapter(this, someDataArray );
....
protected async void OnItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
// here i would like to perfom some actions with instance of MyModel
// associated with current ListView's Item
}
I wanna get the instance of MyModel associated with ListView's Item in OnItemClick event handler but i dont know how i can get this model in this event handler.
There is a Position property on the ItemClickEventArgs. So,
MyModel model = someDataArray[e.Position];

How to use DataGridComboBoxColumn?

I created a DataGrid and added a DataGridComboBoxColumn programmatically.
public partial class MainWindow : Window
{
private DataGridComboBoxColumn weightColumnChar = new DataGridComboBoxColumn();
ObservableCollection<int> mComboBoxValues;
public ObservableCollection<int> ComboBoxValues
{
get { return this.mComboBoxValues; }
set { this.mComboBoxValues = value; }
}//end property
public MainWindow()
{
InitializeComponent();
mComboBoxValues = new ObservableCollection<int>() {-1, 0, 1 };
}//end constructor
private void Window_Loaded(object sender, RoutedEventArgs e)
{
weightColumnChar.Header = "Weight";
dataGrid_Char.Columns.Add(weightColumnChar);
weightColumnChar.ItemsSource = ComboBoxValues;
Binding binding = new Binding();
binding.Path = new PropertyPath(ComboBoxValues[1]);
weightColumnChar.SelectedItemBinding = binding;
}//end method
private void dataGrid_Char_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}//end method
//Opens ComboBox on first click
private void dataGrid_Char_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() == typeof(DataGridCell))
{
DataGrid grd = (DataGrid)sender;
grd.BeginEdit(e);
}//end if
}//end method
}//end class
I added an ItemsSource to it and retrieve the values from an ObservableCollection.
The values from the collection are shown in runtime.
My problem is that if I select a value from the ComboBox this vlaue isn't selected and displayed afterwards. What am I doing wrong?
And I also want to select a default value. How does that work?
Please explain programmatically and not in XAML!
Would be great if anybody could help me.
Thanks!!!
First of all you haven't shown what your underlying collection is that is bound to the DataGrid.
You'll need something like DataGrid.ItemsSource = new ObservableCollection<MyClass>(); (it has to be a collection that supports change notification, thus I chose ObservableCollection).
Secondly you are binding the ComboBox.SelectedItemBinding to the ComboBox.ItemsSource which is nonsense. ComboBox.ItemsSource is the collection of values you can choose from, ComboBox.SelectedItemBinding (I would actually use ComboBox.SelectedValueBinding) is the "path" to the underlying data source that contains/reprsents the final value (eg. MyClass.IntProperty). So you select a value from a collection and assign that to some data item (you cannot assign it back to the collection where you select from).
PS! In case you later use something that resembles a KeyValuePair (eg. Id; Name) as your ComboBox.ItemsSource, then you set ComboBox.SelectedValuePath = Id; ComboBox.DisplayMemberPath = Name;. MyClass.IntProperty would contain the Id value in such a case.

Categories

Resources