DevExpress PropertyGridControl CustomEditor assignment - c#

Want to assign CustomEditor to PropertyGridControl. If I try using default way it does not work, however I am trying alternative which is not suitable for my case.
Class and Custom editor which is needs to be assigned to PropertyGridControl:
public class Foo
{
public Foo()
{
}
public Language LanguageOfFoo { get; set; }
}
public class Language
{
public Language()
{
}
public int ID { get; set; }
public string Name { get; set; }
}
public class RepositoryItemForLanguage : RepositoryItemLookUpEdit
{
public RepositoryItemForLanguage()
{
this.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "Name")}
);
this.DisplayMember = "Name";
List<Language> tmpList = new List<Language>();
tmpList.Add(new Language { ID = 1, Name = "AZE" });
tmpList.Add(new Language { ID = 2, Name = "ENG" });
tmpList.Add(new Language { ID = 1, Name = "RUS" });
this.DataSource = tmpList;
}
}
Default way:
private void button1_Click(object sender, EventArgs e)
{
propertyGridControl1.Rows.Clear();
propertyGridControl1.DefaultEditors.Clear();
propertyGridControl1.RepositoryItems.Clear();
propertyGridControl1.SelectedObject = null;
Foo f1 = new Foo();
f1.LanguageOfFoo = new Language();
RepositoryItemForLanguage repItem = new RepositoryItemForLanguage();
propertyGridControl1.RepositoryItems.Add(repItem);
propertyGridControl1.DefaultEditors.Add(typeof(Language), repItem);
propertyGridControl1.DefaultEditors.AddRange(new DevExpress.XtraVerticalGrid.Rows.DefaultEditor[] {
new DevExpress.XtraVerticalGrid.Rows.DefaultEditor(typeof(Language), repItem)});
propertyGridControl1.RepositoryItems.AddRange(new RepositoryItem[] { repItem});
propertyGridControl1.SelectedObject = f1;
}
Alternative method:
private void button1_Click(object sender, EventArgs e)
{
propertyGridControl1.Rows.Clear();
propertyGridControl1.DefaultEditors.Clear();
propertyGridControl1.RepositoryItems.Clear();
propertyGridControl1.SelectedObject = null;
Foo f1 = new Foo();
f1.LanguageOfFoo = new Language();
RepositoryItemForLanguage repItem = new RepositoryItemForLanguage();
propertyGridControl1.RepositoryItems.Add(repItem);
propertyGridControl1.SelectedObject = f1;
propertyGridControl1.GetRowByFieldName("LanguageOfFoo").Properties.RowEdit = repItem;
}
As you see it is possible only with "GetRowByFieldName" methods and specifying exact property name.
Appreciate for your help.

Related

Singleton property not updating during "OnClicked" event

