c# generics. Trying to set columns generically for a grid control - c#

I need some help on how to set the list of columns for a a grid control generically. I have a 3rd party library with the following method signature I want to use:
public GridControl SetColumns<T>(Action<GridColumnModelList<T>> initCols)
In my Controller on the action method I get a generic model the contains the list of columns I want displaying:
var gridprofile = new GridProfile<SiteVisitSearchGridViewModel>(gridProfileid);
I now want to create the grid control and apply my columns to it but dont know how to do this:
GridControl gc = new GridControl();
gc.SetColumns<SiteVisitSearchGridViewModel>(gridprofile.Columns);
help please
thanks
Andy

you could try something(it may not compile - I don't have that library installed) like this:
gc.SetColumns<SiteVisitSearchGridViewModel>(columns =>
{
foreach(col in gridprofile.Columns)
columns.Add(x => new GridColumnModel(col.Name));
});

Related

Grid data setup control

I am struggling with the grid view control and it seems I miss something as I am
not able to show data in it. I am trying the bind the control to a list of some
objects.
Can you please walk me through and explain how I need to setup the control?
Assuming a lot here since I don't have any code to go off of....
Given a List<GridDataModel> you would set your DataSource property to it and call DataBind method of your GridView.
var myData = GetMyData(); //this function builds the List<GridDataModel>
MyGrid.DataSource = myData;
MyGrid.DataBind();
A more complete example.

How do I persist/apply sort descriptions to a .Net C# ListViewCollection?

I have a ListViewCollection defined in my application.
The ListViewCollection is displayed in a DataGrid in a WPF desktop application.
I'd like to save and reapply the sort descriptions.
Here is a code snippet:
var sortDescriptions = ItemsView.SortDescriptions;
// Do some stuff
ItemsView = new ListCollectionView(Items)
// Reapply the previous column sorts
ItemsView.SortDescriptions.AddRange(sortDescriptions);
ItemsView.Refresh();
The above code does not seem to work.
What is the best way to achieve this?
Thanks, John

Populating a programatically declared datagridview using datasource and entity framework

just having some small issues with c# winforms datagridview. Here's my scenario
I am using entity framework and am trying to bind certain entity models to my datagridview datasource.
var query = from q in context.foo
select q;
dgv_Disp.DataSource = query.ToList();
When I ran this piece of code above on a form class which had a datagridview in the GUI, it all worked fine. The datagridview will automatically generate columns and the number of rows.
But when I run this exact same code with the exception that I don't have a datagridview in the GUI, I just declare it programmatically and then set the datasource like the above code. And when I do it this way, no rows or columns are generated.
What is the difference between these two different datagridviews? I know that there are properties set in the designer.cs file of the form class. But I tried copying these settings and it still won't populate.
I know it's probably something simple but I just can't figure this out at all. If someone could show me what im doing wrong, that'd be great!
Edit
I have used AutoGenerateColumns = true but it didn't make any difference. Also I'm not actually trying to display this datagridview, I was just binding it to entity objects that way I could access its members using a string index. But I dont want to query the database just to get my info in a datagridview specific format because in my actual scenario, I already have my entity data from a previous query. I was just using the above code as an example.
Your code should be work. below is sample code to bind List<T> to DataGridView , but you need to add the DataGridView to the form or some other panel ( or container)
public Form1()
{
InitializeComponent();
DataGridView gv = new DataGridView();
gv.DataSource = new List<string>() { "sss", "aaa" }.Select(x => new { Name = x }).ToList();
this.Controls.Add(gv); // add gridview to current form or panel ( or container), then only it will display
}
At first there is no grid in my design. Iam adding grid also in dynamically
AmolEntities db=new AmolEntities();
DataGrid dataGridView1 = new DataGrid();
this.Controls.Add(dataGridView1);
var v= from n in db.oe_subjects select n;
dataGridView1.DataSource = v.ToList();
When creating object of datagridview you need to set
dataGridView1.AutoGenerateColumns = true
Please make sure you have done this before assigning datasource to it.
Winforms datagridview only generates columns if its actually part of the GUI. By sticking all of my datagridviews on the form in the form designer, the datagridviews generated the columns as I needed. I didn't actually need the form so I just didn't call the show() method. Seems like a bit of a hackish way of dealing with the issue but that's how I fixed this
I have done easily First declared a new variable of the same DataTable and then I put tableAdapter.Fill(dt); and then dbGrid.DataSource = dt;
And it worked fine

How to make VariableSized GridView Item?

I would like to do a VariableSizedWrapGrid for the item within the wrapgrid.
Something like this.
The group title above store all the child item show on photo.
After scroll to right hand, another group title with the child shown as below.
Anyone have any idea how to do so?
I was able to display group title with child item as below. The only things i unable to achieve is the variable size of the child item.
One way you can go is to use the GridView's built-in selector properties.
See my blog entry.
In a nutshell, you can create a custom StyleSelector. All you have to do is override thee StyleSelectorCore() method and put in your logic to choose a style that defines the column or row spans.
You'll need to get the default GridViewItem style template through Blend or an online resource and create a default explicit style. Then create new styles BasedOn the explicit one like so:
<Style x:Key="DoubleHeightGridViewItemStyle"
BasedOn="{StaticResource DefaultGridViewItemStyle}"
TargetType="GridViewItem">
<Setter Property="VariableSizedWrapGrid.RowSpan"
Value="2" />
</Style>
For this to work, you'll also need to change the GridView's ItemsPanel template to use a VariableSizedWrapGrid.
Finally, by creating a custom DataTemplateSelector, you'll be able to change the DataTemlates of your bound items. You'll need to do this unless your over-sized items can use the same DataTemplate as the default sized one.
Updating this because some other helpful things have emerged since the question was asked. My colleague Jerry Nixon has a nice post describing how to create variable-sized items in a GridView:
Windows 8 Beauty Tip: Using a VariableSizedWrapGrid in a GridView makes Grids Prettier
Short version, you can make a custom GridView that implements PrepareContainerForItemOverride.
There's also more details in an earlier example (from May) that Mark Rideout posted:
How To: Create a Variable Sized Grouped GridView (like the store)
Here is a solution for doing it in C#: http://leeontech.wordpress.com/2012/03/01/customizing-gridview-items-in-metro-app-4/
I am not sure about C# and XMAL code of doing this. But in Javascript, instead of creating the template in HTML, you can create the item template in javascript by doing something like this
function MyItemTemplate(itemPromise) {
return itemPromise.then(function (currentItem) {
var result = document.createElement("div");
//use source data to decide what size to make the
//ListView item and apply different css class to it
result.className = currentItem.data.type;
result.style.overflow = "hidden";
// Display image
var image = document.createElement("img");
image.className = "regularListIconTextItem-Image";
image.src = currentItem.data.picture;
result.appendChild(image);
var body = document.createElement("div");
body.className = "regularListIconTextItem-Detail";
body.style.overflow = "hidden";
// Display title
var title = document.createElement("h4");
title.innerText = currentItem.data.title;
body.appendChild(title);
// Display text
var fulltext = document.createElement("h6");
fulltext.innerText = currentItem.data.text;
body.appendChild(fulltext);
result.appendChild(body);
return result;
});
}
Source of this code is in the ListView Item Templates sample of the consumer preview sample package. Unfortunately, I was not able to find the C# version of it.
Hope this helps to some degree.

How do you create a UserControl which has controls mapped to Database DataRow columns. C# Winforms

I have a C# winforms database application.
I want to create a custom user control which will go on say an add new customer page. I have my database app all wired up. I have DataTables, and TableAdapters, and other stuff... but I don't see a way to create a custom UI which will bind the windows form controls to an object which I could then use to add to the database. I expected there to be some way that i could drag some table from my dataset onto my form which would add things like textbox's for each column. I know something like this must exists but I just don't know what to call it... things like DataBinding giving me a bunch of examples which show how you could update put like a phone number in a text box when the Custome row in a DataTable is selected.
I want to avoid doing:
public void button1_OnClick() {
MyDatabase.OrganizationRow r = new MyDatabase.OrganizationRow();
r.ID = textbox1.Text;
r.Name = textbox2.Text;
r.ShortName = textbox3.Text;
this.myDatabase.Organization.Rows.Add(o1);
this.myDatabase.AcceptChanges();
}
Thanks in advance.
I'm using .NET 2.0 and VS 2005.
You can databind the controls to a BindingSource, then call the BindingSource's AddNew method before the user starts entering data and EndEdit when the user clicks save (or add or whatever)

Categories