DevExpress LookUpEdit SelectedText Problem - devexpress

I have some lookupedits binded to some lists where the user can choose values and then save in database. I use EditValueChanged events to handle the values. So far all good!
Now i need to grab the values from the database and assign them to the lookupedits. I don't use BindingSource for the whole object cause lookupedits are binded to independent lists.
As i supposed and read from the documentation, SelectedText is what i need, but when I'm assigning the string i want, it just don't work and sets an empty string. Same Behavior for the DateEdit control, I'm assigning a DateTime value and seems to have this value but doesn't shows it. I could set the EditValue property but i get nothing showed up in the LookUpEdit again.
How to force the LookUpEdit to show me the value i want, basically go to the row with the value i set and show the text in the editor too, or set the SelectedText and match it with its list and show it!
I guess this should be easier...Any help appreciated!
Example:
myLookUpEdit.SelectedText = "George" // The LookUpEdit is Binded to a List<Names> and has the name George.
Thank you

Whenever I am setting the value of a LookupEdit I use EditValue.
You need to make sure that you set the ValueMember property of the LookupEdit to whatever you want to appear in EditValue. DisplayMember will what is displayed when the LoodupEdit is closed. You can pass in a string to the name of the property you want in your object to these properties.
Setting the SelectedText value has the same effect as typing into the control as far as I am aware.
public partial class Form1 : Form
{
List<Name> MyNames = new List<Name>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MyNames.Add(new Name("John", "Smith"));
MyNames.Add(new Name("John", "Doe"));
MyNames.Add(new Name("Jane", "Doe"));
MyNames.Add(new Name("Jane", "Smith"));
lookUpEdit1.Properties.DataSource = MyNames;
lookUpEdit1.Properties.DisplayMember = "FirstName";
lookUpEdit1.Properties.ValueMember = "FirstName";
}
private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
string mystring = lookUpEdit1.EditValue.ToString();
lookUpEdit1.EditValue = mystring;
}
}
public class Name
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Name(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
}

Related

Select ID from Picker

I am getting values in picker from an API, text and Value are two fields as I declared ! I am able to see values in it ! I want that whenever I select item from It, I am able to fetch respective value of that field !
async void CallInspectionMaster()
{
string Url = "192.168.xx.xx/api/QMSin/GetInspectionMasterList";
var data = await Url.GetJsonAsync<List<MyClass>>();
InspectionMasterPicker.ItemsSource = data;
InspectionMasterPicker.ItemDisplayBinding = new Binding("Text");
Binding selecteditemx = new Binding("InspectionMaster");
selecteditemx.Mode = BindingMode.TwoWay;
selecteditemx.Source = InspectionMasterPicker;
}
public class MyClass
{
public string Value { get; set; }
public string Text { get; set; }
}
I have to display text but fetch value so I can pass it other functions ! How do do that ?
assign a handler to the SelectedIndexChanged event
InspectionMasterPicker.SelectedIndexChanged += PickerSelect;
then create the handler
protected void PickerSelect(object sender, EventArgs args)
{
var item = (MyClass)InspectionMasterPicker.SelectedItem;
...
}
there is a complete example included in the docs

UWP: How to map arbitrary data to Telerik RadDataGrid

