DataSource in DataGridView - winforms

I this code
public class Test
{
public string name;
public int age;
public Test (string name, int age)
{
this.name = name;
this.age = age;
}
}
private void button1_Click (object sender, EventArgs e)
{
List <Test> listTest = new List <Test> ();
listTest.Add (new Test ("Pavel", 30));
listTest.Add (new Test ("Dima", 48));
listTest.Add (new Test ("Vova", 48));
dataGridView1.DataSource = listTest;
}
The DataGridView displays three lines, but no value does not tell me that I had incorrectly

Try making the name and age as properties. It will fix your problem.
public class Test
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public Test(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
Hopes you are using .Net 3.5 or more, otherwise Automatic properties doesn't work.
Here is the screenshot

Related

WPF: How to notify the modification of an interface that changes in a DLL on the bindings side

How to notify the modification of an interface that changes in a DLL on the bindings side.
To explain:
Dll code is not editable:
public interface IPlayer
{
int Id { get; }
string Name { get; }
Settings Settings { get; }
PlayerCategory Category { get; }
}
public class TennisPlayer: IPlayer
{
public virtual int Id { get; }
public virtual string Name { get; set; }
public Tennisman(int id, string name)
{
Id = id;
Name = name;
}
public Settings Settings { get; set; }
public PlayerCategory Category { get; set; }
}
My code:
public partial class PlayerItem : NotifyUserControl
{
private DispatcherTimer timer = new DispatcherTimer();
public static readonly DependencyProperty PlayerProperty =
DependencyProperty.Register("Player", typeof(IPlayer),
typeof(PlayerItem),
new PropertyMetadata(null, OnCaptionPropertyChanged));
public IPlayer Player
{
get { return (IPlayer)GetValue(PlayerProperty); }
set
{
SetValue(PlayerProperty, value);
}
}
public string PlayerName
{
get => Player != null ? Player.Name : "";
set => OnPropertyChanged();
}
public PlayerItem()
{
InitializeComponent();
timer.Interval = new TimeSpan(0, 0, 4);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
OnPropertyChanged(nameof(PlayerName));
}
The external dll gives me player classes( for example: tennisman, footballer...) based on the same interface.
But I don't know it and I don't have to do class by class.
I must be missing something huge, but I managed to find nothing in my research.

WPF: set log level and refresh collection after changing

So I have form that show my Application Log.
This is my Log model:
public class LogEntry : IComparable<LogEntry>
{
public string DateTime { get; set; }
public int Index { get; set; }
public string Source { get; set; }
public Level Level { get; set; }
public string Message { get; set; }
public int CompareTo(LogEntry other)
{
return DateTime.CompareTo(other.DateTime);
}
}
public enum Level
{
All = 0,
Debug,
Info,
Warn,
Error,
Fatal,
Off
}
Log Helper
This is my LogHelper class that add the current LogEvent according the level that the user selected:
public static class LogHelper
{
public static ObservableCollection<LogEntry> LogEntries { get; set; }
public static bool AddLogToList { get; set; }
private static int _level;
private static int _index;
private static string _formatPattern = "yyyy-MM-dd HH:mm:ss,fff";
public static void SetLevel(Level level)
{
_level = (int)level;
}
public static void AddLog(Level level, string message, string className, string methodName)
{
if (LogEntries == null)
LogEntries = new ObservableCollection<LogEntry>();
if (AddLogToList)
{
int levelValue = (int)level;
if (levelValue >= _level)
{
Application.Current.Dispatcher.Invoke(new Action(() =>
{
if (LogEntries.Count == 1000)
LogEntries.RemoveAt(LogEntries.Count - 1);
LogEntry logEntry = new LogEntry()
{
DateTime = DateTime.Now.ToString(_formatPattern),
Index = _index++,
Level = level,
Source = className + "\\" + methodName,
Message = message.Trim()
};
LogEntries.Insert(0, logEntry);
}));
}
}
}
}
So I am add LogEvent into my list that contains up ti 1000 entries.
Now I want to be able to filter and show my only the relevant LogEvent Level.
So I added ComboBox with all my LogEvent levels and subscribe to its SelectionChanged event:
private void cbLogLevel_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
int index = cbLogLevel.SelectedIndex;
LogHelper.SetLevel((Level)index);
lvLogger.ItemsSource = LogHelper.LogEntries.Where(m => m.Level == (Level)index).ToList();
}
So after this SelectionChanged event I can see the relevant LogEvent level but my only issue is the new LogEvent not shows.
Maybe I need kind of refresh to my collection or something else ?
You are creating a new List<LogEntry> and setting the ItemsSource property to this one in your event handler. This means that lvLogger will no longer be connected to the ObservableCollection.
Instead of resetting the ItemsSource, you could filter the view:
private void cbLogLevel_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
int index = cbLogLevel.SelectedIndex;
Level level = (Level)index;
LogHelper.SetLevel(level);
var collectionView = CollectionViewSource.GetDefaultView(lvLogger.ItemsSource);
collectionView.Filter = x =>
{
LogEntry logEntry = x as LogEntry;
return logEntry != null && logEntry.Level == level;
};
}

test class for inserting contacts through the following class

