Linq Select All Except items from listbox - asp.net

I've been struggling on a way to do this for a while. I have this a listbox in asp.net that I use that looks like :
<asp:ListBox ID="lstLinkedProspect" runat="server" SelectionMode="Multiple" />
What I want to do with it is to be able to select all items I have in a specific table, all except the one that are in the listbox. Not only the selected but all items that are already in the list. I use System.Linq.Dynamic library. Here is what I'm trying to do :
Dim lstProspect = dbConnection.Prospects.Where(If(String.IsNullOrWhiteSpace(SearchFilters.Value), "ProspectId > 0", SearchFilters.Value).ToString) _
.Except(' There goes all the items in the list box)
What I have already tried is :
Dim lstProspect = dbConnection.Prospects.Where(If(String.IsNullOrWhiteSpace(SearchFilters.Value), "ProspectId > 0", SearchFilters.Value).ToString) _
.Except(lstLinkedProspect.Items.Cast(Of ListItem)().Where(Function(x) x.Value).Cast(Of Beans.Prospect))
Is it possible in Linq to get all the items from a table that are not in the listbox? Note that the Value property of the ListItem in the listbox is the ProspectId.
Thanks in advance.

This is the the code I found to achieve what I was looking for :
Dim lstItems = dbConnection.Prospects.Where(Function(x) ids.Contains(x.ProspectId))
Dim lstProspect = dbConnection.Prospects.Where(If(String.IsNullOrWhiteSpace(SearchFilters.Value),
"ProspectId > 0", SearchFilters.Value).ToString).Except(lstItems)
Hope this can help anyone struggling with that problem.

Related

ASP.NET Basic GridView control not displaying at all

I am getting started with a basic asp.net gridview control and I just dont seem to be able to make it visible in the web browser at all in an asp.net web application.
Here is what I have just to get myself started off. In the aspx page ...
<asp:GridView ID="myGr" runat="server" BackColor="Aqua" AutoGenerateColumns="True" Width="100%" ViewStateMode="Enabled"></asp:GridView>
In the code behind I have ...
Dim tbl As New DataTable
tbl.Columns.Add("ID", GetType(Int32))
tbl.Columns.Add("Name", GetType(String))
tbl.Columns.Add("Birthday", GetType(Date))
Dim pers As DataRow = tbl.NewRow
pers("ID") = 1
pers("Name") = "Tim"
pers("Birthday") = New Date(1973, 6, 9)
myGr.DataSource = tbl
myGr.DataBind()
Would be great if anyone could give me some basic advice on this control
You need to add the row you inserted to the datatable tbl. Because you haven't done this, the datatable is empty so the Gridview is empty too.
Like that: tbl.Rows.add(pers)
You create row but you do not add it to the table.
You have to add line like that:
tbl.Rows.Add(pers)

ASP.NET : GridView in dynamics accordionPanes Ajax

What i'm trying to do : I have some meeting date in my database. For display this, I want make one GridView for each day. (I have several meetings by days). And after, put this gridview in accordionPane (one by day). I want put in Pane for not display a long list in the screen.
I know how make dynamics accordionPanes, I know make dynamics GridView (depends on the number of meetings).
I know put one GridView in one accordionPane. (juste for statics one)
But I can't put dynamically GridViews in accordionPanes...
The source code of my problem :
The way to create dynamic number of accordionPanes :
Code Behind :
For i = 1 To j
Dim volet As New AjaxControlToolkit.AccordionPane
volet.Visible = True
volet.ID = "volet" & i.ToString
Accordion1.Panes.Add(volet)
Next
Code ASP.NET :
<asp:Accordion ID="Accordion1" runat="server" Width="873px" CssClass="accordion" FadeTransitions="true" HeaderCssClass="accordionHeader" HeaderSelectedCssClass="accordionHeaderSelected" RequireOpenedPane="false" ContentCssClass="accordionContent" TransitionDuration="100">
</asp:Accordion>
The way to create dynamic number of GridView :
Code behind :
For i = 1 To j
Dim datag As New GridView
datag.Visible = True
datag.ID = "datag" & i.ToString
Panel1.Controls.Add(datag)
Next
Now I try to insert GridView in accordionPanes. That's my issue.
I hope i'm clear, ask me if i'm not well expressed.
Thanks a lot for your answers
I found my answer !
I missed two functions : pane.ContentContainer.Constrols.Add and pane.HeaderContent.Controls.Add.
For i = 1 To j
Dim datag As New GridView
Dim title As New Label
datag = GenererGridView(i) 'GridView from database
datag.Visible = True
datag.ID = "datag" & i.ToString
title.Text = "datag" & i.ToString
Dim pane As New AjaxControlToolkit.AccordionPane
pane.Visible = True
pane.ID = "pane" & i.ToString
pane.ContentContainer.Controls.Add(datag)
pane.HeaderContainer.Controls.Add(title)
Accordion1.Panes.Add(pane)
Next

Dynamically Adding Multiple User Controls vb.net

I have a User Control which returns a table of data, which in some cases needs to be looped, displaying one on top of another.
I am able to dynamically add a single instance of this by placing a fixed placeholder in the page.
I am now trying to work out how to add more than one, given that I don't know how many might be needed I don't want to hard code the Placeholders.
I've tried the following, but I am just getting one instance, presumably the first is being overwritten by the second
My HTML
<div id="showHere" runt="server"/>
VB
Dim thisPh As New PlaceHolder
thisPh.Controls.Add(showTable)
showHere.Controls.Add(thisPh)
Dim anotherPh As New PlaceHolder
anotherPh .Controls.Add(showTable)
showHere.Controls.Add(anotherPh)
How do I make it add repeated tables within the showHere div?
I would advise generating a different ID for each of your table. For example,
Dim i As Integer
i = 0
For each tbl in ShowTables
tbl.ID = "MyTab" + i.ToString()
i = i + 1
showHere.Controls.Add(tbl)
showHere.Controls.Add(New LiteralControl("<br />"))
Next
On other hand, it would make more sense to have a your user/custom control generate html for a single table and then nest your user/custom control within a repeater (or similar control such as ListView etc).
Did you tried simply, this:
For each tbl in ShowTables
showHere.Controls.Add(tbl)
showHere.Controls.Add(New LiteralControl("<br />"))
Next
After fussing with this issue myself, I stumbled across below solution.
On button click()
LocationDiv.Visible = True
Dim existingItems As New List(Of Object)
If Not Session("existingItems") Is Nothing Then
existingItems = CType(Session("existingItems"), List(Of Object))
For Each item As Object In existingItems
LocationDiv.Controls.Add(item)
Next
existingItems.Clear()
End If
LocationDiv.Controls.Add(New LiteralControl("<b>" & Text & "</b>"))
For Each item As Object In LocationDiv.Controls
existingItems.Add(item)
Next
Session.Add("existingItems", existingItems)

Add New Item to Already Bound ListView in ASP Net (Unable to set DataKeys/FieldName)

My overall goal is to add fake/unbound items to a listview control (for final HTML Table output reasons). This is a code behind solution. Users will not be adding items as it will be outputted in a rigid table.
I have looked at several examples and while this is easy for a dropdown it is not for listview.
The code below works without error, but my item is not shown on runtime. I think the class is not setting the item fieldname correctly, but I can't figure out the right syntax to fix it.
ColumnNameAList.DataSource = PeriodDataView
ColumnNameAList.DataBind()
Dim test As New Example1("ColumnNameA")
Dim newItem As New ListViewDataItem(ColumnNameAList.Items.Count, ColumnNameAList.Items.Count)
newItem.DataItem = test
ColumnNameAList.Items.Insert(ColumnNameAList.Items.Count, newItem)
ColumnNameAList.Items.Add(newItem)
Here is the Example1 class that is supposed to set the DataValueField:
Public Class Example1
Public Sub New(ColumnNameA__1 As String)
ColumnNameA = ColumnNameA__1
End Sub
Private m_ColumnNameA As String
Public Property ColumnNameA() As String
Get
Return m_ColumnNameA
End Get
Set(value As String)
m_ColumnNameA = value
End Set
End Property
End Class
This outputs my original datasource list, but not the added item.
<ItemTemplate>
<td>
<%# Eval("ColumnNameA")%>
</td>
</ItemTemplate>
In the end I could only reliably solve this with a codebehind solution.
I made a copy of the original datasource, modified my copy and then databound to it.
Dim MyOriginalTableSource As Data.DataView = DataManager.example()
Dim ModifiedTable As DataTable = MyOriginalTableSource.ToTable
'do stuff here
Mylistbox.DataSource = ModifiedTable
Mylistbox.DataBind()
Won't work for everyone, but in this case it works fine for me.
There could be a couple of issues with the way you are approaching this, including that the ListView is already databound and that you are both adding and inserting the newItem.
When we have a scenario like this, we take one of two approaches:
1) Add the new item to the data source before the source is data bound.
2) Remove databinding and manually create each of the list view items, then add your new item at the beginning or end of the loop.
Another way to do it would be to inject it into the sql.
select col1, col2, col3 from table1 union select '1','2','3'
this would ensure that the item is always added, and asp.net doesn't need to know or care.
You can add this into the sql query or add it from the behind code before binding query. if you are not binding with sql, you can also do this to any list item with LINQ

