I'm trying to insert properties dynamically to a class.
I need to load properties from database and show it in PropertyGrid, but I want to it to be build in run time, it means that I want to insert property definition to database and to load it to a class(My Class) in runtime.
e.g - if the data base contains row that define property (e.g Name of type string)
the class should something like this:
public MyClass
{
public string Name{get; set;}
}
I also tried to do it using dynamic but it failed.
any ideas?
Thank you.
You can use an ExpandoObject.
I would use Simple.Data to do this. It fetches data from the database and constructs dynamic objects from the query :)
Related
I'm not sure if I'm being clear in the title but I'd like to "load" information from a SQL Server database into a list of objects. I'm new to c# and honestly haven't done any coding in a while.
Essentially the table would have columns: app_name, app_type, app_disposition and the object has properties: name, type, disposition. I've got what I want working using Dapper and simply making the object properties the same as the table columns.
Just curious if you could load but using different object property names.
With Dapper, simplest solution is to use aliases.
Your class is:
public class MyPoco
{
public string Name {get;set;}
//Declare other properties here
}
And, you fill this class as below:
string sql = "SELECT app_name as Name, [include other columns here]
FROM MyTable";
using (var conn = GetOpenConnection())
{
var myPocoList = conn.Query<MyPoco>(sql);
}
GetOpenConnection method above simply returns open connection depending on your RDBMS.
Please note that there are many other ways to map the miss-matching column and property names. Please refer this Q&A for more details.
Yes, you can, and you have to use the "Custom Mapping" feature. Here's a detailed article I wrote on the subject, along with code samples, to shows how you can do it.
https://medium.com/dapper-net/custom-columns-mapping-1cd45dfd51d6
Hint: Use Dapper.Fluent-Map plugin
I am writing a program where data is being displayed using a DataGridView and I was hoping there was a way to access the visible property of the columns without specifying the index location, or Column Name string value.
public class test {
public static string value1 { get; set; }
public static string value2 { get; set; }
}
I am using the LINQ to SQL datacontext to query information to be displayed into my DataGridView.
As it currently is, I can only seem to find a way to change the Columns visible property as so (assuming DataGridView is instantiated as dgvDATA)
dgvDATA.columns["value1"].visible = false;
Is there any way to do something similar to the following? I assume if there is it would be through databindings, but I tried that and couldn't figure it out.
dgvDATA.column.value1.visible = false;
I found this article stating that the DataGridView does not have this kind of ability built in but there was a workaround where you could add a database field representing if you want it visible or not.
HOWEVER the article was written in 2011 so I find it hard to believe that something like this was never implemented.
https://dotnetbuildingblocks.wordpress.com/2011/07/30/binding-datagrid-column-visibility-to-a-datacontext/
Please let me know if this is possible! Thank you
DataGridView doesn't support what you are asking for.
Anyway, if your only goal is to avoid using hardcoded field names, then using nameof() makes sense.
In your case it will be
dgvDATA.Columns[nameof(test.value1)].Visible = false;
That way you will have no issues refactoring your code later.
Apparently you want some kind of special DataGridViewColumn, where columns can be identified using some identification that is not available in a standard DataGridViewcolumn. For example you want to identify them by the PropertyInfo of the property that is shown in the column, or maybe the database column name of the database column whose values you show in this column.
In object oriented programming, if you want to create a special kind of DataGridViewColumn you should write a derived class:
class MySpecialDataGridViewColumn : DataGridViewColumn
{
public string DatabaseColumnName {get; set;}
}
Normally this would be enough: as long as you make sure that you only add MySpecialDataGridViewColumn objects to your DataGridView. When you fetch a column, typecast it to MySpecialDataGridViewColumn.
var theColumnThatDisplaysFirstName = myDataGridView.Columns // get all columns
.Cast<MySpecidalDataGridViewColumn>() // cast to your type
.Where(column => column.DatabaseCollumnName == "FirstName")
.SingleOrDefault(); // keep the ones with "FirstName"
Be aware that others will still be able to Add other kind of columns to your DataGridView. If you are afraid of this, make sure that you keep your members private and add functionality to Add / Fetch / Remove MySpecialDataGridViewColumns.
If your DataGridView is to be used by many, consider creating a UserControl that contains a private DataGridView, with functionalities to Add / Retrieve / Remove MySpecialDataGridViewColumn objects to the user control. This way others can't misuse your `DataGridView by adding other types of columns
Of course, if you want to allow others to add their own kind of Columns, you could always use OfType<MySpecialDataGridViewColumn> instead of a Cast. This way you ignore the other type of added columns, of which you are certain that they don't display your database columns
I just developing a personal project and actually I'm using EF Code First from database and I'm facing a little problem.
The problem simply is I want to define an attribute called Download and this attribute may contain multiple download links for example :
Attribute Name : Download
=>Values :
link1.com
link2.com
link3.com
How to define this in my model an attribute that can hold many values and how can I show them in my view one by one like foreach or something to fetch the attribute values and split them
Thanks and I hope someone help me with that!
You can either use a single string with a separator, or create another table linked to this one:
class Download
{
...
public string Link {get;set;}
}
class ParentClass
{
...
public virtual ICollection<Download> Links {get;set;}
}
Then in your view, you can iterate over the list of Links.
I have the following class model:
public class Person
{
public string Name;
public int Age;
}
public class PersonService
{
public List<Person> GetAll() {...}
}
I'm displaying the data on an ASP.Net web page by binding an ObjectDataSource (ODS) to a GridView.
If I point the 'ODS.TypeName = PersonService' then it gives an "Object does not match target type." error on loading data.
If I point the 'ODS.TypeName = Person' then it can't find the GetAll() method to load data.
Is it possible to bind the ODS to this model (i.e. separate classes for the method and type)?
Edit: I've double checked Type and Select method names are correct (and fully qualified). I made a separate quick test project to prove ODS works with the above model - it does. The only difference now, is that the broken project is using an Entity defined by EF 4.1 - would that cause a problem?
TypeName needs to be set to PersonService in your example. There is no need to provide the ODS with the exact type of object bound to the grid's rows (Person in your example), as neither ASP.NET data binding nor the ODS/grid will really care. Concerning your error, try to fully qualify the service, e.g., My.Namespace.PersonService, and make sure to set SelectMethod on the ODS accordingly.
I think you want to create a PersonList class that exposes a list of Person object and which has a method uses PersonService to populate/return a list.
enter image description here
Just right click on your project and select "Disable Lightweight Solution Load"
it will work. there is no other issue
According to Microsoft:
http://msdn.microsoft.com/de-de/library/system.data.linq.mapping.columnattribute.expression.aspx
It's possible to add expression to the Linq-to-SQL Mapping.
But how to configure or add them in Visual Studio in the Designer?
Problem, when I add it manual to thex XYZ.designer.cs it on change it will be lost.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.4927
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
This is generated:
[Column(Name="id", Storage="_id", DbType="Int")]
public System.Nullable<int> id
{
...
But i need something like this
[Column(Name="id", Storage="_id", DbType="Int", Expression="Max(id)")]
public System.Nullable<int> id
{
...
Thanks.
According to this article:
http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.columnattribute.expression.aspx
you should use the ColumnAttribute.Expression Property when you use CreateDatabase to define a column as containing computed values.
So you should check this article:
http://msdn.microsoft.com/en-us/library/Bb399420%28v=VS.100%29.aspx
Another way is to define expression on your sql server so it'll be mapped by the LINQ designer.
Edit: mmmm you edited your question, so probably my answer is not gonna help you so much, but you might be able to do this anyway with your 'extended' question :D
I do this by adding another class file to the project, give them the same name as the object from LinQ-to-SQL you want to extend and define it as partial.
for example, if you have a table called Files, the object File will be created for you by L2S. If you then create a file (with the same namespace as your DataContext object), and make it partial, like this:
public partial class File
{
}
You can just add properties, methods, etc. From within this class, you also have direct access to the properties of the 'other' File class.
It's a little klugy, but in your linq2sql designer, rename the field from 'id' to 'xid' (or anything else) and change its accessibility to internal.
then, in another file, start another partial class, like Wim Haanstra showed, and create a new property called 'id', add all the attributes you want, and in the get & set, just map it to and from the original property, now called 'xid'.
it would look something like this:
public partial class File
{
public int? id
{
get { return xid; }
set { xid = value; }
}
}
this is more commonly done to map fields in the database to a different type in the object, e.g. an int in the DB to an enum in the object, a byte/smallint/etc. in the DB, a boolean in the object. or to add attributes, like [DataMember] to the property.