I want to get the name I have in the DataGrid In Windows From
Code :
var PersonName = DataGridView.CurrentRow.Cells [1] .value.ToString ();
But in wpf, this method is not for DataGrid
That's not the WPF way. You need to work with the underlying collection that is the DataSource of your DataGrid and use the SelectedItem property.
You need to derive the value from the class that PersonName came from. For example, assume you have a Person class like this.
public class Person
{
public string PersonName { get; set; }
public string Address { get; set; }
public string Age { get; set; }
}
To get the PersonName from the current selected row, you could do something like this:
if (myDataGrid.SelectedItem != null)
{
var PersonName = ((Person)myDataGrid.SelectedItem).PersonName;
// ... now do something with the PersonName
}
Related
Good day!
I'm having an issue with the DevExpress LookUpEdit I can't figure out what the problem is.
I'm use Entity Framework list as a datasource.
public partial class provider_scheme : BaseEntity
{
public provider_scheme()
{
}
public int Provider_Scheme_RowID { get; set; }
public int Currency_RowID { get; set; }
public string Provider_Scheme_Name { get; set; }
public virtual currency currency { get; set; }
}
public partial class currency : BaseEntity
{
public currency()
{
provider_scheme = new HashSet<provider_scheme>();
}
public int Currency_RowID { get; set; }
public string Currency_ISOCode { get; set; }
public virtual ICollection<provider_scheme> provider_scheme { get; set; }
}
I'm setting the Datasource property of the LookUpEdit to IEnumerable<provider_scheme>, and setting up two column field names in my LookUpEdit. One for 'Provider_Scheme_Name' and one for 'currency.Currency_ISOCode'. But for some reason only the 'Provider_Scheme_Name' column values are showing. I've also checked and the 'currency' navigation property is being loaded.
Thanks in advance for your help
A bit late for an answer, but you might consider using the GridLookupEdit control instead. It permits adding all the columns you want
I'm trying to simply display the list of members in a specific group using the Facebook Graph API. I'm using Newtonsoft.JSON.
Here is the results of my url query:
Graph API Results
I used a JSON class generator and it gave me this:
public class Datum
{
public string name { get; set; }
public string id { get; set; }
public bool administrator { get; set; }
}
public class Cursors
{
public string before { get; set; }
public string after { get; set; }
}
public class Paging
{
public Cursors cursors { get; set; }
}
public class Members
{
public List<Datum> data { get; set; }
public Paging paging { get; set; }
}
public class RootObject
{
public Members members { get; set; }
public string id { get; set; }
}
I've tried every combination I can think of to display simply the list of members in a multi-line text box, but not sure if this is even the best way to display the list on a Windows Form App.
Could someone help me understand 2 things.
1) What is the best component to display the list of names in a Windows Form App?
2) What is the 1 or 2 lines to generate just the list of names using JsonConvert.DeserializeObject from this?
My raw data is stored in: string responseFromServer = reader.ReadToEnd();
To deserialize the JSON into your classes:
RootObject obj = JsonConvert.DeserializeObject<RootObject>(responseFromServer);
To get the member names into a List<string>:
List<string> members = obj.members.data.Select(d => d.name).ToList();
Note: You need to have using System.Linq; at the top of your file in order to use the Select and ToList methods.
As far as displaying the data in a windows form app, there's not a "best" component-- it depends on what you're trying to accomplish as to what control you would choose to use. For example, if all you want to do is display the list of names in a multi-line textbox, you could do this:
textBox1.Text = string.Join("\r\n", members);
If you want to allow the user to be able to select individual names and do something based on that selection, you would probably want to use a ListBox or a ComboBox instead. You can populate a ListBox or ComboBox like this:
listBox1.DisplayMember = "name";
listBox1.DataSource = obj.members.data;
That should be enough to get you started.
I'm trying to load data to DataGrid from a generic list.
the relevant code:
XAML:
<Grid>
<DataGrid DataContext="{Binding Lines}"
ItemsSource="{Binding}"
AutoGenerateColumns="True">
</DataGrid>
</Grid>
C#:
public IList<IReportLine> Lines { get; set; }
public interface IReportLine {}
public class ReportLine : IReportLine
{
public string A { get; set; }
public string B { get; set; }
}
It seems that the columns are taken from the type IReportLine - so I'm getting an empty DataGrid.
Of course, if I'm changing IReportLine definition to:
public interface IReportLine
{
string A { get; set; }
string B { get; set; }
}
it works perfectly, but i can't do that because every class that implement IReportLine has different Properties.
What can I do in order to make the columns be generated from the dynamic type of IReportLine?
Or have any other idea to solve my problem?
Thanks!
EDIT:
The interface holding the Lines property and the class implementing the interface(one of many):
interface IReport
{
string Header { get; set; }
IList<IReportLine> Lines { get; set; }
}
public class Report : IReport
{
public string Header
{
get;
set;
}
public IList<IReportLine> Lines
{
get;
set;
}
}
The DataContext of the DataGrid is IReport object.
So I can't Change
public IList<IReportLine> Lines { get; set; }
to
public IList<ReportLine> Lines { get; set; }
Instead of defining members in interface, make the list to be more verbose. You gotta tell dataGrid at least some specific type so that it can look for properties in it.
Change
public IList<IReportLine> Lines { get; set; }
to
public IList<ReportLine> Lines { get; set; }
UPDATE
Like I mentioned above, if you want columns to be auto generated, you gotta supply some specific type.
Consider scenario where you have another class say AnotherReportLine implementing IReportLine:
public class AnotherReportLine : IReportLine
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
Now, you can add both class instances in Lines collection like this:
Lines = new List<IReportLine>();
Lines.Add(new ReportLine() { A = "A1", B = "B1" });
Lines.Add(new AnotherReportLine() { A = "A1", B = "B1", C = "C1" });
What should be the columns list now?
A | B OR A | B | C.
WPF engine cannot infer that without your help.
That brings you down to three possible ways:
Move properties to interface.
Make the list of more specific type.
Last set AutoGenerateColumns to False and provide your own list of columns you want to show.
I am using Telerik RadGridView to display dynamic data, stored in classes like bellow:
public class Field
{
public string FieldName{ get; set; }
public string FieldValue{ get; set; }
}
public class Row
{
public List<Field> Fields{ get; set; }
public Row()
{
Fields= new List<Field>();
}
}
A Row has "n" Fields.
I need to display each "Row" in a line in the grid and each Field like a column in the line.
When I set:
myGrid.ItemSource = myList;
I get something like this:
Fields
--------------------
(Collection)
(Collection)
...
When I really need is a table like this:
Name Gender Date ...
----------- --------- --------
JOHN M 10/10/2010
PETER ....
I just resolve this problems using DataSet native objects, like this example:
http://www.telerik.com/community/forums/winforms/gridview/binding-radgridview-to-datatable-and-adding-items-to-datatable.aspx
Overview
I am designing a mechanism for generating dynamic controls in an ASP.NET MVC application that uses ADO.NET Entity Framework. However, my question has nothing to do with MVC and a little to do with the Entity Framework. It is about comparing two object models.
Problem Statement
In my app, a user must have the ability to interact with Web page A to specify that he wants to add such and such HTML controls to Web Page B.
When he browses Web Page B next, he must see those controls and be able to use them.
What Is Not The Challenge
I have written the code to generate the controls. That was the easy part. I used the Tag Builder, Partial Views, HtmlHelper extensions and Display & Editor templates.
The Challenge
The challenge is in arriving at a database design and an object model generated by Entity Framework to hold the metadata about the controls that need to be generated.
I have come up with a database design as shown below:
You may ignore the User and Permissions tables. They are not relevant to our discussion.
Entity Framework generates the following entities based on the above database design.
Let's call my database design as Design Option A.
I would have wanted a design that looked more like this:
Let's call this second design as Design Option B.
The code (stripped down version) for this second option would look like this:
namespace DynamicControls
{
public class DynamicControlGroup
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Controller { get; set; }
public IEnumerable<string> Actions { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public User CreatedByUser { get; set; }
public DateTime CreationDateTime { get; set; }
public User LastModifiedBy { get; set; }
public DateTime ModificationDateTime { get; set; }
// Navigational
public ICollection<DynamicControl<T>> DynamicControls { get; set; }
}
public class DynamicControl<T>
{
public long Id { get; set; } //db Id
public string HtmlId { get; set; }
public bool ValueRequired { get; set; }
public virtual ControlType ControlType { get; protected set; }
// Every control is capable of having a default value but of a different
// type. Most controls have default values of type text (string). The
// multi-select ones (checkboxes, multi-select lists, etc.) have a default
// value of type IEnumerable<string>. So, I want to leave this generic.
// But I am not that hung-up on this. I am fine if I am required to move
// this property DefaultValue from the base class and make it a concrete
// (not generic) property for each individual child class.
// Mostly I just want the heirarchy. And before that, I want to know
// if it is a good idea to model this heirarchy. Or is it better to just
// work with what my Entity Framework produced for my db?
// Should I change my db? I can because I thought-up the design for
// those tables.
public virtual T DefaultValue { get; set; }
// Navigational
public DynamicControlGroup DynamicControlGroup { get; set; }
}
public class TextBox : DynamicControl<string>
{
public override ControlType ControlType
{
get
{
return DynamicControls.ControlType.TextBox;
}
}
public string Label { get; set; }
public int MaxLength { get; set; }
}
public class PasswordControl : TextBox
{
public override ControlType ControlType
{
get
{
return DynamicControls.ControlType.Password;
}
}
}
public class TextArea : TextBox
{
public override ControlType ControlType
{
get
{
return DynamicControls.ControlType.TextArea;
}
}
public int Rows { get; set; }
}
public class DropDownList: DynamicControl<string>
{
public override ControlType ControlType
{
get
{
return ControlType.DropDownList;
}
}
// I want something like this. That I should be able to say
//
// myDropDownListObject.Options...
//
// You'll notice that given my current database design, I have
// no direct way of accessing the options of a, say, drop down list.
// To do that, I have to make a round-about Linq query.
public ICollection<DynamicControlOption> Options { get; set; }
}
public class DynamicControlOption
{
public long Id { get; set; } // db Id
public string OptionHtmlId { get; set; }
public string OptionValue { get; set; }
public string OptionText { get; set; }
// Navigational property
public DynamicControl<IEnumerable<string>> TheControlWhoseOptionIAm { get; set; }
}
public class User
{
}
public class Permission
{
}
public enum ControlType
{
TextBox,
TextArea,
Password,
RadioButton,
Checkbox,
DropDownList,
MultiSelectList,
DatePicker,
TimePicker,
DateTimePicker
}
}
My Question
1) I feel that I'd like Design Option B better. Am I feeling right?
2) I know I can work with Design Option A just as fine but it'll involve a little round-about way to do some things. For example, to get all the options for a drop down list, there's not navigational property on the DropDownList class in Design Option A. I'll have to write a round-about Linq query to do that.
3) Is it possible to have Entity Framework come close to generating Design Option B? How? What changes will I need to make to my database design to achieve that?
Now we are working on a Project like this at our company...
If I got your meaning correctly and If I were you...I implemented inherited structure as my database design like below.
Now you Classes are inheritance but your database design is not.
I have removed Id in TextBox and I have put ControlId as PK and FK in the same time. (not just FK).
in fact,ControlId is both PK for TextBox and FK from DynamicControl
and also this way for PasswordControl and TextArea
and Now ControlId in TextBox is not Identity. It gets it's ControlId from DynamicControl
I also accept Design Option B .I'm always more comfortable than using Design Option A.in my idea It's true and main structure