Solved

Gridview and Linq

Posted on 2009-05-07
10
601 Views
Last Modified: 2013-11-11
I have two gridviews and a Save Button.  One Gridview for Items and the other for a list.  The Items Gridview has a linkbutton template field and when you click on an item it should add it to the List Gridview.  The List Gridview may already have items on it from previously saving the List.  I am using VB.net and Linq to accomplish this.  My issue is:  I would like to update the List Gridview without committing the changes to the database until I click "Save".
0
Comment
Question by:drquan
  • 5
  • 3
10 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24333818
When you said List Gridview, do you mean you are using a List as a datasource to the gridview?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24335689
I believe that the statement, "One Gridview for Items and the other for a list", just means that the "List GridView" is the 2nd GridView, not a ListBox or ListView control...
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24335777
If you are using a DataTable as the datasource to the List Gridview then you can try this

dim drow as datarow = LGridView.NewRow
drow(0)=somevalue
drow(1)=somevalue
...
LGridView.Rows.Add(drow)

If its not a datatable then comeback here and provide more information.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:drquan
ID: 24337550
Thanks for the replies!!

The gridview is just a gidview of the items on a list.  I've used the DataTable solution in the past, however,  I was hoping to use an Entity provided by the Linq Datamodel to accomplish this.  
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24337667
That's what my previous question was. Is it a List<of T> ? What type of objects are in the list? Please show us the exact datatypes of the list and the types in the list.
0
 

Author Comment

by:drquan
ID: 24337787
Let me try and clarify.
I have two Gridveiws.  One for available widgets and the other for selected widgets.
The "available widgets" Gridview has LinkButton Controls.  When you Click on the Link Button, the widget gets added to the second Gridview ("selected widgets").  I would like the user to be able to add/delete widgets without committing the changes to the database.  Once a user has finished adding and deleting widgets, they then can Click on a "Save" Button and all changes will be committed to the database.  
I can accomplish this via a SQLDataSource and DataTable, however, I would like to use the Entity Framework instead.

K
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24338602
I am not sure about entity framework but as the original question is about LINQ so i would use LINQ here.
You can set a datasource to the grid like this
Grid1.DataSource = db.Widgets
where db is a DataContext class and Widgets is a Table(of Widget) variable within db. Widget is the actual class representing the Widget table.
To add a new item, you could do something like this

Dim W as new Widget
w.id = blablabla
W.Text = blablabla
db.Widgets.InsertOnSubmit(W)
Grid1.DataSource = db.Widgets

Now when you need to save changes, just call this
db.submitchanges()
0
 

Author Comment

by:drquan
ID: 24339791
That works ok for Inserts.  What if a user adds a widget and then wishes to delete the widget (submitchanges has not been called)?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24341106
In that case the alternative method is to use another list of T.
Dim WidgetList as new List(of Widget)

WidgetList.Add(W)
WidgetList.Remove(W)

Then when saving
for each W in WidgetList
      db.Widgets.InsertOnSubmit(W)
Next
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
imap mails 1 24
VB.NET 1 24
Help with exporting to excel 4 28
VB.Net - MemoryMappedFiles - Confirm receipt 2 19
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question