LINQ Query for deleting one column from List(Of )
I have this code:
Dim prueba As New SvcEduardo.Service1
Dim listaAnalisis As List(Of SvcService.Analisis)
listaAnalisis = prueba.GetClinicoAnalisis()
'Dim listaModificada = (From l In listaAnalisis
' Select l.Colum1, l.Colum2).ToList
DataGridView1.DataSource = listaModificada
listaAnalisis is a List which contains classes (Analisis) which have 3 properties each.
listaModificada is suppose to remove one property and keep the other开发者_JS百科 two so i can bind it to a DataGridView. Can I do this with LINQ? Or is strictly necessary to use another Class structure (AnalisisModified) which only contains the two properties that I need? I'm trying to avoid that.EDIT
'Dim listaModificada = (From l In listaAnalisis
' Select l.Colum1, l.Colum2).ToList
Here I can't select the columns because listaAnalisis is a List, how can I set the query as it where a single Class(Analisis)?
You won't need to remove the property from the class. Simply don't show the column/property on your GridView.
Perhaps you can toggle the AutoGenerateColumns
property:
DataGridView.AutoGenerateColumns = False
Then explicity set the columns on the DGV that you actually want to be shown. You can do this at design-time, or at run-time:
dataGridView1.Columns.Add( "CustomerName", "Name")
dataGridView1.Columns.Add( "CustomerStatus", "Current Status")
dataGridView1.Columns.Add( "CustContactPhone", "Phone")
or more granular control on the column:
Dim dvc as New DataGridViewColumn()
dvc.HeaderText = "ID"
dvc.DataPropertyName = "CustomerID"
' all the properties you need.
dataGridView1.Columns.Add(dvc)
dataGridView1.DataSource = listaModificada
You still want to AutoGenerateColumns, so dont set that to false as it will stop the generation of all columns after that.
Instead under the AutoGenerateColumns Event for your datagrid try using a Select statement on the column header, and using e.Cancel for the one you don't want to show.
Ex.
Private Sub SearchResults_AutoGeneratingColumn(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs) Handles SearchResults.AutoGeneratingColumn
Dim colName As String
colName = e.Column.Header
Select Case colName
Case "_city"
e.Column.Header = "City"
Case "_firstName"
e.Column.Header = "First Name"
Case "_lastName"
e.Column.Header = "Last Name"
Case "_state"
e.Column.Header = "State"
Case "_orgCode", "_accntCode", "_state_name"
e.Cancel = True
End Select
End Sub
The final case handles the ones that you dont want to show. In this example, i have also changed the names of the column headers to a more readable name. Hope this helps!
A very simple example
<asp:DataGrid ID="Grid" runat="server"AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn HeaderText="Property1" DataField="Property1"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Property2" DataField="Property2"></asp:BoundColumn>
</asp:DataGrid>
精彩评论