472,145 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

selectedindex of multiple listbox selection in a datagrid

Lie
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
Nov 19 '05 #1
5 3414
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" <Li*@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
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


Nov 19 '05 #2
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 [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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" <Li*@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
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

Nov 19 '05 #3
Lie
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 [MCP]" wrote:
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 [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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" <Li*@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
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


Nov 19 '05 #4
Hey Lie, post the code? You may not being looking in the right cell.
"Lie" <Li*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
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


Nov 19 '05 #5
Lie
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("us ername")) %>'>
</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.DataIte m("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("cou ntry")) %>'>
</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

"Ken Cox [Microsoft MVP]" wrote:
Hey Lie, post the code? You may not being looking in the right cell.
"Lie" <Li*@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
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


Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Russell Campbell | last post: by
2 posts views Thread by Richard Loupatty | last post: by
1 post views Thread by Carlo Marchesoni | last post: by
2 posts views Thread by Dolorous Edd | last post: by
2 posts views Thread by amtamayo | last post: by
1 post views Thread by Marisa | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.