gridview loop issue - asp.net

I have a gridview with 4 columns, three of them are template fields where the user can enter the information needed and click a button to submit it to the database. I have 2 issues:
When I click my button to add a second row... the data in the first row posts
disappears. I want to be able to add the row without the first row of information
disappearing for multiple record entry situations. My code follows:
Private Sub SetInitialRow()
Dim dts As New DataTable()
Dim drs As DataRow = Nothing
dts.Columns.Add(New DataColumn("Approval Date", GetType(String)))
dts.Columns.Add(New DataColumn("Total Amount", GetType(String)))
dts.Columns.Add(New DataColumn("Comments", GetType(String)))
dts.Columns.Add(New DataColumn("Initials", GetType(String)))
drs = dts.NewRow()
drs("Approval Date") = String.Empty
drs("Total Amount") = String.Empty
drs("Comments") = String.Empty
drs("Initials") = String.Empty
dts.Rows.Add(drs)
ViewState("CurrentTable") = dts
gvOLIAdj.DataSource = dts
gvOLIAdj.DataBind()
End Sub
Private Sub AddNewRowToGrid()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
'extract the TextBox values
Dim box1 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(1).FindControl("txtAdjAppr"), TextBox)
Dim box2 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(2).FindControl("txtAdjAmt"), TextBox)
Dim box3 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(3).FindControl("txtCmmts"), TextBox)
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("Approval Date") = box1.Text
dtCurrentTable.Rows(i - 1)("Total Amount") = box2.Text
dtCurrentTable.Rows(i - 1)("Comments") = box3.Text
'dtCurrentTable.Rows(i - 1)("Initials") =
rowIndex += 1
Next
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
gvOLIAdj.DataSource = dtCurrentTable
gvOLIAdj.DataBind()
End If
Else
Response.Write("ViewState is null")
End If
'Set Previous Data on Postbacks
SetPreviousData()
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dats As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
If dats.Rows.Count > 0 Then
For i As Integer = 0 To dats.Rows.Count - 1
Dim box1 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(1).FindControl("txtAdjAppr"), TextBox)
Dim box2 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(2).FindControl("txtAdjAmt"), TextBox)
Dim box3 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(3).FindControl("txtCmmts"), TextBox)
box1.Text = dats.Rows(i)("Approval Date").ToString()
box2.Text = dats.Rows(i)("Total Amount").ToString()
box3.Text = dats.Rows(i)("Comments").ToString()
rowIndex += 1
Next
End If
End If
End Sub
Protected Sub btnAddNewRow_Click(sender As Object, e As EventArgs) Handles btnAddNewRow.Click
AddNewRowToGrid()
End Sub
When I try to write a loop that will loop through each of the template fields to grab
the data and put it into my database it doesn't recognize that I have data in the
template fields? This is what I have tried thus far to no avail...
Protected Sub btn_Update_Click(sender As Object, e As EventArgs) Handles btn_Update.Click
For Each row As GridViewRow In gvOLIAdj.Rows
For Each gv As GridViewRow In gvOLIAdj.Rows
Dim appDt As String = (Rows(rowIndex).Cells(1).FindControl("txtAdjAppr")), TextBox)
Dim approvalDt As String = CType(gv.FindControl("txtAdjAppr"), TextBox).Text
Dim totalAmt As String = CType(gv.FindControl("txtAdjAmt"), TextBox).Text
Dim comments As String = CType(gv.FindControl("txtcmmts"), TextBox).Text
Dim intitials As String = DirectCast(gv.FindControl("total"), TextBox).Text
Next
End Sub

In your AddNewRowToGrid method you are doing the following:
dtCurrentTable.Rows(i - 1)("Total Amount") = box2.Text
dtCurrentTable.Rows(i - 1)("Comments") = box3.Text
What's wrong here is the updating of the row in dtCurrentTable.Rows(i - 1). You are updating the values in the row above that which you wish to update.

Related

How to delete a row in gridview that was added dynamically using asp.net and vb.net