So I have a situation in which I am receiving a collection of Dictionary(string, string) entries where the key of each entry is the column name & value the, well, value. I want to push these to a RadDataGrid so that each dictionary maps to a row. If I knew what/how many columns I'd be getting in advance, I'd just map them to an object and have done with it. Unfortunately, it could be different every time, so that won't work.
So far I'm having no luck. I've tried mapping it*(the collection) directly, converting it to dynamic objects & XMLDocument, none of which worked. Also just got the Fall Creators Update & tried mapping it to a DataTable, no luck there either.
I've been experimenting with mapping the DataTable's DefaultView to the grid's ItemsSource after manually adding columns, but while I get the right # of columns and headers, I still don't get the field values. Not sure where to go next.
Mind you, I'm not married to Telerik. If someone else knows a suitably usable UWP data grid solution that will let me map arbitrary data like this, I'd love to hear about it.
Example using a standard UWP app:
MainPage.xaml:
<Page xmlns:my="using:Telerik.UI.Xaml.Controls.Grid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestTelerikDataGrid"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ComponentModel="using:System.ComponentModel"
x:Class="TestTelerikDataGrid.MainPage"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,0,0,125">
<my:RadDataGrid Margin="0,0,0,-125" x:Name="dataGrid" AutoGenerateColumns="False" >
</my:RadDataGrid>
</Grid>
</Page>
And the back-end:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace TestTelerikDataGrid {
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page {
private ObservableCollection<Dictionary<string, string>> items = new ObservableCollection<Dictionary<string, string>>();
public ObservableCollection<Dictionary<string, string>> ItemDictionary {
get {
return items;
}
set {
items = value;
}
}
public DataTable Items { get; set; }
public MainPage() {
this.InitializeComponent();
CreateItems(); // creates sample data structurally identical to what we'll get in the actual app (i.e., obsv. collection of dictionaries)
CreateTable(); // attempt to take the collection created above and map it to the RadDataGrid
}
private void CreateItems() {
for (int i = 0; i < 5; i++) {
Dictionary<string, string> row = new Dictionary<string, string>();
row["A"] = "A" + i.ToString();
row["B"] = "B" + i.ToString();
row["C"] = "C" + i.ToString();
ItemDictionary.Add(row);
}
}
private void CreateTable() {
Items = new DataTable();
if (ItemDictionary.Count == 0) return;
foreach (KeyValuePair<string, string> entry in ItemDictionary[0]) {
DataColumn column = new DataColumn(entry.Key);
Items.Columns.Add(column);
Telerik.UI.Xaml.Controls.Grid.DataGridTextColumn dgc = new Telerik.UI.Xaml.Controls.Grid.DataGridTextColumn();
dgc.Name = entry.Key;
dgc.Header = entry.Key;
dgc.PropertyName = entry.Key;
dataGrid.Columns.Add(dgc);
}
foreach (Dictionary<string, string> rowEntry in ItemDictionary) {
DataRow row = Items.NewRow();
int col = 0;
foreach (KeyValuePair<string, string> entry in rowEntry) {
row[entry.Key] = entry.Value;
}
Items.Rows.Add(row);
}
DataView dv = Items.DefaultView;
dataGrid.ItemsSource = dv;
}
}
}
Ideally, this will result in a table with 5 rows, 3 columns (A, B, C) and the fields showing the correct value (e.g., first row reading A0, B0, C0).
I've been experimenting with mapping the DataTable's DefaultView to the grid's ItemsSource after manually adding columns, but while I get the right # of columns and headers, I still don't get the field values. Not sure where to go next.
Those are all part of UWP 2.0 that came with the Fall Creators Update. I believe it's build 16299 or something like that. Sorry, should have mentioned that's the build I'm using.
The RaDataGrid doesn't support setting DataTable or DataView as ItemsSource directly. You need to cast to an IEnumerble collection instead of DataView.
Please see this thread on Telerik forum for more details: binding-dictionary-to-raddatagrid
In the end, I wound up creating a generic "GridRow" class with properties "Item00"..."Item99" and just mapped the data to that instead. It's a pill but it works. Just putting this here for the next person.
public class GridRow {
public Int32 Index { get; set; }
public string Item00 { get; set; }
public string Item01 { get; set; }
public string Item02 { get; set; }
...etc
}
public ObservableCollection<GridRow> GridData { get; set; }
And here's how you populate that:
GridData = new ObservableCollection<GridRow>();
foreach (Dictionary<string, string> record in ViewModel.ItemsSource) {
GridRow gridRow = new GridRow();
gridRow.Index = rowIndex;
colIndex = 0;
foreach (DataGridHeader header in ViewModel.Headers) {
gridRow.GetType().GetProperty(string.Format("Item{0:D2}", colIndex)).SetValue(gridRow, record[header.Name]);
colIndex += 1;
}
GridData.Add(gridRow);
rowIndex += 1;
}
You get the idea.

Select many CheckBox on GridPanel Ext.Net (ASP.NET)

