I'm creating my own user control in a asp.net project and have two asp list elements in this control that I want to set a data source from another class' list objects.
My exception error is:
DataBinding: 'TeamTracker.Flash.ErrorMessage' does not contain a property with the name 'message'.
The user control looks like this:
public partial class flashMessage : System.Web.UI.UserControl
{
protected void Page_PreRender (object sender, EventArgs e)
{
TTPage page = (TTPage)this.Page;
WebFlash flash = page.flashMessages;
messages.DataSource = flash.NoticeMessages;
messages.DataTextField = "message";
messages.DataValueField = "name";
messages.DataBind();
errorMessages.DataSource = flash.ErrorMessages;
errorMessages.DataTextField = "message";
errorMessages.DataValueField = "name";
errorMessages.DataBind();
}
}
ErrorMessage is the class bellow:
namespace TeamTracker.Flash
{
public class ErrorMessage : Message
{
public ErrorMessage(string message)
{
this.message = message;
}
}
}
The message property is defined in the parent abstract class
namespace TeamTracker.Flash
{
public abstract class Message
{
public string name, message, colour;
}
}
Is there a reason why the data Bind cant see this property "message"? The error occurred on the errorMessages bind as it had two objects in the list as NoticeMessages had 0.
You need to use public properties for databinding,
public abstract class Message
{
public string Name { get; set; }
public string Message { get; set; }
public string Colour { get; set; }
}
if you can't change the fields to properties, then do as below.
errorMessages.DataSource = flash.ErrorMessages.Select(x=>
new { Message = x.message, Name = x.name}).ToList();
errorMessages.DataTextField = "Message";
errorMessages.DataValueField = "Name";
It looks like the binding expects a property. name, message and colour are fields.
What it looks like you need is something like this:
public string name
{
get;
set;
}
public string message
{
get;
set;
}
public string colour
{
get;
set;
}
Related
This seems like it should be straight forward, but I can't seem to get the Models Entities type and name?
I can get the class name/type, but not the properties inside the class which I am accessing while editing the DataGrid.
This is the event in which I am trying to get the values:
protected void OnDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
var userControl = FindVisualParent<UserView>(sender as UIElement);
var rowType = e.Row.Item.GetType(); //This gets the class type
//trying to get name and type of entity here
var name = e.Row.Item. ??????
var type = e.Row.Item.GetType(). ??????
}
}
And this is the Model, I am using code first EntityFrameWork
public partial class Bank
{
public System.Guid Id { get; set; } // ID (Primary key)
public string Section { get; set; } // Section (length: 50)
public string Name { get; set; } // Name (length: 50)
public Bank()
{
InitializePartial();
}
partial void InitializePartial();
}
To try to make things a little more clearer, I am trying to get the Property name and type of of the Property named Section in the above class. How do I do this from the OnDataGrid_CellEditEnding event?
I was trying to populate a DevExpress gridview with some datas.
Let's say we have two classes :
public class ObjectA
{
public string Name { get; set; }
public List<ObjectB> Details1 { get; set; }
public List<ObjectB> Details2 { get; set; }
}
public class ObjectB
{
public string Name { get; set; }
public string Description { get; set; }
}
And a form created like that :
private List<ObjectA> datas;
public Form1()
{
InitializeComponent();
// Fill data
datas = ...
// Set datasource
this.gridControl1.DataSource = datas;
this.gridView1.BestFitColumns();
this.SetRelation();
}
private void SetRelation()
{
GridView customPatternView = new GridView(gridControl1);
customPatternView.Columns.AddField("Name").VisibleIndex = 0;
customPatternView.Columns.AddField("Description").VisibleIndex = 1;
this.gridControl1.LevelTree.Nodes.Add("CustomRelation", customPatternView);
}
This code is working well but I'm obly able to display Details1 in the details view.
How can I do to display only Details2 ?
Thanks
You need one GridView for each detail view.
gridControl1.LevelTree.Nodes.Add("Details1", customPatternView);
gridControl1.LevelTree.Nodes.Add("Details2", customPatternView);
You can use GridControl.ShowOnlyPredefinedDetails property. If you set this property to true then the GridControl displays only the relationships that exist in the LevelTree. Also, use the name of your child list property as relation name in GridControl.LevelTree.Nodes.Add method.
Here is example:
private void SetRelation()
{
var customPatternView = new GridView(gridControl1);
customPatternView.Columns.AddField("Name").VisibleIndex = 0;
customPatternView.Columns.AddField("Description").VisibleIndex = 1;
this.gridControl1.LevelTree.Nodes.Add("Details2", customPatternView);
this.gridControl1.ShowOnlyPredefinedDetails = true;
}
I want to make public property that gets and sets object I get from datagrid.
I have datagrid that has 3 columns with text and 3 columns with checkboxes.
When I check one of the checkbox I get a value of whole row in list like this:
var selectedItemsController = MyObsCollection.Where(n => n.Controller).ToList();
That is a list of objects with all 3 string values (and all 3 bool values of checkboxes) that are in same row where checkboxes in column named Controller is.
MyObsCollection is also public property for ObservableCollection and they are defined like this:
ObservableCollection<RowData> _obsCollection =
new ObservableCollection<RowData>();
public ObservableCollection<RowData> MyObsCollection
{
get { return _obsCollection; }
}
RowData is my class that caries a model I need and it's defined like this:
public class RowData : INotifyPropertyChanged
{
public string Type { get; set; }
public string MapTo { get; set; }
public string Name { get; set; }
public bool Controller { get; set; }
public bool Service { get; set; }
public bool Injection { get; set; }
public RowData(string type, string mapTo, string name)
{
Type = type;
MapTo = mapTo;
Name = name;
}
What I'm trying to do is to make a public property for that list of objects(in selectedItemsController) so I can use it in other class.
For example I was doing this with name for some Area that is also part of WindowsForm. I was taking the name from some text box and making public property like this:
public string AreaName
{
get { return AreaNameValue.Text; }
set { AreaNameValue.Text = value; }
}
And after that I was able to do this in other class:
var areaName = areaDialog.AreaName.Trim();
So finally my question is does someone know how can I make same public property for DataRow object if the name of DataGrid is for example: tabela? Is there something already defined in DataGrid that I can use? (like 'Text' property is for InputTextBoxes).
You can set your selectedItemsController as the datacontext of your DataGrid. Then you can set and get values on the selectedItemsController itself.
i've got 3 classes (all deriving from the same base class) and i have to dynamicly fill a ListBox with the Property-Names.
I've tried like this
class Test : TestBase {
[NameAttribute("Name of the Person")]
public string PersonName { get; set; }
private DateTime Birthday { get; set; }
[NameAttribute("Birthday of the Person")]
public string PersonBDay {
get {
return this.bDay.ToShortDateString();
}
}
}
...
[AttributeUsage(AttributeTargets.Property)]
public class NameAttribute : Attribute {
public string Name { get; private set; }
public NameAttribute(string name) {
this.Name = name;
}
}
Is there a possibility to look in my object for all properties which has the attribute NameAttribute and get the string form the Name property of NameAttribute?
You can inspect each property from Type.GetProperties and then filter the ones that have the required attribute with the MemberInfo.GetCustomAttributes method.
With a little bit of LINQ, this would look like:
var propNameTuples = from property in typeof(Test).GetProperties()
let nameAttribute = (NameAttribute)property.GetCustomAttributes
(typeof(NameAttribute), false).SingleOrDefault()
where nameAttribute != null
select new { Property = property, nameAttribute.Name };
foreach (var propNameTuple in propNameTuples)
{
Console.WriteLine("Property: {0} Name: {1}",
propNameTuple.Property.Name, propNameTuple.Name);
}
By the way, I also recommend declaring the attribute to be single-use only with AllowMultiple = false in the AttributeUsage decoration.
I have a form (CustomerInfoForm) with 10 TextBoxes. The default Text property for each of the TextBoxes is defined at design-time. A subclass CustomerInfoForm.CustomerInfo contains properties to hold the data entered in the form. The subclass containing the data will be serialized to XML.
In the automatically generated form code, each of the text boxes has a line of code to bind the datasource to the text box
this.customerInfoBindingSource = new System.Windows.Forms.BindingSource(this.components);
Code automatically generated by the C# ide for each text box:
this.txtCustomer.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.customerInfoForm_CustomerInfoBindingSource, "CustomerName", true));
this.txtCustomer.Location = new System.Drawing.Point(60, 23);
this.txtCustomer.Name = "txtCustomer";
this.txtCustomer.Size = new System.Drawing.Size(257, 20);
this.txtCustomer.TabIndex = 0;
this.txtCustomer.Text = "CustomerName";
(I noticed that the Text property isn't set until after the DataBinding) in the IDE generated code.
When I run the project, the form is displayed with the default values in the TextBoxes. However when the SaveButton is pressed to serialize the properties in the MyForm.CustomerInfo subclass, they are all null. Since these values will only be changed from the form I was hoping that I didn't have to implement the interface INotifyPropertyChanged.
Am I missing something basic or simple?
The code for the form including the serialization of the data is attached below
using System;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;
namespace SimpleCustomerInfo
{
// You must apply a DataContractAttribute or SerializableAttribute
// to a class to have it serialized by the DataContractSerializer.
public partial class CustomerInfoForm : Form
{
CustomerInfo ci = new CustomerInfo();
public CustomerInfoForm()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(CustomerInfo));
FileStream writer = new FileStream(#"C:\Users\Me\temp\testme.xml", FileMode.Create);
serializer.WriteObject(writer,ci);
writer.Close();
}
[DataContract(Name = "Customer", Namespace = "net.ElectronicCanvas")]
public class CustomerInfo
{
[DataMember]
public string CustomerName { get; set; }
[DataMember]
public PhoneInfo PhonePrimary { get; set; }
[DataMember]
public PhoneInfo PhoneDays { get; set; }
[DataMember]
public PhoneInfo PhoneEvening { get; set; }
}
public class PhoneInfo
{
public string number { get; set; }
public string type { get; set; }
public bool textOk { get; set; }
}
}
}
EDIT -- For others that may happen upon this question
using System;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;
namespace SimpleCustomerInfo
{
public partial class CustomerInfoForm : Form
{
CustomerInfo ci;
public CustomerInfoForm()
{
InitializeComponent();
ci = new CustomerInfo();
ci.CustomerName = "My Customer Name";
ci.PhoneDays.number = "888-888-8888";
customerInfoForm_CustomerInfoBindingSource.DataSource = ci;
}
private void btnSave_Click(object sender, EventArgs e)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(CustomerInfo));
FileStream writer = new FileStream(#"C:\Users\me\temp\testme.xml", FileMode.Create);
serializer.WriteObject(writer,ci);
writer.Close();
}
// You must apply a DataContractAttribute or SerializableAttribute
// to a class to have it serialized by the DataContractSerializer.
[DataContract(Name = "Customer", Namespace = "net.ElectronicCanvas")]
public class CustomerInfo
{
[DataMember]
public string CustomerName { get; set; }
[DataMember]
public PhoneInfo PhonePrimary { get; set; }
[DataMember]
public PhoneInfo PhoneDays { get; set; }
[DataMember]
public PhoneInfo PhoneEvening { get; set; }
// Constructor is needed to instantiate the PhoneInfo classes
// within the CustomerInfo class
public CustomerInfo()
{
PhonePrimary = new PhoneInfo();
PhoneDays = new PhoneInfo();
PhoneEvening = new PhoneInfo();
}
}
public class PhoneInfo
{
public string number { get; set; }
public string type { get; set; }
public bool textOk { get; set; }
}
}
}
First of all you need to decide whether to use databinding or manipulate Text property directly. Those two approaches should not be mixed together.
If you want to use databinding than you are missing one line in the code:
public Form1()
{
InitializeComponent();
customerInfoBindingSource.DataSource = ci; // This is the missing line
}
You need to let your customerInfoBindingSource know about the data source.
If you add this line, then the Text that is assigned in design time will be overridden by the text from your bound data source. If you want to use binding you should manipulate with the data source instead of setting Text fields directly. Like this:
public Form1()
{
InitializeComponent();
ci.CustomerName = "TestCustomerName";
customerInfoBindingSource.DataSource = ci;
}