I have a gridview. the rows are added dynamically
Protected Sub btnAddCustomer_Click(sender As Object, e As EventArgs) Handles btnAddCustomer.Click
Dim dtSource As New DataTable
dtSource = CType(ViewState("dtSource"), DataTable)
Dim dr As DataRow = dtSource.NewRow()
dr("Id") = DropDownList3.SelectedIndex
dr("Name") = DropDownList3.SelectedItem
dtSource.Rows.Add(dr)
ViewState("dtSource") = dtSource
GridView1.DataSource = dtSource
GridView1.DataBind()
GridView1.HeaderRow.Cells(1).Text = "ID"
GridView1.HeaderRow.Cells(2).Text = "NAME"
End Sub
Each row has a delete image button, and when it is clicked it fires the Code:
Protected Sub GridVeiw1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
If ViewState("dtSource") IsNot Nothing Then
Dim dtSource As New DataTable
Dim drCurrentRow As DataRow = Nothing
Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
dtSource = DirectCast(ViewState("dtSource"), DataTable)
If dtSource.Rows.Count > 0 Then
dtSource.Rows.RemoveAt(rowIndex)
drCurrentRow = dtSource.NewRow()
dtSource.AcceptChanges()
ViewState("dtSource") = dtSource
GridView1.DataSource = dtSource
GridView1.DataBind()
End If
End If
End Sub
The delete procedure is not working!!!
Any suggessions Please?
I read the question regarding How to delete a row in GV, but it is not helping because am not deleting from GV but from DataTable that contains the data for the GV

ASP.Net Looping Through Gridview After adding Group Row

In a GridView I am adding Grouping rows to the underlying table in the RowDataBound, but when I ApplyChanges and loop through each GridViewRow not all rows can be tested for TextBox value etc, the number of rows is reduced by the number of grouping rows inserted. Although it does loop through the grouping rows which I ignore, and misses the last n rows.
Private tmpSiteName As String = ""
Private Sub gvVehicles_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvVehicles.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
If tmpSiteName <> drv("lcname").ToString() Then
tmpSiteName = drv("lcname").ToString()
Dim tbl As Table = TryCast(e.Row.Parent, Table)
If tbl IsNot Nothing Then
Dim row As New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim cell As New TableCell()
' Span the row across all of the columns in the Gridview
cell.ColumnSpan = Me.gvVehicles.Columns.Count
cell.Width = Unit.Percentage(100)
'cell.Style.Add("font-weight", "bold")
cell.Style.Add("background-color", "#f5f5f5")
cell.Style.Add("color", "#000000")
Dim span As New HtmlGenericControl("span")
span.InnerHtml = tmpSiteName
cell.Controls.Add(span)
row.Cells.Add(cell)
tbl.Rows.AddAt(tbl.Rows.Count - 1, row)
End If
End If
End If
End Sub
Private Sub ApplyChanges()
Dim AuctionDB As fleet.AuctionDB = New fleet.AuctionDB
Dim OldFleetNo As String = ""
Dim NewFleetNo As String = ""
Dim OldReserve As Integer = 0
Dim NewReserve As Integer = 0
Dim MTA As Long = 0
Dim Sitekey As String = ""
Dim vcaudit As Long = 0
Dim i As Integer = gvVehicles.Rows.Count
Try
For Each DRow As GridViewRow In gvVehicles.Rows
Sitekey = TryCast(DRow.FindControl("hiddenSitekey"), HiddenField).Value
MTA = Val(TryCast(DRow.FindControl("hiddenMTA"), HiddenField).Value)
strChange = ""
' The Grouping rows throw up funny results, but we can catch MTA = 0
If MTA > 0 Then
OldFleetNo = TryCast(DRow.FindControl("hiddenFleetNo"), HiddenField).Value
NewFleetNo = TryCast(DRow.FindControl("txtFleetNo"), TextBox).Text
If NewFleetNo <> OldFleetNo Then
vcaudit = AuctionDB.FleetUpdates(Sitekey, MTA, "FLEET NO", OldReserve, NewFleetNo, Session("user_name"))
updateCount += 1
strChange = "Fleet No. changed from " & OldFleetNo & " to " & NewFleetNo & "<br /><br />"
End If
End If
Next DRow
Catch ex As Exception
ShowMessage("Error: " & ex.Message, "alert-danger")
End Try
End Sub

