Link to home
Start Free TrialLog in
Avatar of Andrew
AndrewFlag for United States of America

asked on

Persist KeyValuePair and bind to gridview?

I am having trouble figuring out how to generate a key value pair based on a barcode scan (text changed event) and bind it to a gridview on postback.  The  key value pairs are to populate 2 columns of the gridview.  Barcode,Plant (this come from a SQL db lookup based on the Barcode value).  I was able to persist a single value (barcode) by using a session variable, but I do not understand how to achieve this while needing 2 values to bind to the grid. Every time a barcode is scanned, the gridview should append the new key value pair to the existing values already bound to the gridview.

User generated image
<telerik:RadGrid ID="rgTrashed" runat="server" RenderMode="Lightweight" AutoGenerateColumns="False" CellSpacing="-1" AllowSorting="True" Height="500px" Width="25%">
	<GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>
	<ClientSettings EnableRowHoverStyle="true">
		<Scrolling AllowScroll="True" UseStaticHeaders="True" />
	</ClientSettings>
	<MasterTableView DataSourceID="" DataKeyNames="">
		<Columns>
			<telerik:GridTemplateColumn UniqueName="BarCode" HeaderText="BarCode">
				<ItemTemplate>
					<asp:Label ID="BarCode" runat="server" Text=<%# DataBinder.Eval(Container,"DataItem") %>></asp:Label>
				</ItemTemplate>
			</telerik:GridTemplateColumn>
			<telerik:GridTemplateColumn UniqueName="Plant" HeaderText="Plant">
				<ItemTemplate>
					<asp:Label ID="Plant" runat="server" Text=<%# DataBinder.Eval(Container,"DataItem") %>></asp:Label>
				</ItemTemplate>
			</telerik:GridTemplateColumn>
		</Columns>
	</MasterTableView>
	<FilterMenu RenderMode="Lightweight"></FilterMenu>
	<HeaderContextMenu RenderMode="Lightweight"></HeaderContextMenu>
</telerik:RadGrid>

Open in new window


Protected Sub txtScan_TextChanged(sender As Object, e As EventArgs) Handles txtScan.TextChanged

	Dim _items As New List(Of KeyValuePair(Of String, String))()
	_items.Add(New KeyValuePair(Of String, String)(txtScan.Text, "Plant"))  'Plant is a value from SQL lookup based on txtScan.text value
	rgTrashed.DataSource = _items
	rgTrashed.DataBind()
	
End Sub

Open in new window


TIA,
Andrew
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Save your dictionary to the session and bind to that:
Dim _items As New List(Of KeyValuePair(Of String, String))();

'first time thru, don't set it to anything;
if Session("Items") IsNot Nothing  
   _items = Ctype(Session("Items"),  KeyValuePair(Of String, String))

_items.Add(New KeyValuePair(Of String, String)(txtScan.Text, "Plant"))
Session("Items") = _items
rgTrashed.DataSource = _items
rgTrashed.DataBind()

Open in new window

Avatar of Andrew

ASKER

User generated image
Protected Sub txtScan_TextChanged(sender As Object, e As EventArgs) Handles txtScan.TextChanged

        Dim _items As New List(Of KeyValuePair(Of String, String))()
        'first time thru, don't set it to anything;
        if Session("Items") IsNot Nothing  
            _items = Ctype(Session("Items"),  KeyValuePair(Of String, String))

            _items.Add(New KeyValuePair(Of String, String)(txtScan.Text, "Plant"))
            Session("Items") = _items
            rgTrashed.DataSource = _items
            rgTrashed.DataBind()
        end if	
End Sub

Open in new window

Avatar of Andrew

ASKER

Is this the proper syntax?

Dim _items As New List(Of KeyValuePair(Of String, String))()
        'first time thru, don't set it to anything;
        if Session("Items") IsNot Nothing  
            _items = CType(Session("Items"), List(Of KeyValuePair(Of String,String)))

            _items.Add(New KeyValuePair(Of String, String)(txtScan.Text, "Plant"))
            Session("Items") = _items
            rgTrashed.DataSource = _items
            rgTrashed.DataBind()
        end if

Open in new window

Avatar of Andrew

ASKER

I no longer get the error, but nothing is bound to the grid on postback when I scan a barcode now.
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Andrew

ASKER

Ok, again, no errors, but the grid is not bound with anything when barcode is scanned.  Should there be some logic in Page_Load?


 Protected Sub txtScan_TextChanged(sender As Object, e As EventArgs) Handles txtScan.TextChanged

        Dim _items As New List(Of KeyValuePair(Of String, String))()
        'first time thru, don't set it to anything;
        if Session("Items") IsNot Nothing  
            _items = CType(Session("Items"), List(Of KeyValuePair(Of String,String)))

            _items.Add(New KeyValuePair(Of String, String)(txtScan.Text, "Plant"))
            Session("Items") = _items
            rgTrashed.DataSource = _items
            rgTrashed.DataBind()
        end if

End Sub

Open in new window

@Abrahams

where is SQL / DB involved here...
I guess, we need to insert into DB and query each time...

not sure though...
Avatar of Andrew

ASKER

The SQL lookup would happen in the loop each time, yes.  For now the static "Plant" value is fine in order to get this process working, then I can implement the DB work.

Thanks!
Avatar of Andrew

ASKER

I added this to Page_Load and now get this:

If Not Page.IsPostBack Then

	Session("Items") = New List(Of KeyValuePair(Of String,String))

End If

Open in new window


User generated image
Avatar of Andrew

ASKER

Why are there brackets around the value?

<telerik:RadGrid ID="rgTrashed" runat="server" RenderMode="Lightweight" AutoGenerateColumns="False" CellSpacing="-1" AllowSorting="True" Height="500px" Width="25%">
	<GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>
	<ClientSettings EnableRowHoverStyle="true">
		<Scrolling AllowScroll="True" UseStaticHeaders="True" />
	</ClientSettings>
	<MasterTableView DataSourceID="" DataKeyNames="">
		<Columns>
			<telerik:GridTemplateColumn UniqueName="BarCode" HeaderText="BarCode">
				<ItemTemplate>
					<asp:Label ID="BarCode" runat="server" Text=<%# DataBinder.Eval(Container,"DataItem") %>></asp:Label>
				</ItemTemplate>
			</telerik:GridTemplateColumn>
		</Columns>
	</MasterTableView>
	<FilterMenu RenderMode="Lightweight"></FilterMenu>
	<HeaderContextMenu RenderMode="Lightweight"></HeaderContextMenu>
</telerik:RadGrid>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Andrew

ASKER

Thank you!

User generated image