I'm sure I remember this being a threading issue, but I can't find an answer. It seems like it should be simple. I have the following code:
private void Dingle_Clicked(object sender, RoutedEventArgs e)
{
dynamic doc = ScraperBrowser.Document;
string htmlText = doc.documentElement.InnerHtml;
htmlText = htmlText.Replace("\r\n", " ");
Regex targetStart = new Regex(this works just fine);
MatchCollection target = targetStart.Matches(htmlText);
string priceData = target[0].Value;
foreach (StorePriceData spData in Lists.Singleton.MedicineList[medIndex].Prices)
{
Regex rx = new Regex(spData.StoreName + #".+?(\$\d+\.\d+)");
MatchCollection matches = rx.Matches(priceData);
if (matches.Count > 0)
{
if (matches[0].Groups.Count > 0)
{
spData.MedicinePrice = matches[0].Groups[1].Value;
}
}
}
string cookie = Application.GetCookie(new Uri("https://www.goodrx.com"));
++medIndex;
ScraperBrowser.Navigate(Lists.Singleton.MedicineList[medIndex].GoodRxUrlString);
}
The problem I'm having is that the spData.MedicinePrice takes the value, but the value in the singleton "MedicineList" is not being updated. How can I make that value update?
The singleton code:
public class Lists
{
private static Lists _singleton;
public static Lists Singleton
{
get
{
if (_singleton == null) _singleton = new Lists(); return _singleton;
}
}
public List<MedicineInfo> MedicineList {
get
{
return new List<MedicineInfo>()
{
new MedicineInfo() { Name = "ZOLPIDEM TAB 10MG", Doses = "30 tablets" },
new MedicineInfo() { Name = "PANTOPRAZOLE TAB 40MG", Doses = "30 tablets" }
};
}
}
}
MedicineInfo class code:
public class MedicineInfo
{
public MedicineInfo()
{
Prices = new List<StorePriceData>()
{
new StorePriceData() { StoreName = "xxxx" },
new StorePriceData() { StoreName = "yyyy" },
new StorePriceData() { StoreName = "zzzz" },
};
}
public string Name { get; set; }
public string Doses { get; set; }
public List<StorePriceData> Prices { get; set; }
}
Thanks!
Carl
You are returning a new List<MedicineInfo> each time the getter of MedicineList is called.
Also, Lists is not really a singleton. A better implementation would look something like this:
public sealed class Lists
{
private static readonly Lists _singleton = new Lists();
private readonly List<MedicineInfo> _medicineList = new List<MedicineInfo>
{
new MedicineInfo() { Name = "ZOLPIDEM TAB 10MG", Doses = "30 tablets" },
new MedicineInfo() { Name = "PANTOPRAZOLE TAB 40MG", Doses = "30 tablets" }
};
private Lists() { }
public static Lists Singleton => _singleton;
public List<MedicineInfo> MedicineList => _medicineList;
}

Related comboboxes, add a third combobox

I'm trying to do a related combobox. I already have 2 comboboxes, but now I want to add a third.
I have this code for the 2nd combo box.
I'm using windows forms.
The entire code: https://repl.it/#devilonline/MuddyPartialBytecode#main.cs
private string[] GetCastById(int id)
{
return nomes.Where(line => line.movies_id== id).Select(l => l.nomes).ToArray();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
int id = nomes[comboBox1.SelectedIndex].id;
foreach (string name1 in GetCastById(id))
{
this.comboBox1.Items.Add(name1);
}
}
print
It is obvious that you are clearing the Items of the comboBox1 then try to get the id of the selected item, which should throw an exception because no item will be selected by then:
comboBox1.Items.Clear(); // here the items are cleared
int id = nomes[comboBox1.SelectedIndex].id; // nomes[comboBox1.SelectedIndex] = -1
Based on your database, the cast table is related to the movies so each movie has a corresponding list of cast, you should then get the id of the selected movie rather than the selected nome:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
int id = movies[comboBoxMovie.SelectedIndex].id; // here we used comboBoxMovie
foreach (string name1 in GetCastById(id))
{
this.comboBox1.Items.Add(name1);
}
}
This is a great opportunity to use databinding through a System.ComponentModel.BindingList<>. Below is a working example. Note, I've added get methods inside the classes for simplicity of populating the ComboBoxes for this example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
public class Form1
{
class Category
{
public int Id { get; set; }
public string Name { get; set; }
public Category(int id, string name)
{
this.Id = id;
this.Name = name;
}
public static List<Category> GetCategories()
{
return new List<Category>()
{
new Category(1, "Action"),
new Category(2, "Comedy")
};
}
}
class Movie
{
public int Id { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public Movie(int id, string name, int catId)
{
this.Id = id;
this.Name = name;
this.CategoryId = catId;
}
public static List<Movie> GetMovies()
{
return new List<Movie>()
{
new Movie(1, "Rambo", 1),
new Movie(2, "Delta Force", 1),
new Movie(3, "Elf", 2),
new Movie(4, "Space Balls", 2)
};
}
}
class Cast
{
public int Id { get; set; }
public string Names { get; set; }
public int MovieId { get; set; }
public Cast(int id, string names, int movieId)
{
this.Id = id;
this.Names = names;
this.MovieId = movieId;
}
public static List<Cast> GetCast()
{
return new List<Cast>()
{
new Cast(1, "Silvester Stalone", 1),
new Cast(2, "Chuck Norris", 2),
new Cast(3, "Will Farrell", 3),
new Cast(4, "John Candy", 4)
};
}
}
private BindingList<Category> _categoryBindingList = new BindingList<Category>();
private BindingList<Movie> _moviesBindingList = new BindingList<Movie>();
private BindingList<Cast> _castBindingList = new BindingList<Cast>();
private void Form1_Load(object sender, EventArgs e)
{
// Your database calls would replace these Get methods.
Category.GetCategories().ForEach(x => _categoryBindingList.Add(x));
Movie.GetMovies().ForEach(x => _moviesBindingList.Add(x));
Cast.GetCast().ForEach(x => _castBindingList.Add(x));
ComboBox1.DataSource = _categoryBindingList;
ComboBox1.DisplayMember = "Name";
ComboBox2.DataSource = _moviesBindingList.Where(x => x.CategoryId == (Category)ComboBox1.SelectedValue.Id).ToList();
ComboBox2.DisplayMember = "Name";
ComboBox3.DataSource = _castBindingList.Where(x => x.MovieId == (Movie)ComboBox2.SelectedValue.Id).ToList();
ComboBox3.DisplayMember = "Names";
}
private void ComboBox1_SelectedValueChanged(object sender, EventArgs e)
{
ComboBox2.DataSource = _moviesBindingList.Where(x => x.CategoryId == (Category)ComboBox1.SelectedValue.Id).ToList();
ComboBox2.DisplayMember = "Name";
ComboBox3.DataSource = _castBindingList.Where(x => x.MovieId == (Movie)ComboBox2.SelectedValue.Id).ToList();
ComboBox3.DisplayMember = "Names";
}
private void ComboBox2_SelectedValueChanged(object sender, EventArgs e)
{
ComboBox3.DataSource = _castBindingList.Where(x => x.MovieId == (Movie)ComboBox2.SelectedValue.Id).ToList();
ComboBox3.DisplayMember = "Names";
}
}

Dropdown has a SelectedValue which is invalid because it does not exist in the list of items, even though I cleared the selected items (C#)

I'm getting this error on DataBind(), and I don't know why since there shouldn't be anything selected.
DdState.Items.Clear();
DdState.DataSource = UsStates;
DdState.DataTextField = "Title";
DdState.DataValueField = "Title";
DdState.Items.Insert(0, String.Empty);
if (DdState.SelectedItem != null)
{
DdState.SelectedItem.Selected = false;
}
DdState.DataBind();
private IEnumerable<IStateItem> UsStates
{
get
{
var statesFolder = _sitecoreService.GetItem<ISitecoreItem>(ItemReference.BcsUs_ProductData_States.Guid);
if (statesFolder == null)
return new List<IStateItem>();
List<IStateItem> usStates = _sitecoreService.QueryChildren<IStateItem>(statesFolder).OrderBy(s => s.Title).ToList();
return usStates;
}
}
I tried putting in DdState.SelectedIndex = 0 before the DataBind(), but then I got an error that the selected index did not exist. What's going on?
If the DataSource is a list its much easier to implement. So just "convert" the UsStates IEnumerable to a List an then add it to the data source.
DdState.DataSource = UsStates.ToList();
Then choose the property of a list item as binding.
OR
public Form1()
{
InitializeComponent();
DdState.Items.Clear();
DdState.DataSource = UsStates;
DdState.DisplayMember = "Statename";
DdState.SelectedIndex = 0;
}
private List<IStateItem> UsStates
{
get
{
List<IStateItem> usStates = new List<IStateItem>();
usStates.Add(new IStateItem("California","status1"));
usStates.Add(new IStateItem("Ohio", "status3"));
return usStates;
}
}
private class IStateItem
{
public IStateItem(string statename, string stateStatus)
{
Statename = statename;
StateStatus = stateStatus;
}
public string Statename { get; set; }
public string StateStatus { get; set; }
}
Could there be something wrong with your IStateItem class?
I copy/pasted your code in a new asp.net application, made my own IStateItem class and it works.
using System;
using System.Collections.Generic;
namespace TestIt
{
public partial class Form1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillTheList();
}
private void FillTheList()
{
ddl_TheList.Items.Clear();
ddl_TheList.DataSource = UsStates;
ddl_TheList.DataTextField = "statename";
ddl_TheList.DataValueField = "stateStatus";
//ddl_TheList.Items.Insert(0, String.Empty);
ddl_TheList.DataBind();
ddl_TheList.SelectedIndex = 0;
}
private IEnumerable<IStateItem> UsStates
{
get
{
List<IStateItem> usStates = new List<IStateItem>();
for (int i = 0; i < 10; i++)
{
usStates.Add(new IStateItem { statename = "state #" + i, stateStatus = "cool state bro" });
}
return usStates;
}
}
}
public class IStateItem
{
public string statename { get; set; }
public string stateStatus { get; set; }
}
}