Get all selected values of CheckBoxList in VB.NET

I've used ASP's CheckBoxList control. Now what I want is to get the all selected values in VB code.
HTML
<asp:CheckBoxList ID="chkbxlst_Users" runat="server" RepeatColumns="2" RepeatDirection="Vertical" RepeatLayout="Table"></asp:CheckBoxList>
VB
Protected Sub btnSaveSetProject_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveSetProject.Click
Dim ds_selectedProjects As New DataSet
Dim eStr As String = String.Empty
Try
Catch ex As Exception
Me.ShowErrorMessage(ex.Message, "...btnSaveSetProject")
End Try
End Sub
On this Save button's click I want to get all the selected items' value and text in dataset.
try this..
For Each li As ListItem In chkbxlst_Users.Items
If li.Selected Then
// add item data into your dataset
Else
// do whatever you need
End If
End If
Next
You can try following code:
Protected Sub btnSaveSetProject_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveSetProject.Click
Dim ds_selectedProjects As New DataSet
Dim dt = New DataTable()
Dim dcName = New DataColumn("Name", GetType(String))
dt.Columns.Add(dcName)
Dim eStr As String = String.Empty
Try
For Each checkBox As CheckBox In chkbxlst_Users.Items
If (checkBox.Checked = True) Then
Dim dr As DataRow = dt.NewRow()
dr("ID") = checkBox.Text
dt.Rows.Add(dr)
End If
Next
ds_selectedProjects.Tables.Add(dt)
Catch ex As Exception
'Me.ShowErrorMessage(ex.Message, "...btnSaveSetProject")
End Try
End Sub
Try this code
Dim ds_selectedProjects As New DataSet
Dim dt_selectedProjects As New DataTable
dt_selectedProjects.Columns.Add("Value")
dt_selectedProjects.Columns.Add("Text")
Dim dr As DataRow
For i = 0 To chkbxlst_Users.Items.Count - 1
If chkbxlst_Users.Items(i).Selected Then
dr = dt_selectedProjects.NewRow()
dr("Value") = Val(chkbxlst_Users.Items(i).Value)
dr("Text") = chkbxlst_Users.Items(i).Text
dt_selectedProjects.Rows.Add(dr)
End If
Next
ds_selectedProjects.Tables.Add(dt_selectedProjects)
Try this
Dim str As [String] = ""
For i As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
If str = "" Then
str = "'" + CheckBoxList1.Items(i).Value + "'"
Else
str += "," + "'" + CheckBoxList1.Items(i).Value + "'"
End If
End If
Next
'display in a textbox or lable with ID txtmsg
txtmsg.Text = str

I need to check a checkbox based on the result of a dataset. If the result is 1 then checkbox should be checked and not checked if it's a 0

