Andrew
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.
TIA,
Andrew
<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>
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
TIA,
Andrew
ASKER
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
@Abrahams
where is SQL / DB involved here...
I guess, we need to insert into DB and query each time...
not sure though...
where is SQL / DB involved here...
I guess, we need to insert into DB and query each time...
not sure though...
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!
Thanks!
ASKER
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>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window