This is a migrated thread and some comments may be shown as answers.

Can We User LINQ work on GridView's GridViewRowCollection?

5 Answers 231 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hector
Top achievements
Rank 1
Hector asked on 10 Aug 2011, 02:51 PM
I wanted to see if I can use LINQ on RADGridView's GridViewRowCollection (Telerik.Wincontrols.UI.GridViewRowCollection).

So I tried the following VB.NET code and got an error:

Dim lockedRows = _
    From row In Me.dgTotalCosts.Rows _
    Where row.Cells("colLocked").Value = True
    Select New With {.Code = row.Cells("colCode").Value, _
                     .Description = row.Cells("colDesc").Value}

... when I then tried to check the value of lockedRows.Count. So i tried changing it around a little bit to:

Dim lockedRows As IEnumerable(Of Telerik.WinControls.UI.GridViewRowInfo) = _
    From row As Telerik.WinControls.UI.GridViewRowInfo In Me.dgTotalCosts.Rows _
    Where row.Cells("colLocked").Value = True
    Select New With {.Code = row.Cells("colCode").Value, _
                     .Description = row.Cells("colDesc").Value}

But then got the following error (which I've somewhat abbreviated for briefness):

Unable to cast object of type 'WhereSelectEnumerableIterator'2[Telerik.WinControls.UI.GridViewRowInfo, VB$AnonymousType_0'6
[System.Object, ...]]' to type 'System.Collections.Generic.IEnumerable'1[Telerik.WinControls.UI.GridViewRowInfo]'.

So, the first question is, can I use LINQ on GridView's GridViewRowCollection. Secondly, if so, how (can you show me using my example code, above, where I'm trying to pull out the locked rows from a GridView (

Telerik.WinControls.UI.

 

RadGridView

 

)?

Thanks in advance for your help from a LINQ and RADGridView Newbie.

5 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 11 Aug 2011, 02:07 PM
Hello Hector,

You can use LINQ with a GridViewRowCollection instance. Here is a sample code snippet:

Dim lockedRows = From row In Me.radGridView1.Rows Where Object.Equals(row.Cells("ID").Value, 1)New With { _
    Key .Code = row.Cells("Name").Value, _
    Key .Description = row.Cells("Description").Value _
}

Greetings,
Svett
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Hector
Top achievements
Rank 1
answered on 11 Aug 2011, 06:39 PM
Svett,

thanks for your prompt reply. Did you mistakenly forget the 'Select' before the 'New With'? Also, remember now, I'm pretty new to Linq, especially Linq using VB.NET (unfortunately the language I must use at the present), so forgive me for what may be fundamental or elementary questions, but what is the purpose of the 'Key' in the object initializer? Is it specific to VB.NET?

More importantly, though, let's assume I get the above to work. How would I then iterate through the results?

I tried the following code:

Dim lockedRows = _
    From row In Me.dgTotalCosts.Rows _
    Where Object.Equals(row.Cells("colLock").Value, True) _
    Select New With {Key .Code = row.Cells("colTotalCode").Value, _
                     Key .Description = row.Cells("colTotCostDesc").Value}

. . . then:

For Each gvr As tlrk.GridViewRowInfo In lockedRows
    Dim sCode As String = gvr.Cells("colTotCostDesc").Value
    Dim sDescription As String = gvr.Cells("colTotCostDesc").Value
    MessageBox.Show("UsingLINQ    " & vbCrLf & _
                    "  Code: " & sCode & vbCrLf & _
                    "  sDescription: " & sDescription)
Next

. . . and got the following error:

Exception: Unable to cast object of type 'VB$AnonymousType_0'6(Of Obect, ...)' to type 'Telerik.WinControls.UI.GridViewRowInfo'.


0
Accepted
Svett
Telerik team
answered on 15 Aug 2011, 12:31 PM
Hello Hector,

I am enclosing a sample project that demonstrates how you can use the GridViewRowCollection with LINQ.

Regards,
Svett
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Hector
Top achievements
Rank 1
answered on 15 Aug 2011, 02:06 PM

Svett,

thanks, the sample project brought me to the solution. However, I'd like to point out that I tried the following For Each loop:

 

For Each gvr In lockedRows
    MessageBox.Show("UsingLINQ    " & vbCrLf & _
                    "  Code: " & gvr.Code & vbCrLf & _
                    "  Description: " & gvr.Code)
Next

. . . but got compilation error:

'gvr' is not declared. It may be inaccessible due to its protection level.
- Generate method stub for 'gvr' in 'Namespace.ClassName'
- Generate property stub for 'gvr' in 'Namespace.ClassName'
- Generate field for 'gvr' in 'Namespace.ClassName'

So I changed it to:

 

For Each gvr As Object In lockedRows
    MessageBox.Show("UsingLINQ    " & vbCrLf & _
                    "  Code: " & gvr.Code & vbCrLf & _
                    "  Description: " & gvr.Code)
Next

. . . and it worked. Note, the addition of 'As Object'.

For those following the thread and wanting to see the full code that populated the lockedRows LINQ expression variable along with the For Each processing loop, here it is:

Dim lockedRows = _
From row In Me.RadGridView1.Rows _
Where Object.Equals(row.Cells("colIsLocked").Value, True) _
Select New With {.Code = row.Cells("colCode").Value, _
            .Description = row.Cells("colDesc").Value}
For Each gvr As Object In lockedRows
    MessageBox.Show("UsingLINQ Debugging   " & vbCrLf & _
                    "  Code: " & gvr.Code & vbCrLf & _
                    "  Description: " & gvr.Description)
Next

The thing I'd like to add, I guess, is that in the For Each loop, the elements being iterated are not of the type GridViewRowInfo, and instead are of the anonymous type created in the 'Select' query operator.

Regards,

Hector (LINQ and Rad WinForms controls newbie)

0
Svett
Telerik team
answered on 16 Aug 2011, 09:51 AM
Hello Hector,

I am glad to hear that the supplied project helped you to understand how to use LINQ with GridViewRowCollection.

Feel free to write back if you have other questions.

Best wishes,
Svett
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
GridView
Asked by
Hector
Top achievements
Rank 1
Answers by
Svett
Telerik team
Hector
Top achievements
Rank 1
Share this question
or