How do I edit a Telerik RadGrid when using multiple tables as a data source - linq

I have asked this question on Telerik forums with no reply so I'm turing to you.
I have seen this topic come up before but without an answer so I thought I'd define the problem as simply as possible.
I have a LINQ command creating a query:
var gridData = from au in dc.aspnet_Users
join aur in dc.aspnet_UsersInRoles
on au.UserId equals aur.UserId
join ar in dc.aspnet_Roles
on aur.RoleId equals ar.RoleId
join am in dc.aspnet_Memberships
on au.UserId equals am.UserId
select new
{
au.UserName,
ar.RoleName,
am.Email,
am.IsApproved,
am.IsLockedOut
};
THE IMPORTANT POINT IS THAT THIS USES MORE THAN 1 TABLE.
Note: This is accessing the Roles and Users tables using the standard ASP.NET Membership framework.
Then I have a simple RadGrid:
<MasterTableView EditMode="InPlace">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
</MasterTableView>
Finally I bind the data to the grid in Page_Load
rgUsers.AutoGenerateColumns = true;
rgUsers.DataSource = gridData.ToList();
rgUsers.DataBind();
rgUsers.EditIndexes.Add(1);
rgUsers.DataBind();
GridView1.AutoGenerateColumns = true;
GridView1.DataSource = gridData.ToList();
GridView1.DataBind();
GridView1.EditIndex=1;
GridView1.DataBind();
This will display the data from the 4 tables correctly.
I included a GridView to compare functionality.
I also put the grid into Edit mode programatically to make sure that this was not just a problem with the Edit button.
However, when in Edit mode the RadGrid fields are not editable.
I have tried the following:
1) 1) I can use a LinqDataSource that allows Edit/Update BUT only if there is only 1 table in the LINQ query.
2) 2) I can use a ObjectDataSource onto a dataset but once again this only works if the the dataset adaptor is accessing a single table.
I have looked at the variouse suggested demos and example code but they have all ignored the multiple table/Join problem:
www.telerik.com/community/code-library/aspnet-ajax/grid/automatic-operations-with-linqdatasource.aspx
demos.telerik.com/aspnet-ajax/grid/examples/dataediting/threelevel/defaultcs.aspx
www.telerik.com/community/code-library/aspnet-ajax/grid/automatic-operations-with-linqdatasource.aspx
I am not wedded to LINQ. I am quite happy to adapt in order to get a RadGrid that can support Update and Edit to multiple tables.
What should I do to achieve a RadGrid that displays records composed from multiple tables with Update and Edit available?
Thanks for any help
Richard

Have you considered using ADO.net's dataview class in conjunction with a dataset class? You should be able to pull you data using the system.data.sqlclient class with a stored proc if that is the type of database you're using. You can preserve your database's relationships in the dataset class and the dataview class will allow you to view the data as if it were a single table. Then you should be able to bind the dataview class to the Telerik control.

Why not using NeedDataSource binding as opposed to DataBind() calls? Thus the Telerik grid should manage its editing states without extra coding. If you use joins for different source tables and set you update or insert commands using appropriate T-SQL statements, everything should work fine. Check out this demo for a start:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx
Dick

Turns out that the LINQ query returns an anonymous type that is read only.
I replaced it with a SQLDataSource using stored procedures so it works now.
Thanks

Related

How can I create a insert/update form for multiple table in Oracle Apex 5.1.4

I have craeted two tables. I have also created a page and shown all data as classic report by inner joining between two tables. Now I want to know how can I create a form to update both table at a time?
For single table it can be done easily by creating form and report page. But In case of multiple table what will be way?
Thanks
Create new page by the new page creation wizard using the Form > Single Page Master Detail template.
It will guide you.
Here's one option: create a view, then create instead-of trigger(s) upon that view. Use the same view as a data source on the Apex page.
I know for sure that it works in Oracle Forms; never tried it in Apex, though, but I believe that it should be just fine.

DataGridview with linq-to-sql

I have a datagridview which is used for listing all the students information .I am using linq to sql as a datasource.My problem arises when I edit a row or add a new row directly in a gridview.I mean there are no any forms for editing or adding student information.User should type on the particular columns to edit or to add a new row.
I found the soultions which uses SqlDataAdapter .
TableAdapter.Update(e.Row); which updates the database.
I wonder how can I achieve the same functionality using linq to sql.
Call the SubmitChanges() method of the DataContext, which you used to fill the DataGridView.
MSDN reference:
http://msdn.microsoft.com/en-us/library/bb292162.aspx

XtraReport and xrTable shows only 1 record