When I delete a row from the gridview, it doesn't delete the row that is chosen to be deleted and it deletes the last row of the gridview. It also clears the user input of the row that is now the last row, but all of the other rows maintain their inputs. Any help on this is greatly appreciated.
Here is my RowDeleting sub:
Protected Sub gvPDetails_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvPDetails.RowDeleting
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim delRowIndex As Integer = Convert.ToInt32(e.RowIndex)
Dim delRow As DataRow = dt.Rows(delRowIndex)
dt.Rows.Remove(delRow)
SetRowData()
ViewState("CurrentTable") = dt
End If
End Sub
Here is the SetRowData()
Private Sub SetRowData()
Dim rowIndex As Integer = 0
Dim arrl1 As New ArrayList()
Dim arrl2 As New ArrayList()
Dim arrl3 As New ArrayList()
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 0 To dtCurrentTable.Rows.Count - 1
//extract the TextBox values
Dim box1 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtStore"), TextBox)
//extract the DropDownList Selected Items
Dim ddl1 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlStatus"), DropDownList)
Dim ddl2 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlReason"), DropDownList)
Dim ddl3 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlPldDetail"), DropDownList)
Dim box2 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtInfo"), TextBox)
//Update the DataRow with the DDL Selected Items
dtCurrentTable.Rows(rowIndex)("Store") = box1.Text
dtCurrentTable.Rows(rowIndex)("Status") = ddl1.SelectedItem.Text
myDD1 = ddl1.SelectedItem.Text
dtCurrentTable.Rows(rowIndex)("Reason") = ddl2.SelectedItem.Text
myDD2 = ddl2.SelectedItem.Text
If ddl3 IsNot Nothing And ddl3.Enabled = True Then
dtCurrentTable.Rows(rowIndex)("Detail") = ddl3.SelectedItem.Text
End If
myDD3 = ddl3.SelectedItem.Text
dtCurrentTable.Rows(rowIndex)("Information") = box2.Text
rowIndex += 1
Dim MemErrList As New ArrayList()
Dim index As Integer = -1
Dim row As GridViewRow = gvPDetails.Rows(i)
index = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
Dim result As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("ckbMemErr"), CheckBox).Checked
//Check in the Session
If Session("CHECKED_ITEMS") IsNot Nothing Then
MemErrList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
End If
If result Then
If Not MemErrList.Contains(index) Then
MemErrList.Add(index)
End If
Else
MemErrList.Remove(index)
End If
//Next
If MemErrList IsNot Nothing AndAlso MemErrList.Count > 0 Then
Session("CHECKED_ITEMS") = MemErrList
End If
//----SLS File Val Checkbox Array Builder-----------------------------
Dim DetailList As New ArrayList()
Dim index1 As Integer = -1
Dim row1 As GridViewRow = gvPDetails.Rows(i)
index1 = CInt(gvPDetails.DataKeys(row1.RowIndex).Value)
Dim result1 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbSLSFileVal"), CheckBox).Checked
//Check in the Session
If Session("DETAIL1_ITEMS") IsNot Nothing Then
DetailList = DirectCast(Session("DETAIL1_ITEMS"), ArrayList)
End If
If result1 Then
If Not DetailList.Contains(index1) Then
DetailList.Add(index1)
End If
Else
DetailList.Remove(index1)
End If
//Next
If DetailList IsNot Nothing AndAlso DetailList.Count > 0 Then
Session("DETAIL1_ITEMS") = DetailList
End If
//----Router Checkbox Array Builder-----------------------------
Dim DetailList2 As New ArrayList()
Dim index2 As Integer = -1
Dim row2 As GridViewRow = gvPDetails.Rows(i)
index2 = CInt(gvPDetails.DataKeys(row2.RowIndex).Value)
Dim result2 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbRouter"), CheckBox).Checked
//Check in the Session
If Session("DETAIL2_ITEMS") IsNot Nothing Then
DetailList2 = DirectCast(Session("DETAIL2_ITEMS"), ArrayList)
End If
If result2 Then
If Not DetailList2.Contains(index2) Then
DetailList2.Add(index2)
End If
Else
DetailList2.Remove(index2)
End If
//Next
If DetailList2 IsNot Nothing AndAlso DetailList2.Count > 0 Then
Session("DETAIL2_ITEMS") = DetailList2
End If
//----Register1 Checkbox Array Builder-----------------------------
Dim DetailList3 As New ArrayList()
Dim index3 As Integer = -1
Dim row3 As GridViewRow = gvPDetails.Rows(i)
index3 = CInt(gvPDetails.DataKeys(row3.RowIndex).Value)
Dim result3 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbRegister1"), CheckBox).Checked
//Check in the Session
If Session("DETAIL3_ITEMS") IsNot Nothing Then
DetailList3 = DirectCast(Session("DETAIL3_ITEMS"), ArrayList)
End If
If result3 Then
If Not DetailList3.Contains(index3) Then
DetailList3.Add(index3)
End If
Else
DetailList3.Remove(index3)
End If
//Next
If DetailList3 IsNot Nothing AndAlso DetailList3.Count > 0 Then
Session("DETAIL3_ITEMS") = DetailList3
End If
//----Register2 Checkbox Array Builder-----------------------------
Dim DetailList4 As New ArrayList()
Dim index4 As Integer = -1
Dim row4 As GridViewRow = gvPDetails.Rows(i)
index4 = CInt(gvPDetails.DataKeys(row4.RowIndex).Value)
Dim result4 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbRegister2"), CheckBox).Checked
//Check in the Session
If Session("DETAIL4_ITEMS") IsNot Nothing Then
DetailList4 = DirectCast(Session("DETAIL4_ITEMS"), ArrayList)
End If
If result4 Then
If Not DetailList4.Contains(index4) Then
DetailList4.Add(index4)
End If
Else
DetailList4.Remove(index4)
End If
//Next
If DetailList4 IsNot Nothing AndAlso DetailList4.Count > 0 Then
Session("DETAIL4_ITEMS") = DetailList4
End If
arrl1.Add(myDD1)
arrl2.Add(myDD2)
arrl3.Add(myDD3)
Next
//dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
//Rebind the Grid with the current data to reflect changes
gvPDetails.DataSource = dtCurrentTable
gvPDetails.DataBind()
End If
Else
End If
//Set Previous Data on Postbacks
SetPrevDataOnDelete(arrl1, arrl2, arrl3)
End Sub
And here is the SetPrevDataOnDelete(arrl1, arrl2, arrl3)
Private Sub SetPrevDataOnDelete(ByVal arrl1 As ArrayList, ByVal arrl2 As ArrayList, ByVal arrl3 As ArrayList)
Dim rowIndex As Integer = 0
myGridViewRow = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 0 Then
Dim arr1 As ArrayList = arrl1
Dim arr2 As ArrayList = arrl2
Dim arr3 As ArrayList = arrl3
For i As Integer = 0 To dt.Rows.Count - 1
Dim box1 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtStore"), TextBox)
Dim ddl1 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlStatus"), DropDownList)
Dim ddl2 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlReason"), DropDownList)
Dim ddl3 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlPldDetail"), DropDownList)
Dim box2 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtInfo"), TextBox)
Dim cbResult As CheckBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ckbMemErr"), CheckBox)
//Fill the DropDownList with Data
myGridViewRow = i
//FillDropDownList(ddl1)
FillDDL2OnDel(arrl1, rowIndex)
FillDDL3OnDel(arrl1, arrl2, rowIndex)
If i < dt.Rows.Count - 1 Then
//Assign the value from DataTable to the TextBox
//gvPDetails.Rows(i).Cells(0).Text = Convert.ToString(i + 1)
box1.Text = dt.Rows(rowIndex)("Store").ToString()
box2.Text = dt.Rows(rowIndex)("Information").ToString()
ddl1.Items.FindByText(dt.Rows(rowIndex)("Status").ToString()).Selected = True
If ddl2 Is Nothing Then
ElseIf ddl2 Is Nothing Then
ElseIf ddl2 IsNot Nothing And ddl2.Enabled = True Then
ddl2.Items.FindByText(dt.Rows(rowIndex)("Reason").ToString()).Selected = True
End If
If ddl3 IsNot Nothing And ddl3.Enabled = True Then
ddl3.Items.FindByText(dt.Rows(rowIndex)("Detail").ToString()).Selected = True
End If
Dim MemErrList As ArrayList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
If MemErrList IsNot Nothing AndAlso MemErrList.Count > 0 Then
Dim row As GridViewRow = gvPDetails.Rows(i)
Dim index As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
If MemErrList.Contains(index) Then
Dim myCheckBox As CheckBox = DirectCast(row.FindControl("ckbMemErr"), CheckBox)
myCheckBox.Checked = True
End If
//Next
End If
Dim DetailList As ArrayList = DirectCast(Session("DETAIL1_ITEMS"), ArrayList)
If DetailList IsNot Nothing AndAlso DetailList.Count > 0 Then
Dim row As GridViewRow = gvPDetails.Rows(i)
Dim index1 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
If DetailList.Contains(index1) Then
Dim myCheckBox1 As CheckBox = DirectCast(row.FindControl("cbSLSFileVal"), CheckBox)
myCheckBox1.Checked = True
End If
//Next
End If
Dim DetailList2 As ArrayList = DirectCast(Session("DETAIL2_ITEMS"), ArrayList)
If DetailList2 IsNot Nothing AndAlso DetailList2.Count > 0 Then
Dim row As GridViewRow = gvPDetails.Rows(i)
Dim index2 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
If DetailList2.Contains(index2) Then
Dim myCheckBox2 As CheckBox = DirectCast(row.FindControl("cbRouter"), CheckBox)
myCheckBox2.Checked = True
End If
//Next
End If
Dim DetailList3 As ArrayList = DirectCast(Session("DETAIL3_ITEMS"), ArrayList)
If DetailList3 IsNot Nothing AndAlso DetailList3.Count > 0 Then
Dim row As GridViewRow = gvPDetails.Rows(i)
Dim index3 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
If DetailList3.Contains(index3) Then
Dim myCheckBox3 As CheckBox = DirectCast(row.FindControl("cbRegister1"), CheckBox)
myCheckBox3.Checked = True
End If
//Next
End If
Dim DetailList4 As ArrayList = DirectCast(Session("DETAIL4_ITEMS"), ArrayList)
If DetailList4 IsNot Nothing AndAlso DetailList4.Count > 0 Then
Dim row As GridViewRow = gvPDetails.Rows(i)
Dim index4 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
If DetailList4.Contains(index4) Then
Dim myCheckBox4 As CheckBox = DirectCast(row.FindControl("cbRegister2"), CheckBox)
myCheckBox4.Checked = True
End If
//Next
End If
End If
rowIndex += 1
Next
End If
End If
End Sub
Thanks again for any help.
Is e.RowIndex returning the correct row number that you are expecting it to?