I have one apex class which inserts the contacts.i wrote one test class for that where its is passing but the code coverage is zero.can somebody suggest what i missed?
test class:
#isTest
public class TestReferalAccessclass
{
static testMethod void ReferalAccessclassMethod()
{
Test.StartTest();
Contact c=new Contact(FirstName='fname',LastName = 'lname',Email = 'email#gmail.com',Phone = '9743800309');
insert c;
System.AssertNotEquals(Null, c.Id);
Test.StopTest();
}
}
apex class:
public without sharing class ReferalAccessclass {
public String inputID{get; set;}
public String firstName{get; set;}
public String lastName{get; set;}
public String email{get; set;}
public String phone{get; set;}
public Decimal exp{get; set;}
public String location{get; set;}
public contact con{get;set;}
Public attachment objAttachment{get; set;}
public ReferalAccessclass(ApexPages.StandardController controller)
{
objAttachment = new Attachment();
}
public void saveInformation()
{
try{
IF(inputID != 'NULL'){
con = [SELECT ID,Name,FirstName,LastName,Email,Phone,Years_of_Experience__c,Location__c FROM Contact where ID =: inputID ];
con.FirstName = firstName;
con.LastName = lastName;
con.Email = email;
con.Phone = phone;
}
update con;
objAttachment.ParentId = con.id;
Insert objAttachment;
}
catch(exception e){}
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Thank you for your valuable response');
//return null;
}
}
You didn't invoke your actual class from test class. That's why its not giving code coverage. Try this test class.
#isTest public class TestReferalAccessclass {
static testMethod void ReferalAccessclassMethod() {
Contact c=new Contact(
FirstName='fname',
LastName = 'lname',
Email = 'email#gmail.com',
Phone = '9743800309');
insert c;
Test.StartTest();
System.AssertNotEquals(Null, c.Id);
ApexPages.StandardController sc = new ApexPages.StandardController(c);
ReferalAccessclass refClass = new ReferalAccessclass(sc);
refClass.inputID = c.id;
refClass.firstName = c.id;
refClass.lastName = c.id;
refClass.email = c.id;
refClass.phone = c.id;
refClass.con = c;
refClass.saveInformation();
Test.StopTest();
}
}

WPF Datagrid get data on CheckboxChecked-Event

I've got a datagrid with a checkbox, name and email.
On CheckboxChecked I want to copy the email into another list or string.
How do I get the specific value from the checked row?
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
object row = lbxCC1.SelectedItem;
int columnIndex = lbxCC1.Columns.Single(c => c.Header.Equals("eMail")).DisplayIndex;
String eMail = (lbxCC1.SelectedCells[columnIndex].Column.GetCellContent(row) as TextBlock).Text;
MessageBox.Show(eMail);
}
Edit (09.09.2016):
Maybe I should show you a bit more code.
public class Person
{
public string Nachname { get; set; }
public string Vorname { get; set; }
public string eMail { get; set; }
public string Abteilung { get; set; }
}
public static class PersonService
{
public static List<Person> ReadFile(string filepath)
{
var lines = File.ReadAllLines(filepath);
var data = from l in lines.Skip(1)
let split = l.Split(';')
select new Person
{
Nachname = split[1],
Vorname = split[2],
eMail = split[31],
Abteilung = split[4],
};
return data.ToList();
}
}
I call it with:
lbxCC1.DataContext = PersonService.ReadFile(#"C:\Test.csv");
As I'm building the columns from code behind, I guess I have to bind them aswell am I right?
Sorry for this, but I'm new to datagrids :-)
I think this might help you:
Dim row As Data.DataRowView = DirectCast([yourDataGrid].SelectedItems(rowIndex), Data.DataRowView)
Then in your CheckBox_Checked Event:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show(row("email"); 'Assuming the column name is email
}
This is IF your values are data-bound to the DataGrid.

JAVA Google App Engine + Facebook API + GSON = Trouble with Javabean

I am trying to get the user's friends list from Facebook.
The problem seems to be the Javabean...
FBUser fbuser = new Gson().fromJson(jsonStr, FBUser.class);
public class FBUser implements Serializable {
private static final long serialVersionUID = -3154429420153433117L;
private String id;
private String name;
private String email;
private Friends friendsList = new Friends();
private FBUser() { }
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Data> getFriendsList() {
return friendsList.getData();
}
public static class Friends implements Serializable {
private static final long serialVersionUID = 6991758772193514527L;
private List<Data> data;
private Friends() { }
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
public class Paging implements Serializable {
private static final long serialVersionUID = 1689816298710621080L;
private String next;
private Paging() { }
public String getNext() {
return next;
}
public void setNext(String next) {
this.next = next;
}
}
}
public class Data implements Serializable {
private static final long serialVersionUID = -5008541658519841090L;
private String id;
private String name;
private Data() { }
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
Json:
json: {"id":"10861234","name":"Whatever","email":"whatever\u0040gmail.com","friends":{"data":[{"name":"Someone","id":"10861234"},{"name" ...43"}],"paging":{"next":"https:\/\/graph.facebook.com\/10861234\/friends..."}}}
The fields ID, Name and Email I can retrieve succesfully... but the friendsList is null... =(
Maybe it is the way I am trying to get it from the nested class, any suggestions on that?
There is no friendsList in your JSON (or, there's no friends in your Java class - whichever way you'd like to look at it). Gson silently ignores anything in the JSON that is not present in your classes.
You have a field friends whose value is an object. That object has a field data which is an array of objects and a field paging which is another object.
You need to write Java classes that match that structure. You're ... close.
In your FBUser class change:
private Friends friendsList = new Friends();
to:
private Friends friends = new Friends();
or:
#SerializedName("friends")
private Friends friendsList = new Friends();
Then in your Friends class you need to add:
private Paging paging = new Paging();
Also note that you don't have to initialize these values unless you specifically don't want them to be non-null when using these classes elsewhere.

Resources