EF Use values in WPF listbox

I Have one table of data:
tblFeed
Id
Title
Content
And I populated a Listbox in my WPF application with this table.
I have the issue now of using the Id value for an event but the Id keeps returning 0.
Any Suggestions?
WCF
public List<Feed> GetFeed()
{
List<Feed> r = new List<Feed>();
List<Feed> e;
using (TruckDb db = new TruckDb())
e = db.Feed.Where(x => x.Id != null).ToList();
foreach (var a in e)
{
var feed = new Feed()
{
Id = a.Id,
Title = a.Title,
Content = a.Content
};
r.Add(feed);
}
return r;
}
WPF
public async Task LoadFeeds()
{
TruckServiceClient TSC = new TruckServiceClient();
try
{
List<ClientItems> feeditems = new List<ClientItems>();
if (lbFeed.Items.Count <= 0)
foreach (var item in await TSC.GetFeedAsync())
{
feeditems.Add(new ClientItems
{
FId = item.Id,
FTitle = item.Title,
FContent = item.Content
});
}
lbFeed.ItemsSource = (feeditems.ToArray());
lbFeed.DisplayMemberPath = "FTitle";
}
catch (Exception)
{
throw;
}
}
public class ClientItems
{
public int FId { get; set; }
public string FTitle { get; set; }
public string FContent { get; set; }
public override string ToString()
{
return FTitle;
}
}
Delete Event
WCF
private void bnFeedDel_Click(object sender, RoutedEventArgs e)
{
TruckServiceClient service = new TruckServiceClient();
service.DelFeedAsync(new FeedView
{
Id = lbFeed.SelectedIndex
});
}
WPF
public void DelFeed(FeedView feedview)
{
using (var result = new TruckDb())
{
var t = new Feed
{
Id = feedview.Id
};
result.Feed.Remove(t);
result.SaveChanges();
}
}
In your bnFeedDel_Click method you are doing this:
Id = lbFeed.SelectedIndex
I think this is your problem as you don't want to set Id to a SelectedIndex value but rather:
[EDIT after some discussion]
Set SelectedValuePath inside LoadFeeds:
lbFeed.SelectedValuePath = "FId";
And use SelectedValue instead of SelectedIndex:
private void bnFeedDel_Click(object sender, RoutedEventArgs e)
{
TruckServiceClient service = new TruckServiceClient();
service.DelFeedAsync(new FeedView
{
// Of course you may want to check for nulls etc...
Id = (int)lbFeed.SelectedValue;
});
}
Also, you should use DbSet.Attatch() before deleting a record:
public void DelFeed(FeedView feedview)
{
using (var result = new TruckDb())
{
var t = new Feed
{
Id = feedview.Id
};
result.Feed.Attatch(t);
result.Feed.Remove(t);
result.SaveChanges();
}
}

