WPF listbox binding to listview - data-binding

I have below classes:
public class BusinessFunction
{
private string str_Id;
public string id
{
get { return str_Id; }
set { str_Id = value; }
}
private List<Function> str_FunctionList;
public BusinessFunction() { }
public List<Function> functionList
{
get { return str_FunctionList; }
set { str_FunctionList = value; }
}
}
public class Function
{
private string str_FunctionType;
public string functionType
{
get { return str_FunctionType; }
set { str_FunctionType = value; }
}
private string str_FunctionName;
public string functionName
{
get { return str_FunctionName; }
set { str_FunctionName = value; }
}
private string str_Value;
public string value
{
get { return str_Value; }
set { str_Value = value; }
}
}
I have to bind the above data into Listbox and Listview. The "id" should display in listbox and the "functionList" in listview. When user changes the selection in listbox, it should bring the associated "functionList" into listview.
How to achieve this scenario?
Thanks

Listview Itemsource={Binding functionList} did the trick.

Related

How to retrieve an object's Property name/value pairs on a custom object?

I have a custom object with varying datatypes for each property.
I would like to be able to do something like:
public void evalCI(configurationItem CI)
{
foreach (PropertyInformation n in CI)
{
Response.Write(n.Name.ToString() + ": " + n.Value.ToString() + "</br>");
}
}
My custom object is:
public class configurationItem : IEnumerable
{
private string serial;
private string model;
private DateTime? wstart;
private DateTime? wend;
private Int32 daysLeft;
private string platform;
private string productVersion;
private string manufacturer;
private bool verificationFlag;
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public string Serial
{
set { serial = value; }
get { return serial; }
}
public string Model
{
set { model = value; }
get { return model; }
}
public DateTime? Wstart
{
set { wstart = value; }
get { return wstart; }
}
public DateTime? Wend
{
set { wend = value; }
get { return wend; }
}
public Int32 DaysLeft
{
set { daysLeft = value; }
get { return daysLeft; }
}
public string Platform
{
set { platform = value; }
get { return platform; }
}
public string ProductVersion
{
set { productVersion = value; }
get { return productVersion; }
}
public string Manufacturer
{
set { manufacturer = value; }
get { return manufacturer; }
}
public bool VerificationFlag
{
set { verificationFlag = value; }
get { return verificationFlag; }
}
My expected output would be:
-Serial: 1234567
-Model: Mustang
-Wstart: 12/12/2005
-Wend: 12/11/2006
-DaysLeft: 0
-Platform: Car
-ProductVersion: GT
-Manufacturer: Ford
-VerificationFlag: true
At first I was getting an error that GetEnumerator() had to be implemented to use a foreach loop. The problem I keep running into is that all of the examples of Indexed Properties are of a single property with an indexable list, instead of an index for each property in the object. I was able to get intellisense to give me methods for PropertyInfo by adding:
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
However, the 2nd GetEnumerator() throws:
Compiler Error Message: CS0103: The name 'GetEnumerator' does not exist in the current context.
What am I missing here? How do I modify my object to give me the results I expect from evalCI()?
You don't need to implement IEnumerable. What you do need to do is use Reflection.
This is from memory, but I believe it would look like this:
foreach (PropertyInfo n in typeof(configurationItem).GetProperties())
{
Response.Write(string.Format("{0}: {1}<br/>", n.Name, n.GetValue(CI, null)));
}
This - the code as written - will also only give you public properties, and non-indexed properties (but it doesn't look like you have any indexed properties).

Interface Implementation Issue

I Have a Base Interface Like this
public interface IHRMISBaseConnector
{
IHRMISEmployeeConnector EmployeeConnector { get ; set; }
}
And i have one more interface like this
public interface IHRMISEmployeeConnector
{
String Add(EmployeeDetails e);
Boolean Update(EmployeeDetails e);
Boolean Delete(int id);
}
I want implement IHRMISBaseConnector in this class DDWEDocumentOperations
How can i implement ? Please let me know if anybody knows it
Something like this should help you:
class FakeImplementationOfEmployeeConnector : IHRMISEmployeeConnector
{
public string Add(EmployeeDetails e)
{
//...
}
public bool Update(EmployeeDetails e)
{
//...
}
public bool Delete(int id)
{
//...
}
}
class DDWEDocumentOperations : IHRMISBaseConnector
{
IHRMISEmployeeConnector employeeConnector = new FakeImplementationOfEmployeeConnector();
public IHRMISEmployeeConnector EmployeeConnector
{
get
{
return employeeConnector;
}
set
{
employeeConnector = value;
}
}
}
Here is an example:
public class DDWEDocumentOperations : IHRMISBaseConnector
{
private IHRMISEmployeeConnector _employeeConnector;
public IHRMISEmployeeConnector EmployeeConnector
{
get { return _employeeConnector; }
set { _employeeConnector = value; }
}
}

Bind Menu To a List in ASP.NET

How to bind a list to ASP.NET Menu control?
try something like this .
This is just example how you bind data to the menu control using asp.net.. you can bind list also same way like this....
Start with a IHierarcyData class that will store each string from the StringCollection...
public class MyMenuItem : IHierarchyData
{
public MyMenuItem(string s)
{
Item = s;
}
public override string ToString()
{
return Item.ToString();
}
#region IHierarchyData Members
public IHierarchicalEnumerable GetChildren()
{
return null;
}
public IHierarchyData GetParent()
{
return null;
}
public bool HasChildren
{
get { return false; }
}
public object Item
{
get; set;
}
public string Path
{
get { return string.Empty; }
}
public string Type
{
get { return string.Empty; }
}
#endregion
}
Build a class that will be the collection...
public class MyMenu : StringCollection, IHierarchicalEnumerable
{
List<IHierarchyData> _list = new List<IHierarchyData>();
public void Add(StringCollection strings)
{
foreach (string s in strings)
{
MyMenuItem i = new MyMenuItem(s);
_list.Add(i);
}
}
#region IHierarchicalEnumerable Members
public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return enumeratedItem as IHierarchyData;
}
#endregion
#region IEnumerable Members
public System.Collections.IEnumerator GetEnumerator()
{
return _list.GetEnumerator();
}
#endregion
}
In the page you can now construct the menu...
MyMenu pos = new MyMenu();
StringCollection sc = new StringCollection();
sc.Add("First");
sc.Add("Second");
pos.Add(sc);
Menu1.DataSource = pos;
Menu1.DataBind();

Turning objects DataSource to a List<>

Attempting to take the data in my gridview and convert it back into a list so I can run an update function. Currently I'm getting a null value when I attempt this.
List<CodeEntity> codes = new List<CodeEntity>();
codes = (List<CodeEntity>)Convert.ChangeType(gdvFGCode.DataSource,typeof(List<CodeEntity>));
Response.Write(codes[0].FGCode);
Also, Since the Gridview contains 3 template fields all with controls inside, would the datasource returned contain the updated values from the controls or would it return the original List<> pasted into it.
EDIT: The class I'm attempting to return in the list
public class CodeEntity
{
private string _fgCode;
private Int16 _palletSet;
private bool _priority;
private int _codeState;
private string _formingCode;
private string _d4Code;
public string FGCode
{
get { return _fgCode; }
set { _fgCode = value; }
}
public int CodeState
{
get { return _codeState; }
set { _codeState = value; }
}
public Int16 PalletSet
{
get { return _palletSet; }
set { _palletSet = value; }
}
public bool Priority
{
get { return _priority; }
set { _priority = value; }
}
public string FormingCode
{
get { return _formingCode; }
set { _formingCode = value; }
}
public string D4Code
{
get { return _d4Code; }
set { _d4Code = value; }
}
}
I would think the following should work:
if (gdvFGCode.DataSource != null)
{
List<CodeEntity> codes = (List<CodeEntity>)gdvFGCode.DataSource;
}
Edit
Added null check on the Datasource and updated code to accurately reflect OP's code.

help with image class implementation

i build that class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
namespace CDN
{
public class Image
{
#region Image parameters
private int _PG_ID;
private string _PG_FileName;
private int _PG_ItemId;
private int _TD_ID;
private Boolean _PG_Visible;
private string _PG_Caption;
public int PG_ID
{
get { return _PG_ID; }
set { _PG_ID = value; }
}
public string PG_FileName
{
get { return _PG_FileName; }
set { _PG_FileName = value; }
}
public int PG_ItemId
{
get { return _PG_ItemId; }
set { _PG_ItemId = value; }
}
public int TD_ID
{
get { return _TD_ID; }
set { _TD_ID = value; }
}
public Boolean PG_Visible
{
get { return _PG_Visible; }
set { _PG_Visible = value; }
}
public string PG_Caption
{
get { return _PG_Caption; }
set { _PG_Caption = value; }
}
#endregion
public Image(int PG_ID)
{
GetImage(PG_ID);
}
public Image()
{
}
private void GetImage(int PG_ID)
{
//TODO get image data from db and fill all parameters with the data
}
public int SaveImage(FileUpload fileUpload,string caption,Boolean visible,int toolId,int itemId)
{
try
{
SaveImageToTemporaryFolder(fileUpload);
return CreateImageOnDb(fileUpload.FileName, caption, visible, toolId, itemId);
}
catch (Exception)
{
throw;
}
}
private int CreateImageOnDb(string fileName, string caption, bool visible, int toolId, int itemId)
{
//TODO save image data on db return image id
return 0;
}
private void SaveImageToTemporaryFolder(FileUpload fileUpload)
{
try
{
string savePath = HttpContext.Current.Server.MapPath("~") + "\\upload\\pgallery\\";
fileUpload.SaveAs(savePath + fileUpload.FileName);
}
catch (Exception)
{
throw;
}
}
}
}
How to pass the file?
EDITED:
in the function SaveImage i pass a FileUpload control.
I want to use the class like this:
when uploading image to craete a new Image object and to pass him all data, but how can i transfer him the file.
Image img = new Image();
img.PG_Caption = "my file name";
img.PG_Visible = True;
img.SaveImage(file);
You can alter your SaveImage function to take a Stream and pass in like this:
using (MemoryStream stream = new MemoryStream(fileUpload.FileBytes))
{
img.SaveImage(stream);
}

Resources