selectedindex of multiple listbox selection in a datagrid

G

Guest

Hi all,

I have problem in getting selectedindex of multiple listbox selection in a
datagrid. I have a listbox with multiple selection mode inside datagrid. In
Edit mode, I need to get back all selected items of that listbox and display
it.
can anyone help?

Thanks

rgds,
Lie
 
K

Ken Cox [Microsoft MVP]

Hi Lie,

You need to get a reference to the listbox from the datagriditem that was
clicked. After that, you loop through the listbox items to see which ones
were selected. I've put some code below that demonstrates the idea.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemCommand _
(ByVal source As Object, ByVal e As _
System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
Dim lbx As ListBox
Dim lbxitm As ListItem
Dim itm As DataGridItem
itm = e.Item
lbx = itm.Cells(0).FindControl("listbox1")
Label1.Text = ""
For Each lbxitm In lbx.Items
If lbxitm.Selected = True Then
Label1.Text = Label1.Text & lbxitm.Text & "<br>"
End If
Next
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<p>
<asp:label id="Label1" runat="server"></asp:label></p>
<asp:datagrid id="DataGrid1" runat="server"
autogeneratecolumns="False">
<columns>
<asp:templatecolumn>
<itemtemplate>
<p>
<asp:listbox id="ListBox1" runat="server"
SelectionMode="Multiple">
<asp:listitem
Value="Red">Red</asp:listitem>
<asp:listitem
Value="Green">Green</asp:listitem>
<asp:listitem
Value="Blue">Blue</asp:listitem>
<asp:listitem
Value="White">White</asp:listitem>
</asp:listbox></p>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn HeaderText="StringValue">
<itemtemplate>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.StringValue") %>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:buttoncolumn Text="Select"
CommandName="Select"></asp:buttoncolumn>
</columns>
</asp:datagrid>
 
J

Jeffrey Palermo [MCP]

Ken,
You example was good, but there is one line that I believe should be
simplified:

lbx = itm.Cells(0).FindControl("listbox1")

Since you are using a TemplateColumn, and you control the ID, it is
unnecessary to call e.Item.Cells(0).FindControl(). You can merely call
e.Item.FindControl("listbox1"), and you will find the control.

--
Best regards,
Jeffrey Palermo
Blog: http://dotnetjunkies.com/weblog/jpalermo


Ken Cox said:
Hi Lie,

You need to get a reference to the listbox from the datagriditem that was
clicked. After that, you loop through the listbox items to see which ones
were selected. I've put some code below that demonstrates the idea.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemCommand _
(ByVal source As Object, ByVal e As _
System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
Dim lbx As ListBox
Dim lbxitm As ListItem
Dim itm As DataGridItem
itm = e.Item
lbx = itm.Cells(0).FindControl("listbox1")
Label1.Text = ""
For Each lbxitm In lbx.Items
If lbxitm.Selected = True Then
Label1.Text = Label1.Text & lbxitm.Text & "<br>"
End If
Next
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<p>
<asp:label id="Label1" runat="server"></asp:label></p>
<asp:datagrid id="DataGrid1" runat="server"
autogeneratecolumns="False">
<columns>
<asp:templatecolumn>
<itemtemplate>
<p>
<asp:listbox id="ListBox1" runat="server"
SelectionMode="Multiple">
<asp:listitem
Value="Red">Red</asp:listitem>
<asp:listitem
Value="Green">Green</asp:listitem>
<asp:listitem
Value="Blue">Blue</asp:listitem>
<asp:listitem
Value="White">White</asp:listitem>
</asp:listbox></p>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn HeaderText="StringValue">
<itemtemplate>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.StringValue") %>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:buttoncolumn Text="Select"
CommandName="Select"></asp:buttoncolumn>
</columns>
</asp:datagrid>

Lie said:
Hi all,

I have problem in getting selectedindex of multiple listbox selection in a
datagrid. I have a listbox with multiple selection mode inside datagrid.
In
Edit mode, I need to get back all selected items of that listbox and
display
it.
can anyone help?

Thanks

rgds,
Lie
 
G

Guest

Hi,

Thanks for all replies.
I've tried both ways:
lbx = e.Item.FindControl("listbox1")
and
lbx= e.Item.Cells(8).FindControl("listbox1")

both return nothing ..couldn't find the control. any idea?

Thanks
rgds,
Lie



Jeffrey Palermo said:
Ken,
You example was good, but there is one line that I believe should be
simplified:

lbx = itm.Cells(0).FindControl("listbox1")

Since you are using a TemplateColumn, and you control the ID, it is
unnecessary to call e.Item.Cells(0).FindControl(). You can merely call
e.Item.FindControl("listbox1"), and you will find the control.

--
Best regards,
Jeffrey Palermo
Blog: http://dotnetjunkies.com/weblog/jpalermo


Ken Cox said:
Hi Lie,

You need to get a reference to the listbox from the datagriditem that was
clicked. After that, you loop through the listbox items to see which ones
were selected. I've put some code below that demonstrates the idea.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemCommand _
(ByVal source As Object, ByVal e As _
System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
Dim lbx As ListBox
Dim lbxitm As ListItem
Dim itm As DataGridItem
itm = e.Item
lbx = itm.Cells(0).FindControl("listbox1")
Label1.Text = ""
For Each lbxitm In lbx.Items
If lbxitm.Selected = True Then
Label1.Text = Label1.Text & lbxitm.Text & "<br>"
End If
Next
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<p>
<asp:label id="Label1" runat="server"></asp:label></p>
<asp:datagrid id="DataGrid1" runat="server"
autogeneratecolumns="False">
<columns>
<asp:templatecolumn>
<itemtemplate>
<p>
<asp:listbox id="ListBox1" runat="server"
SelectionMode="Multiple">
<asp:listitem
Value="Red">Red</asp:listitem>
<asp:listitem
Value="Green">Green</asp:listitem>
<asp:listitem
Value="Blue">Blue</asp:listitem>
<asp:listitem
Value="White">White</asp:listitem>
</asp:listbox></p>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn HeaderText="StringValue">
<itemtemplate>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.StringValue") %>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:buttoncolumn Text="Select"
CommandName="Select"></asp:buttoncolumn>
</columns>
</asp:datagrid>

Lie said:
Hi all,

I have problem in getting selectedindex of multiple listbox selection in a
datagrid. I have a listbox with multiple selection mode inside datagrid.
In
Edit mode, I need to get back all selected items of that listbox and
display
it.
can anyone help?

Thanks

rgds,
Lie
 
K

Ken Cox [Microsoft MVP]

Hey Lie, post the code? You may not being looking in the right cell.
 
G

Guest

Hi Ken,

Here's the code:

<asp:datagrid id="dgAssignMM" runat="server" Width="100%"
Font-Size="X-Small" BackColor="White" Font-Names="Tahoma" PageSize="20"
AutoGenerateColumns="False" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" CellPadding="1" DataKeyField="no">
<SelectedItemStyle Font-Bold="True" ForeColor="Black"
BackColor="LightCoral"></SelectedItemStyle>
<EditItemStyle BackColor="LightCoral"></EditItemStyle>
<AlternatingItemStyle
BackColor="MistyRose"></AlternatingItemStyle>
<ItemStyle ForeColor="#330099"
BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#A51C44"></HeaderStyle>
<FooterStyle ForeColor="#330099"
BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="Action"
ItemStyle-VerticalAlign=Top >
<HeaderStyle Wrap="False" HorizontalAlign="Center"
Width="10%"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<asp:ImageButton id="Imagebutton6" runat="server"
AlternateText="Edit Data" Imageurl="../images/edit_mm.gif"
CommandName="edit"></asp:ImageButton>
<asp:ImageButton id="ImageButton5" runat="server"
AlternateText="Edit Data" Imageurl="../images/Delete_mm.gif"
CommandName="delete"></asp:ImageButton>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton id="Imagebutton7" runat="server"
AlternateText="Save Data" Imageurl="../images/save_mm.gif"
CommandName="update"></asp:ImageButton>
<asp:ImageButton id="Imagebutton8" runat="server"
AlternateText="Cancel Updates" Imageurl="../images/Cancel_mm.gif"
CommandName="cancel"></asp:ImageButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="DIR UserID"
ItemStyle-VerticalAlign=Top>
<HeaderStyle Wrap="False"
HorizontalAlign="Center"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("dir_id") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlDIR_ID" runat="server"
Font-Size="8pt" SelectedIndex='<%#
GetSelectedIndex("dir_id",Container.DataItem("dir_id")) %>'
DataValueField="dir_id" DataTextField="dir_id"
DataSource='<%#GetSource("dir_id")%>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="MM Head UserID"
ItemStyle-VerticalAlign=Top >
<HeaderStyle Wrap="False"
HorizontalAlign="Center"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("mm_head") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlMM_head" runat="server"
Font-Size="8pt" SelectedIndex='<%#
GetSelectedIndex("mm_head",Container.DataItem("mm_head")) %>'
DataValueField="mm_head" DataTextField="mm_head"
DataSource='<%#GetSource("mm_head")%>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="MM UserID"
ItemStyle-VerticalAlign=Top>
<HeaderStyle Wrap="False"
HorizontalAlign="Center"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("username") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=ddlName runat="server"
Font-Size="8pt" DataValueField="username" DataTextField="username"
DataSource='<%#GetSource("username")%>' SelectedIndex='<%#
GetSelectedIndex("username",Container.DataItem("username")) %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Cust Sector"
ItemStyle-VerticalAlign=Top>
<HeaderStyle Wrap="False"
HorizontalAlign="Center"></HeaderStyle>
<ItemTemplate>
<%# Container.DataItem("custsector")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlCustSector" runat="server"
Font-Size="8pt" DataValueField="custsector" DataTextField="custsector"
DataSource='<%#GetSource("custsector")%>' SelectedIndex='<%#
GetSelectedIndex("custsector",Container.DataItem("custsector")) %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Industry Code"
ItemStyle-VerticalAlign=Top >
<HeaderStyle Wrap="False"
HorizontalAlign="Center"></HeaderStyle>
<ItemTemplate>
<%# Container.DataItem("industry_code")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlIndustry_code"
runat="server" Font-Size="8pt" DataValueField="Industry_Code"
DataTextField="Industry_Code" DataSource='<%#GetSource("Industry_Code")%>'
SelectedIndex='<%#
GetSelectedIndex("Industry_Code",Container.DataItem("Industry_Code")) %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="SVC_AMG"
ItemStyle-VerticalAlign=Top>
<HeaderStyle Wrap="False"
HorizontalAlign="Center"></HeaderStyle>
<ItemTemplate>
<%# Container.DataItem("svc_amg")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlsvc_amg" runat="server"
Font-Size="8pt" DataValueField="svc_amg" DataTextField="svc_amg"
DataSource='<%#GetSource("svc_amg")%>' SelectedIndex='<%#
GetSelectedIndex("svc_amg",Container.DataItem("svc_amg")) %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Country"
ItemStyle-VerticalAlign=Top>
<HeaderStyle Wrap="False"
HorizontalAlign="Center"></HeaderStyle>
<ItemTemplate>
<%# Container.DataItem("country")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlCountry" runat="server"
Font-Size="8pt" DataValueField="country" DataTextField="country"
DataSource='<%#GetSource("country")%>' SelectedIndex='<%#
GetSelectedIndex("country",Container.DataItem("country")) %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="custsector Test"
ItemStyle-VerticalAlign=Top>
<HeaderStyle Wrap="False"
HorizontalAlign="Center"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="lblCustSector"><%#
Container.DataItem("custsector")%></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:ListBox ID=lstCustSector Runat=server
Font-Size="8pt" DataValueField="custsector" DataTextField="custsector"
DataSource='<%#GetSource("custsector")%>'></asp:ListBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099"
BackColor="#FFFFCC"></PagerStyle>
</asp:datagrid>


Public Function GetSource(ByVal col As String)
Dim dt As New DataTable()
Select Case col.ToLower
Case "dir_id"
dt = CorpHandler.GetMMUsername("dir_id", "")
Case "mm_head"
dt = CorpHandler.GetMMUsername("mm_head", "")
Dim newRow As DataRow
newRow = dt.NewRow
newRow(0) = ""
dt.Rows.Add(newRow)

Case "username"
dt = CorpHandler.GetMMUsername("username", "")
Dim newRow As DataRow
newRow = dt.NewRow
newRow(0) = ""
dt.Rows.Add(newRow)

Case "custsector"
dt = CorpHandler.GetMMCustSector
Dim newRow As DataRow
newrow = dt.NewRow
newrow(0) = "ALL"
dt.Rows.InsertAt(newrow, 0)

Case "industry_code"
dt = CorpHandler.GetMMIndustryCode
Dim newRow As DataRow
newrow = dt.NewRow
newrow(0) = "ALL"
dt.Rows.InsertAt(newrow, 0)

Case "svc_amg"
dt = CorpHandler.GetMMSVC_AMG
Dim newRow As DataRow
newrow = dt.NewRow
newrow(0) = "ALL"
dt.Rows.InsertAt(newrow, 0)

Case "country"
dt = CorpHandler.GetMMCountry
Dim newRow As DataRow
newrow = dt.NewRow
newrow(0) = "ALL"
dt.Rows.InsertAt(newrow, 0)
End Select
Return dt
End Function

Function GetSelectedIndex(ByVal col As String, ByVal value As Object) As
Integer
Dim bEmpty As Boolean = False
Dim row As DataRow
Dim iLoop As Integer
Dim iSel As Integer = -1
Dim sData As String
Dim sValue As String

Dim dt As New DataTable()
dt = GetSource(col)

For iLoop = 0 To dt.Rows.Count - 1
If IsDBNull(dt.Rows(iLoop)(col)) Then
sData = "-9999"
Else
sData = dt.Rows(iLoop)(col)
End If

If IsDBNull(value) Then
sValue = "-9999"
Else
sValue = value.ToString.Trim.ToUpper
End If
If sValue = sData Then
'Return iLoop
iSel = iLoop
Exit For
End If

Next iLoop

If iSel = -1 Then
For iLoop = 0 To dt.Rows.Count - 1
If IsDBNull(dt.Rows(iLoop)(col)) Then
sData = "-9999"
Else
sData = dt.Rows(iLoop)(col)
End If
If sData = "" Then
Return iLoop
End If
Next
Else
Return iSel
End If
dt.Dispose()
End Function


Private Sub dgAssignMM_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgAssignMM.ItemCommand
Dim lbx As ListBox
Dim lbxitm As ListItem
Dim itm As DataGridItem
Dim arr As Array
itm = e.Item
'lbx = e.Item.FindControl("lstCustSector")
'e.Item.Cells(9).FindControl("lstcustsector")

arr = Split(CType(e.Item.FindControl("lblCustSector"), Label).Text,
",")
' don't know what to put here .. as i can't get the control..

End Sub

in display, custsector is in label control. Only after pressing EDIT, we
will display in listbox with its multiple selection selected .. but i still
have no idea how to get those selected value from label to listbox.

Thanks
rgds,
Lie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top