I have a linq to sql generated object MyObject and this contains an EntitySet.
Now i use an XtraReport with a BindingSource.DataSource = MyObject. I have a xrTable in the Detail band (which is the only one i use btw) with four xrTableCells binded binded to properties from MyObject.MyotherObject.Property. It keeps showing me only the first record and not all of them.
Anyone with an idea?
I believe you'll need to implement the ITypedList with your custom object to use it as a datasource. Check out this online documentation article for more info:
http://www.devexpress.com/Help/?document=XtraReports/CustomDocument4046.htm
I have just solved a similar problem. Did you use typed datasets to help you layout the report at design time? If you did then clear the Data Source in design view. If you apply LINQ to SQL Data Source at runtime without clearing the typed dataset Data Source in the design view, you may get only one row showing in the detail band.
i have similar problem and tried above answers, but it was so simple
just set your report DataMember to the name of your sub collection MyObject.MyotherObject
and the table will load all items in your sub collection.
XtraReport report = new XtraReport(){DataSource = MyObject,DataMember="MyotherObject" };

LINQ and Devexpress Grid Datasource

I have a DevExpress grid (DevExpress.XtraGrid.GridControl 8.2) with a datasource set at runtime like so:
private DataContext db = new DataContext("connection string");
gridControl.DataSource = from t in db.sometable
select new
{
Field1 = t.Name,
Field2 = t.Email,
Field3 = t.City
};
This means that the view has no idea what the data is going to look like at design time. I like being able to set a LINQ query as the datasource, but I'd also like to specify what the view will look like at design time.
Is there a way that I can tell the view that it will be using this query?
Would the best solution be to create a small object for holding the
contents of what gets returned from
this query?
You will have to define a class for the return type of your LINQ query if you want the DevExpress grid to automatically pick up the columns for the data source. At design time, the WinForm binding engine is using reflection or ICustomTypeDescriptor if the source implements it to automatically discover the properties, their types, etc of the data source. The DevExpress grid is using this underlying binding mechanism and automatically generating the columns for you at design time based on the property information. In your case however, you're creating an anonymous type in your LINQ query which is not known or available at design time. Therefore, DevExress Grid cannot generate the columns automatically. As #Dennis mentioned, you can manually add columns to the grid in designer. You need to make sure that 'FieldName', I believe, on the column matches the property name on your data source.
If you go with a class, you may also want to implement INotifyPropertyChanged to make the grid aware of data changes in the data source.
IIRC, the xtragrid requires that the datasource implement a databinding interface (ie IBindingList(T)) for it to auto-generate columns and the items should implement INotifyPropertyChanged.
With that in mind: if you do create columns via the wizard at design time or in code at runtime, as long as you set the FieldName property of the columns, they will show the data from the datasource with a property of that name.
Notes:
I think it must be a property, auto or not, as I've found that it sometimes won't bind to public variables.
The property must be assigned something (default or otherwise).
There must be a parameterless constructor for the item.
The fields are known at design time (Field1, Field2, Field3).
According to DevExpress you can use IList, IListSource, ITypedList or IBindingList. The difference between them is whether you can add new rows or if changes are refin the control.
So you can use ToList():
private DataContext db = new DataContext("connection string");
gridControl.DataSource = (from t in db.sometable
select new
{
Field1 = t.Name,
Field2 = t.Email,
Field3 = t.City
}).ToList();
Note: I tested it using DevExpress 10.1, but if it does use the WinForms binding then it should still work according to MSDN.
I haven't worked with the DevExpress grid, but I've done a lot with the .NET DataGridView.
Does the DevExpress grid have the same functionality as the .NET DataGridView that auto generates columns?
If so, then it should display whatever fields are found in your query and will use Field1, Field2 and Field3 (from your example code) as column names.
Or just turn off the auto generate column feature and add the columns at design time. As long as they match what your query returns it should work fine.

How to programmatically hide a linq-to-sql column on a datagridview?

I'm binding a linq Query to a datagridview for the purposes of allowing a user to insert/update/delete. However I'd like to hide the Id column. but I'd rather not hard code the
datagridview.columns[id].visible=false
In the event the id column name changes. I've read that you can walk through reflection to get a run-time list of columns, but I can't have that checked at design-time for a column name. Is it possible?
If I change the query to not include the ID, then I'm hard coding the column list instead of the id column name, so if columns were added, I'd have to go manually update there instead.
If you're using Winforms, then you should have a BindingSource on your Form that is typed to your LINQ-to-SQL type. If you do this, then you'll get full designer support for all of the fields available on that class.
If you're doing a lot of data binding work with LINQ-to-SQL entities (or any other classes, for that matter), I highly recommend checking into HyperDescriptor by Mark Gravell. It speeds up the UI data binding performance considerably by bypassing repeated runtime invocations of the properties on the classes.
Couldn't you do it in the html? Or are you using ASP.NET?
<asp:BoundField DataField="id" Visible="false" />

Resources