ListBox selector odd behavior when there are dupes

I'm working on a bigger project atm, but I made this simple example to show you what happens..
using System.Collections.Generic;
using System.Windows;
namespace txt
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var obsLst = new List<Info> { new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" } };
var temp = new List<Info>();
for (var i = 1; i <= 3; i++)
{
temp.Add(obsLst[0]); //I add 3 of the same item from obsLst to temp
}
lst.DataContext = temp; //lst = ListBox
}
}
public class Info
{
public string name { get; set; }
}
}
The ListBox ItemsSource is set to {Binding}..
When I start the application I get 3 txt.Info objects displayed and if I click any of them, 2 or even all of them get selected aswell. From my understanding the problem relies in the fact that the listbox selector cannot differentiate between the items and therefor doesn't know which one I selected.
Here's a picture of what it looks like..
I only clicked on the second txt.Info item.
I found a solution where someone said that I have to specify the DisplayMemberPath, but I can't really do that in the other project because I have a datatemplate for the object.
Any ideas on how I could fix this would be great..
Thx in advance.
EDIT 1:
this works but it's not nice..
using System.Collections.Generic;
using System.Windows;
namespace txt
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var obsLst = new List<Info> { new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" } };
var temp = new List<Container>();
for (var i = 1; i <= 3; i++)
{
var t = new Container();
t.obj = obsLst[0];
temp.Add(t);
}
lst.DataContext = temp;
}
}
public class Info
{
public string name { get; set; }
}
public class Container
{
public Info obj { get; set; }
}
}
In this case you need to set DisplayMemberPath="obj"
Assigning an ID to the object, doesn't work..
using System;
using System.Collections.Generic;
using System.Windows;
namespace txt
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var rand = new Random();
var obsLst = new List<Info> { new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" }, new Info { name = "asd" } };
var temp = new List<Info>();
for (var i = 1; i <= 3; i++)
{
obsLst[0].id = rand.Next(10000);
temp.Add(obsLst[0]);
}
lst.DataContext = temp;
}
}
public class Info
{
public string name { get; set; }
public int id { get; set; }
}
}
I had this problem too a while ago, i fixed it by adding the id # to the item so they're always different.

Categories