Update ListView, ObservableCollection - c#

I dont know if is coincidence or not, but after updating Visual Studio 2013 with update 5 my listview doesnt update any more. Because i am not any mvvm wpf expert please for help.
App update List every time when USB plug in or out - this happening with help of backgroundworker and eventhandler.
my ViewModel
public ObservableCollection<Devices> devicelist { get; protected set; }
void DeviceInsertedEvent(object sender, EventArrivedEventArgs e)
{
fd.FindDevices();
RefreshList();
}
void DeviceRemovedEvent(object sender, EventArrivedEventArgs e)
{
fd.FindDevices();
RefreshList();
}
public void RefreshList()
{
devicelist = new ObservableCollection<Devices>();
devicelist.Clear();
foreach (var item in fd.devices)
{
devicelist.Add(item);
}
}
So, if device is pluged in or out, method FindDevices start (this loop just asking for information for every device) and at the end I get List of devices with all the informations. The I like to refresh list. "fd.devices" have right number of devices only WPF doesnt refresh.
If have anyone any question, please ask. And please help me with my issue :)

You need to Raise a Property changed event. This ripples up to the UI element and basically it refreshes what data it has.
Assuming the list is bound to from the UI?
If you don't already have a base view model class it might be worth looking at this:
http://www.galasoft.ch/mvvm

Related

How to switch tabs with button in a TabControl?

I've watched a few tutorials online on how to change tabs using buttons but for some reason all the code I've tried doesn't work. I am currently running Microsoft Visual Studio 2017 and am trying to write some code for a button to change tabs.
I couldn't find any differences between my code and code shown in tutorials, so it may just be a Visual Studio setting that I haven't set up correctly to allow the button correctly, but I couldn't figure out if or where it may be.
Here's my current code:
//Element event handlers
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
tabControl.SelectedTab = DestSelect;
}
private void buttonGotoIntro_Click(object sender, EventArgs e)
{
tabControl.SelectedTab = Intro;
}
//An old computer-generated segment code for the previous button.
//When I try to remove it the computer gets mad at me.
private void GotoIntro_Click(object sender, EventArgs e)
{
}
Please confirm you have subscribed to the Click event for the buttons.
public Form1()
{
InitializeComponent();
buttonStart.Click += buttonStart_Click;
buttonGotoIntro.Click += buttonGotoIntro_Click;
}
Instead of 'tabControl.SelectedTab = DestSelect;" try instead the method 'tabControl.SelectTab(DestSelect);'
I read through this article to find your (hopefully) answer:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/2cf22896-c5bd-4a9b-ab61-34404b55ef01/how-to-jump-to-a-specific-tab-in-the-tabcontrol?forum=vbgeneral
I assume u want to select a tab when a different Button is clicked.
tabControl.SelectedIndex = [Index of tab to switch to];
Code should look like;
tabControl.SelectedIndex = feeTabIndex;
If this is not clear enough, tell me exactly what you want to do.

How do you fire event using NotifyPSEvent every time a field is changed?

I'm trying to run event handler methods the moment a Presentation Space is changed. I'm not the most familiar with the native AUTPS methods but the PS.NotifyPSEvents seems promising
I've tried some of the online examples on the IBM sites but i cant seem to figure them out.
public partial class Form1 : Form
{
public AutPS A_PS = new AutPS();
public AutOIA A_OI = new AutOIA();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
A_PS.SetConnectionByName("A");
A_OI.SetConnectionByName("A");
A_PS.NotifyPSEvent += A_PS_NotifyPSEvent();
A_PS.RegisterPSEvent(true);
}
private AutPSTypeLibrary.IPSEvent_NotifyPSEventEventHandler A_PS_NotifyPSEvent()
{
if (A_PS.SearchText("GEEP", PsDir.pcSrchForward, 1, 1))
{
MessageBox.Show("BLAH"); return null;
}
}
}
I was hoping that when the PS registers a field change, that i could capture this and record what was changed, however it's only firing the event upon starting my program regardless of anything being updated on the screen. Subsequently, when i do update anything on the screen the event does not fire. I'm sure i'm just misunderstanding how this particular method is working, but I've searched for a week with no real answers that i can translate well enough to have it working. Any info would be appreciated!
Figured it out, I was on the right track with the events but missed a little bit of information. below is the resolution that worked for me:
private void Form1_Load(object sender, EventArgs e)
{
A_PS.SetConnectionByName("A");
A_OI.SetConnectionByName("A");
//Added a new A_PS_NotifyPSEvent handler and it works perfectly now.
A_PS.NotifyPSEvent += A_PS_NotifyPSEvent1;
A_PS.RegisterPSEvent(true);
A_Reco.AddPS(A_PS);
}
private void A_PS_NotifyPSEvent1()
{
MessageBox.Show("BLAH");
throw new NotImplementedException();
}

MonthCalendar: prevent view change

I need to prevent the view change of a MonthCalendar.
I was able to prevent the view change like this:
private void WPF_S_MC_Calendar_MouseDown(object sender, MouseEventArgs e)
{
if (WPF_S_MC_Calendar.HitTest(e.Location).HitArea.ToString() == "TitleBackground")
{
WPF_S_MC_Calendar.Enabled = false;
WPF_S_MC_Calendar.Enabled = true;
}
}
But that prevent all the next events to be fired.
So, someone know, if this is possible?
EDIT:
Another way would be to let the events to be fired and the scroll in the selected month again. The user wouldn't be able to see it.
Thank you guys.