Format BoundField Data Types Dynamically

I'm using a CheckBoxList to define which columns are displayed in a GridView. I populate the CheckBoxList using a query like
select column_name, data_type from information_schema.columns
where table_name = 'myTable'
After the user has chosen the columns (with various Data Types in them) they wish to display, they press a button and the following snippet of VB code generates the GridView.
For Each item As ListItem In chooseColsList.Items
If item.Selected Then
Dim bf As New BoundField()
'PRODUCES BUGGY RESULTS BECAUSE APPLIED TO ALL BoundFields
bf.DataFormatString = "{0:dd-MMM-yyyy}"
bf.ApplyFormatInEditMode = True
bf.DataField = item.Value
bf.HeaderText = item.Value
bf.SortExpression = item.Value
statusReportGrid.Columns.Add(bf)
End If
Next
What I want to do is to apply the DataFormatString ONLY to the columns with a 'date' Data Type in them. However, I have found no way of programmatically determining the Type of the data in the column being bound, no any way of passing this info to the Control. This information exists in the information_schema, as shown in my query, but I don't know how to extract that out and use it to dynamically setup my BoundFields. It is important to note that I'm using an SqlDataSource.
I've experimented with just about every possible solution I can think of and end up here.
Thanks in advance for your help, it is VERY appreciated :)
If you set your check box list like this:
<asp:checkboxlist id="list" runat="server"
DataTextField="column_name" DataValueField="data_type" DataSourceID="YourDSID" />
You should be able to iterate through the items and check the value of the current Item like so:
For Each item As ListItem In chooseColsList.Items
If item.Selected Then
Dim bf As New BoundField()
If StrComp(item.Value,"date") = 0 Then 'Or however datetime is returned
bf.DataFormatString = "{0:dd-MMM-yyyy}"
bf.ApplyFormatInEditMode = True
End If
bf.DataField = item.Text
bf.HeaderText = item.Text
bf.SortExpression = item.Text
statusReportGrid.Columns.Add(bf)
End If
Next

Resources