How do you get the proper mapping name from a binding source bound to a List<T>, or an anonymous type, to use on a DataGridTableStyle?

c# datagridview bind object collection
uwp data binding example
datagridview bind to list of objects
how to bind datagridview in c# with datatable
c# bind object to datagridview
c# datagridview datasource list<string
uwp binding in code
data binding c#

I'm trying to create a DataGridTableStyle object so that I can control the column widths of a DataGrid. I've created a BindingSource object bound to a List. Actually it's bound to an anonymous type list created though Linq in the following manner (variable names changed for clarity of what I'm doing):

List<myType> myList = new List<myType>(someCapacity);
.
...populate the list with query from database...
.

var query = from i in myList
            select new
            {
                i.FieldA,
                i.FieldB,
                i.FieldC
            };

myBindingSource.DataSource = query;
myDataGrid.DataSource = myBindingSource;

Then I create a DataGridTableStyle object and add it to the datagrid. However, it never applies my table style properties I set up because I can't seem set the proper myDataGridTableStyle.MappingName property.

I've searched Google for about 1/2 an hour and keep seeing links to the same question throughout a bunch of different forums (literally the same text, like someone just copied and pasted the question... I hate that...). Anyway, none of the suggestions work, just like the guy says on all the other sites.

So does anybody here know what I need to set the MappingName property to in order to have my TableStyle actually work properly? Where can I grab the name from? (It can't be blank... that only works with a BindingSource that is bound to a DataTable or SqlCeResultSet etc.).

I'm thinking it could be an issue with me using Linq to create an anonymous, more specialized version of the objects with only the fields I need. Should I just try to bind the BindingSource directly to the List object? Or maybe even bind the DataGrid directly to the List object and skip the binding source altogether.

Thanks

PS - C#, Compact Framework v3.5

UPDATE:

I've posted an answer below that solved my problem. Whether or not it's the best approach, it did work. Worth a peek if you're having the same issue I had.

I've found the way to make this work. I'll break it out into sections...


List<myType> myList = new List<myType>(someCapacity);
.
...populate the list with query from database...
.

DataGridTableStyle myDataGridTableStyle = new DatGridtTableStyle();
DataGridTextBoxColumn colA = new DataGridTextBoxColumn();
DataGridTextBoxColumn colB = new DataGridTextBoxColumn();
DataGridTextBoxColumn colC = new DataGridTextBoxColumn();

colA.MappingName = "FieldA";
colA.HeaderText = "Field A";
colA.Width = 50; // or whatever;

colB.MappingName = "FieldB";
.
... etc. (lather, rinse, repeat for each column I want)
.

myDataGridTableStyle.GridColumnStyles.Add(colA);
myDataGridTableStyle.GridColumnStyles.Add(colB);
myDataGridTableStyle.GridColumnStyles.Add(colC);

var query = from i in myList
            select new
            {
                i.FieldA,
                i.FieldB,
                i.FieldC
            };

myBindingSource.DataSource = query.ToList(); // Thanks Marc Gravell

// wasn't sure what else to pass in here, but null worked.
myDataGridTableStyle.MappingName = myBindingSource.GetListName(null); 

myDataGrid.TableStyles.Clear(); // Recommended on MSDN in the code examples.
myDataGrid.TablesStyles.Add(myDataGridTableStyle);
myDataGrid.DataSource = myBindingSource;

So basically, the DataGridTableStyle.MappingName needs to know what type of object it is mapping to. Since my object is an anonymous type (created with Linq), I don't know what it is until runtime. After I bind the list of the anonymous type to the binding source, I can use BindingSource.GetListName(null) to get the string representation of the anonymous type.

One thing to note. If I just bound the myList (which is type "myType") directly to the binding source, I could have just used the string "myType" as the value for DataGridTableStyle.MappingName.

Hopefully this is useful to other people!

Bind Objects to DataGridView Controls, I'm trying to create a DataGridTableStyle object so that I can control the column widths of a DataGrid. I've created a BindingSource object bound to a List. If you are binding several properties to a common source, you want to use the DataContext property, which provides a convenient way to establish a scope within which all data-bound properties inherit a common source. In the following example, the data context is established on the root element of the application.

Just to add to the collection of answers already on this page....

I was just frustrated with this same issue trying to develop my fist application using windows forms and compact framework (For Windows Mobile 6.5).

What I found out, through Marc Gravell's comment above is that indeed is possible to get the run time MappingName inspecting the properties of the DataGrid. Doing this I found out that when binding my List<MyType> directly to the DataSource property of the DataGrid, the DataGrid was actually looking for a DataGridTableStyle with the MappingName of

"List`1"

instead of any combination of List<MyType> or MyType...

So... by putting "List`1" in the Mapping name on the DataGridTableStyle Collection Editor (at design time), I was able to customize the columns and other properties without having to create them all at run time.

I just hope this adds some more to the answers already provided. Thank you all for providing me with the guidelines.

Data binding in depth, I've created a BindingSource object bound to a List. Actually it's bound to an anonymous type list created though Linq in the following manner (variable names  Binding Sources Overview. 03/30/2017; 7 minutes to read +11; In this article. In data binding, the binding source object refers to the object you obtain data from. This topic discusses the types of objects you can use as the binding source. Binding Source Types. Windows Presentation Foundation (WPF) data binding supports the following binding

The query returns IEnumerable<T> for some T, but most binding sources (except ASP.NET) require IList (such as any IList<T> implementation) - try adding .ToList() - i.e.

myBindingSource.DataSource = query.ToList();

A BindingList<T> might work even better (if it is supported in CF 3.5) since it has better support for some of the common binding scenarios; if you need this (and assuming BindingList<T> exists on CF 3.5), you can add an extension method:

static BindingList<T> ToBindingList<T>(this IEnumerable<T> data)
{
    return new BindingList<T>(new List<T>(data));
}

then call:

myBindingSource.DataSource = query.ToBindingList();

For completeness, an alternative to an IList is IListSource (or even Type for purely-metadata scenarios), which is why DataSource is commonly typed as object; if it wasn't for this issue, the compiler probably would have been able to tell you the problem (i.e. if DataSource was defined as IList).

A Detailed Data Binding Tutorial, How to: Bind Objects to Windows Forms DataGridView Controls so that the combo box drop-down list contains the enumeration values. new DataGridView​(); private BindingSource bindingSource1 = new BindingSource(); private bool good; private Title hisTitle; public Knight(Title title, string name,  First, it simplifies binding controls on a form to data by providing currency management, change notification, and other services between Windows Forms controls and data sources. This is accomplished by attaching the BindingSource component to your data source using the DataSource property.

I followed this answer and found that the MappingName always came out to be the underlying class name (myType in the example).

So it seems that putting the collection it through the BindingSource solves the problem anyway and that there is then no need for BindingSource.GetListName(null).

Also I found no need to ToList() the query as the BindingSource will also do this for you.

Many thanks to Jason Down for putting me on the right track.

Data Binding in .NET / C# Windows Forms, FrameworkElements are good binding sources right out of the box. You can bind list controls to arbitrarily large data sources, and still achieve These files can be found in your obj folder, with names like (for C#) <view name>.g.cs . The converter maps true to the Visible enumeration value and false to  How do these map to our XAML binding? Let's look at the various components of our markup: The target object is the object that owns the property which we are binding to, i.e. the UI control rendering our data. The target property is the property that has been set via the markup extension, and the source property is the path of the binding.

I was facing same problem for setting column width. After lot of R & D, i changed code as below and its working fine. Code:

DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = dgCustom.DataSource.GetType().Name;

where dgCustom is DataGrid ID in dgCustom.DataSource.GetType().Name which is working perfectly.

Programmatically Binding DataSource To ComboBox In Multiple Ways, DataGrid and DataGridView bind to the List through their DataSource property. Then, in the designer, put a DataGridView (named " grid ") and a in the top-​right corner that you can click to find a configuration box. This ReflectPropertyDescriptor contains a mapping from our Airplane to BindingSource. The BindingSource component is designed to simplify the process of binding controls to an underlying data source. The BindingSource component acts as both a conduit and a data source for other controls to bind to.

Radgrid Bind To List Of Objects, For example if you add a TextBox control to a form and bind it to a column of a table (e.g. "Customers. to the fifth name, because it is zero-based), both controls display the appropriate To act as a data source, a list must implement the IList interface; one example Shows Master-Detail, Table-Mapping, Fill a Combobox Step 3: Apply! If you've taken in the previous steps properly, you should have a fair idea of how to bind your spells. It may take some time to get it all done, and a tad longer to master, but after you've done this I guarantee you that you've taken your skills to a new level.

“The” Athenaeum: Journal of Literature, Science, the Fine Arts, , A The data source is bound to a ComboBox using the "DataSource" with the person's name and when the user selects a name on the list,  As you can see, both sites are using the same IP address and port. The host name is the one thing that differentiates the two sites. If we tried to add a binding for foobarinc.com to the Acme site, we will get a warning telling us that the same binding already exists.

Do I need to bind the RadGrid and other databound controls to something else than an Then we set the DropDownList's DataSource property value to the newly The referenced class must have object-directory mapping metadata specified surname, adress) in a Grid, is it possible to directly bind the list of objects with  Update The Following Properties Only and select the options you want. Don’t Update Any Related Properties. Click OK. Whether you update the object’s properties or not, the form object is now bound to the data source. Select the same node in step 1 and repeat steps 2 and 3 for the other fields that you want to bind to the same node.

Comments
  • You usually see duplicate questions because many sites are aggregating the same newsgroups so in some cases the question was only posted once yet re-displayed in the web multiple times in different locations.
  • True enough... just frustrating sometimes because the same question takes up the first 10 pages on Google. It'd be nice if they could be filtered as being the same.
  • Nice to see someone who posts the complete follow-up answer for a change!
  • I had the same issue, and this solved my problem! +1 and thank you!
  • Glad I could help. I know it was frustrating trying to get this one working, so hopefully this saved you a few headaches =)
  • Exactly what I needed. Thank you.
  • +1 - An alternative is to use tableStyle.MappingName = yourList.GetType().Name; I saw it here
  • Thanks Mark. I'll try the query.ToList() approach first. I'm still wondering what I actually need to set my TableStyle.MappingName to though. How do I get the name from the BindingSource object (e.g. bs.List.GetType().Name or something like that)? Thanks again
  • What do you mean by the "name" of the BindingSource? Can you clarify?
  • Some searching suggests that you can check the dataGrid.m_tabstyActive field in the Quick Watch window; this should give you the name you need to use to avoid getting a new style.
  • The DataTableGridStyle needs the MappingName set to whatever datasource you are using. I've tried the name of the BindingSource, but it doesn't seem to work, so my DataTableGridStyle doesn't actually apply to the DataGrid. Now I'm trying to get the name of the underlying list in the bs.
  • Ya I did see that link. I actually wanted to create my own DataGridTableStyle object though and have that apply, rather than using the one that is used by default. I guess I can try changing the default style object's properties if that's possible.