How to select Rows on GridPanel using ext.net Library on ASP.NET.
When i put this code on Page_Load event, it's work fine :
protected void Page_Load(object sender, EventArgs e)
{
RowSelectionModel sm = gridRolesPermission.GetSelectionModel() as RowSelectionModel;
sm.SelectedRows.Add(new SelectedRow(1));
sm.SelectedRows.Add(new SelectedRow(2));
}
but when i would to select CheckBoxs on Ajax event, it's not working !
[DirectMethod]
public void FillPermissionForSelectedRole()
{
RowSelectionModel sm = gridRolesPermission.GetSelectionModel() as RowSelectionModel;
sm.SelectedRows.Add(new SelectedRow(1));
sm.SelectedRows.Add(new SelectedRow(2));
}
On view :
....
<Listeners>
<Command Handler="App.direct.FillPermissionForSelectedRole();" />
</Listeners>
...
Any Help Please !
In your example,when u click the button(directMethod button),a pamatercollection sent to the server called CheckboxSelectionModel1 .on server side, u can get the this parameter collection like this.
string hh= HttpContext.Current.Request["CheckboxSelectionModel1"];
*CheckboxSelectionModel1 is the model name of the grid on your example.
in your case like that
[DirectMethod]
public void FillPermissionForSelectedRole()
{
string hh= HttpContext.Current.Request["CheckboxSelectionModel1"];
}
string hh should be something like this depents on the what u checked on grid.
"[{"RecordID":5,"RowIndex":5},{"RecordID":7,"RowIndex":7},{"RecordID":3,"RowIndex":3}]"
later u can desrialize this string and get the RecordID something like this
var rw = JSON.Deserialize<Row[]>(hh);
string txt = "";
foreach (var item in rw)
{
txt += item.RecordID;
}
and the row class
public class Row{
public string RecordID { get; set; }
public string RowIndex { get; set; }
}
to resolve this probleme i should add this line to upadate modifications :
sm.UpdateSelection();

Bind custom object of list to ListBox

All, Say you have a list of object which class is named Person.
public class Person
{
public string name{get;set;}
public string id{get;set;}
public int age{get;set;}
public Major major{get;set;}
}
public class Major{
public string MajorName{get;set;}
public string MajorId{get;set;}
}
And Say you have a ListBox which id is personListBox. and It is trying to bind with the List of Person.
List<Person> list= new List<Person>();
list.add(...);
...
list.add(...);
personListBox.DataSource=list;
personListBox.DataTextField = "name";
personListBox.DataValueField = "id";
personListBox.DataBind();
But My question is How can I convert the selected Item to Person?
What I image the code looks like below.
protected void personListBox_SelectedIndexChanged(object sender, EventArgs e)
{
//Person item = (Person)lbBizCategory.SelectedItem;
//string majorName = item.major.MajorName;
}
Unfortunatedly, It doesn't work. Is there any way to make it ? thanks.
You probably still have a reference to your list. Try
protected void personListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Person yourPerson = list.First(x => x.id == int.Parse(personListBox.SelectedValue));
}
We cannot directly typecast the selectedItem to the Person type..!!
Instead, we can have a Person object created
and assign the value of the object's property to the selectedItem, which is like:
Person person=new Person();
person.name=personListBox.SelectedItem.ToString();
By the way, we cannot add values to the list as you wrote:
List list= new List();
list.add(...);
Since list can now accept only Person type; so we need to create person object,
fill the object's properties and later add the object to the list as:
Person person=new Person();
person.name="sagar";
person.id=1;
....
list.add(person);

DataBinding a DateTimePicker raises "DataBinding cannot find a row in the list that is suitable for all bindings."

I have a simple test application which reproduces an error I encountered recently. Basically I have a simple WinForm with databound TextBox and DateTimePicker controls, and a button. When I execute the code below (on the button click), I get the error "DataBinding cannot find a row in the list that is suitable for all bindings". If I move the DataSource assignment into the form's constructor, I don't get the error.
If I remove the data binding for the DateTimePicker, it works fine.
Can anyone explain what the problem is ?
public partial class Form1 : Form
{
private BindingSource bs;
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
bs = new BindingSource();
bs.DataSource = typeof(Thing);
this.textBox1.DataBindings.Add("Text", bs, "MyString");
this.dateTimePicker1.DataBindings.Add(new Binding("Value", bs, "MyDate"));
//Thing thing = new Thing { MyString = "Hello", MyNumber = 123, MyDate = DateTime.Parse("01-Jan-1970") };
//bs.DataSource = thing;
}
private void button1_Click(object sender, EventArgs e)
{
Thing thing = new Thing { MyString = "Hello", MyNumber = 123, MyDate = DateTime.Parse("01-Jan-1970") };
bs.DataSource = thing;
}
}
public partial class Thing
{
public String MyString { get; set; }
public Int32 MyNumber { get; set; }
public DateTime MyDate { get; set; }
}
}
Thanks
Edit:
It seems that if I change the data binding for the DateTimePicker control such that I bind to the "Text" property, the problem goes away. I don't understand why that would be though, because "Value" is valid for data binding.

Resources