Textbox values disappear on postback

I have an asp.net page with a button that adds an additional row to a gridview for input to the database. The gridview consists of 3 textboxes(template fields), when I add a row the information already entered somehow disappears on the postback. I want the button to add additional rows without disspelling the data in the other rows, until I hit the submit button. Here's my code
Private Sub AddNewRowToGrid()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
'extract the TextBox values
Dim box1 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(1).FindControl("txtAdjAppr"), TextBox)
Dim box2 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(2).FindControl("txtAdjAmt"), TextBox)
Dim box3 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(3).FindControl("txtCmmts"), TextBox)
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("Approval Date") = box1.ToString
dtCurrentTable.Rows(i - 1)("Total Amount") = box2.ToString
dtCurrentTable.Rows(i - 1)("Comments") = box3.ToString
'dtCurrentTable.Rows(i - 1)("Initials") =
rowIndex += 1
Next
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
gvOLIAdj.DataSource = dtCurrentTable
gvOLIAdj.DataBind()
End If
Else
Response.Write("ViewState is null")
End If
'Set Previous Data on Postbacks
'SetPreviousData()
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dats As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
If dats.Rows.Count > 0 Then
For i As Integer = 0 To dats.Rows.Count - 1
Dim box1 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(1).FindControl("txtAdjAppr"), TextBox)
Dim box2 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(2).FindControl("txtAdjAmt"), TextBox)
Dim box3 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(3).FindControl("txtCmmts"), TextBox)
box1.Text = dats.Rows(i)("Approval Date").ToString()
box2.Text = dats.Rows(i)("Total Amount").ToString()
box3.Text = dats.Rows(i)("Comments").ToString()
rowIndex += 1
Next
End If
End If
End Sub
I can think of two reasons why this may be happening:
When you create controls dynamically it is VERY difficult to
retrieve the values in the code behind after postback
Your controls have the read-only attribute set in the raw HTML
You may want to try old-fashioned methods such as Request.Form http://msdn.microsoft.com/en-us/library/ms525985%28v=vs.90%29.aspx

Resources