S
shapper
Hello,
I need to loop though each row in a GridView and if the checkbox is a
Template Field is checked I want to display the value of an invisible
column named "LevelName".
I tried everything I could think off but I always get an error or no
value.
Could someone please let me know what might be wrong?
Here is my GridView init code:
1 Private Sub gvLevels_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles gvLevels.Init
2
3 With gvLevels
4 .AutoGenerateColumns = False
5 .DataKeyNames = New String() {"LevelName"}
6 .DataSource = Levels_Fill()
7 .ID = "gvLevels"
8 End With
9
10 Dim bfLevelDescription As New BoundField
11 bfLevelDescription.DataField = "LevelDescription"
12 gvLevels.Columns.Add(bfLevelDescription)
13
14 Dim tfLevelSubscription As New TemplateField
15 tfLevelSubscription.ItemTemplate = New
tfLevelSubscription(ListItemType.Item)
16 gvLevels.Columns.Add(tfLevelSubscription)
17
18 gvLevels.DataBind()
19
20 End Sub ' gvLevels_Init
My grid view data source is:
1 Private Function Levels_Fill() As DataView
2
3 Dim dtLevels As New DataTable("Levels")
4 Dim dvLevels As DataView
5 dtLevels = Levels.GetAllLevels().Tables(0)
6 dtLevels.Columns.Add(New DataColumn("LevelSubscription",
GetType(Boolean)))
7
8 ' Define dtLevels primary key
9 dtLevels.PrimaryKey = New DataColumn()
{dtLevels.Columns("LevelName")}
10
11 dvLevels = New DataView(dtLevels, "", "LevelName",
DataViewRowState.CurrentRows)
12 Return dvLevels
13
14 End Function ' Levels_Fill
I am trying to access my check boxes as follows:
1 Dim dkLevel As DataKey
2 For Each gvrLevel As GridViewRow In gvLevels.Rows
3 Dim cbLevelSubscription As CheckBox =
CType(gvrLevel.FindControl("cbLevelSubscription"), CheckBox)
4 If cbLevelSubscription.Checked Then
5 dkLevel = gvLevels.DataKeys(gvrLevel.RowIndex)
6 Response.Write(dkLevel.Value)
7 End If
8 Next gvrLevel
When I click the button which will run the code that loops through
each check box I get the following error:
"Item has already been added.Key in dictionay: "LevelName" Key been
added: "LevelName"
I removed:
5 .DataKeyNames = New String() {"LevelName"}
And I stopped having this error but still does not work.
Any idea?
Thanks,
Miguel
I need to loop though each row in a GridView and if the checkbox is a
Template Field is checked I want to display the value of an invisible
column named "LevelName".
I tried everything I could think off but I always get an error or no
value.
Could someone please let me know what might be wrong?
Here is my GridView init code:
1 Private Sub gvLevels_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles gvLevels.Init
2
3 With gvLevels
4 .AutoGenerateColumns = False
5 .DataKeyNames = New String() {"LevelName"}
6 .DataSource = Levels_Fill()
7 .ID = "gvLevels"
8 End With
9
10 Dim bfLevelDescription As New BoundField
11 bfLevelDescription.DataField = "LevelDescription"
12 gvLevels.Columns.Add(bfLevelDescription)
13
14 Dim tfLevelSubscription As New TemplateField
15 tfLevelSubscription.ItemTemplate = New
tfLevelSubscription(ListItemType.Item)
16 gvLevels.Columns.Add(tfLevelSubscription)
17
18 gvLevels.DataBind()
19
20 End Sub ' gvLevels_Init
My grid view data source is:
1 Private Function Levels_Fill() As DataView
2
3 Dim dtLevels As New DataTable("Levels")
4 Dim dvLevels As DataView
5 dtLevels = Levels.GetAllLevels().Tables(0)
6 dtLevels.Columns.Add(New DataColumn("LevelSubscription",
GetType(Boolean)))
7
8 ' Define dtLevels primary key
9 dtLevels.PrimaryKey = New DataColumn()
{dtLevels.Columns("LevelName")}
10
11 dvLevels = New DataView(dtLevels, "", "LevelName",
DataViewRowState.CurrentRows)
12 Return dvLevels
13
14 End Function ' Levels_Fill
I am trying to access my check boxes as follows:
1 Dim dkLevel As DataKey
2 For Each gvrLevel As GridViewRow In gvLevels.Rows
3 Dim cbLevelSubscription As CheckBox =
CType(gvrLevel.FindControl("cbLevelSubscription"), CheckBox)
4 If cbLevelSubscription.Checked Then
5 dkLevel = gvLevels.DataKeys(gvrLevel.RowIndex)
6 Response.Write(dkLevel.Value)
7 End If
8 Next gvrLevel
When I click the button which will run the code that loops through
each check box I get the following error:
"Item has already been added.Key in dictionay: "LevelName" Key been
added: "LevelName"
I removed:
5 .DataKeyNames = New String() {"LevelName"}
And I stopped having this error but still does not work.
Any idea?
Thanks,
Miguel