Get edited GridView item when EditingElement is unknown

I have a UserControl, which is basically a wrapper for a GridView that needs to send a message every time a cell content (of the GridView) is changed. Usually, that could be solved like this:
private void MainDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
var editingTextBox = e.EditingElement as TextBox;
doSomething(editingTextBox.Text);
}
The problem is that I don't know the type of EditingElement (which comes as a FrameworkElement) so I cannot do the conversion. And in that moment, the currentCell.SelectedValue is still the original value. I also don't have control over the model (where I could implement INotifyPropertyChanged and use it to catch the changes).
Is there some simple way I am missing? Or how would you go about implementing this? Thank you for any suggestions.
Wrapping your model within a CollectionView and use this for the binding.
myCollectionView = (CollectionView)
CollectionViewSource.GetDefaultView(rootElem.DataContext);
This will provide you a INotifyPropertyChanged interface.
Update
Sorry my first answer was somewhat misleading.
If you're not able to change the model, you should create a view model. The view model, implementing INotifyPropertyChanged can provide the needed change events without any knowledge of the current view. This way the view doesn't depend directly on the model.
Futher reading:
The role of the model in MVVM
I have found a very simple solution (can't belive I haven't seen that one) composed of catching two events from the DataGrid.
Here is the code:
private object changedRow;
private void MainDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
changedRow = e.Row.Item;
}
private void MainDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
if (changedRow != null)
{
// do something with the edited row here
changedRow = null;
}
}

Passing Data from datagridview to form C#

Hoping someone can shed some light on my problem. I followed the tutorial found here http://msdn.microsoft.com/en-us/library/ms171925(v=VS.100).aspx#Y3500 and cannot get this to work.
My code is as follows:
namespace CityCollectionCSharp
{
public partial class frmSwitch : Form
{
public frmSwitch()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'newCityCollectionDataSet.ClientTable' table. You can move, or remove it, as needed.
this.clientTableTableAdapter.Fill(this.newCityCollectionDataSet.ClientTable);
// TODO: This line of code loads data into the 'newCityCollectionDataSet.PropertyInformation' table. You can move, or remove it, as needed.
this.propertyInformationTableAdapter.Fill(this.newCityCollectionDataSet.PropertyInformation);
}
private void propertyInformationDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
System.Data.DataRowView SelectedRowView;
newCityCollectionDataSet.PropertyInformationRow SelectedRow;
SelectedRowView = (System.Data.DataRowView)propertyInformationBindingSource.Current;
SelectedRow = (newCityCollectionDataSet.PropertyInformationRow)SelectedRowView.Row;
frmSummary SummaryForm = new frmSummary();
SummaryForm.LoadCaseNumberKey(SelectedRow.CaseNumberKey);
SummaryForm.Show();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
propertyInformationBindingSource.Filter = "ClientKey ='" + comboBox1.SelectedValue + "'";
}
}
}
That is for the first form and now the second form:
namespace CityCollectionCSharp
{
public partial class frmSummary : Form
{
public frmSummary()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'newCityCollectionDataSet.PropertyInformation' table. You can move, or remove it, as needed.
this.propertyInformationTableAdapter.Fill(this.newCityCollectionDataSet.PropertyInformation);
}
internal void LoadCaseNumberKey(String CaseNumber)
{
propertyInformationTableAdapter.FillByCaseNumberKey(newCityCollectionDataSet.PropertyInformation, CaseNumber);
}
}
}
I have the query set up as follows in the propertyInfromationTableAdapter :
SELECT CaseNumberKey, BRTNumber, ParcelNumber, Premises, ClientKey, ParcelNum, Registry, TaxAcctName, StreetCode, CoverDate, OrderDate, Assessment, TaxFrom, TaxTo, TaxOpen, WaterOpen, WaterAcct, WaterTo, WaterFrom, AssessedBeg, AssessedDim, SumNotes, Legal, TotalWater, TotalTax, Type, OPARec, OPADoc, OPADocNum, Recital, Num, Name, Direction, Unit, ProductKey, DateFinished, Finished, Paid, BD, BDPaid, Search, Exam
FROM PropertyInformation
WHERE (CaseNumberKey = #CaseNumberKey)
I cannot figure out for the life of me why this does not work as prescribed. When I click on a record it passes both records in the table and always has the first one in the boxes I have. I only know this as I left the bindingnavigator. Any help would be much appreciated.
Whenever I have an issue getting to work off the net it boils down to me doing it / implementing it wrong, a misunderstanding of how something works and therefore it is going to break anyway or if the code is from a third party then it may not work to start or you find a bug.
It is unlikely the MSDN code is wrong (but not impossible) so I would suggest breakpoints galore in the code you have produced and if that doesn't enlighten then I would create a brand new project copying the code word for word may mean you see where you went wrong or if you get that working then slowly copy in what you have to see when it breaks.
Try this,
Load to Datagridview using sqldatareader
DataBinding between DataSet and DataGridView in C#
How to: Bind Data to the Windows Forms DataGridView Control
You can also use sqlDatareader,sqlDataAdapter to load on Datagridview Programatically..
Regards

Categories

Resources