Adding Rows dynamically to GridView in ASP.NET - asp.net

I use the following code for adding Rows dynamically to GridView in ASP.NET.
ViewState("CourseFeesDetails") = dsGetStudentCourseFeesDetail
If ViewState("CourseFeesDetails") IsNot Nothing Then
Dim dtCourseTable As New DataTable
dtCourseTable = CType(ViewState("CourseFeesDetails"), DataTable)
Dim drCurrentRow As DataRow = Nothing
Dim txtFeesAmt As New System.Web.UI.WebControls.TextBox
If dtCourseTable.Rows.Count > 0 Then
For i = 1 To nNoOfInstallments
drCurrentRow = dtCourseTable.NewRow()
i = i + 1
dtCourseTable.Rows.Add(drCurrentRow)
Next
ViewState("CourseFeesDetails") = dtCourseTable
gvCourseFeeDisplay.DataSource = dtCourseTable
gvCourseFeeDisplay.DataBind()
End If
End If
It does add the number of rows, but adds 1 extra. What should I do?

Related

get values of dynamic checkboxes

I am dynamically creating checkboxes in VB.Net and an .aspx page, based on values in my db. I'm placing them in a two column table for ease of alignment. this part works fine.
Private Async Function InitForEditAsync() As Task
Dim docList = Await GetLoanApplicationConfigurationDocs()
Dim row = New HtmlTableRow()
Dim cell = New HtmlTableCell()
Dim i = 0
For Each doc In docList
Dim chkBox = New HtmlInputCheckBox()
Dim lbl = New Label()
Dim remainder = i Mod 2
chkBox.ID = "chkDocId" + doc.Id.ToString
lbl.Text = doc.DisplayName
cell.Controls.Add(chkBox)
cell.Controls.Add(lbl)
row.Cells.Add(cell)
cell = New HtmlTableCell()
If remainder <> 0 OrElse i = docList.Count() - 1 Then
tblEdit.Rows.Add(row)
row = New HtmlTableRow()
End If
i += 1
Next
End Function
Now I need to retrieve the values without knowing the id's but am not having any luck. I tried this:
For Each chkBox As HtmlInputCheckBox In pnlEdit.Controls.OfType(Of HtmlInputCheckBox)
but the checkboxes are not returned in the list of controls. The table is, but there are no rows in the table object when I explored it in the control collection and when I tried this:
For Each row As HtmlTableRow In tblEdit.Rows.OfType(Of HtmlTableRow)
If it will help, here is a Snip of the UI and the HTML that is created:
Any suggestions are appreciated. Thanks in advance.
Based on some ideas I got from another site, I'm going to rewrite this using the asp:CheckBoxList. apparently it binds like a datagrid and you can enumerate through it. Seems like what i need.
UPDATE: Everything I posted to start was resolved with five lines of code! "cblDocList is my asp CheckboxList and docList is my ienumerable of objects.
cblDocList.RepeatColumns = 2
cblDocList.DataSource = docList
cblDocList.DataTextField = "DisplayName"
cblDocList.DataValueField = "Id"
cblDocList.DataBind()
It’s something you can do through a loop for each row and each cell or using Linq to have only cells that have controls of type HtmlInputCheckBox inside.
I have simplified your code to be able run that here also shows you an example to achieve your task. Obviously you must change following your exigences .
Hope I well understood :)
Dim tblEdit As New HtmlTable
For k As Integer = 0 To 10
Dim cell = New HtmlTableCell()
Dim row = New HtmlTableRow()
Dim chkBox = New HtmlInputCheckBox()
Dim lbl = New Label()
Dim remainder = k Mod 2
chkBox.ID = "chkDocId_" + k.ToString
chkBox.Checked = remainder = 0
lbl.Text = "Text indicator of CheckBox nr:" + k.ToString
cell.Controls.Add(chkBox)
cell.Controls.Add(lbl)
row.Cells.Add(cell)
cell = New HtmlTableCell()
tblEdit.Rows.Add(row)
Next
Dim checkBoxes As IEnumerable(Of HtmlInputCheckBox) =
(From mRow In tblEdit.Rows).Select(Function(mr)
Dim cb = (From cc In CType(mr, HtmlTableRow).Cells
Where CType(cc, HtmlTableCell).Controls.OfType(Of HtmlInputCheckBox).Count > 0
Select CType(cc, HtmlTableCell).Controls.OfType(Of HtmlInputCheckBox)()(0)).FirstOrDefault
Return CType(cb, HtmlInputCheckBox)
End Function).ToList
For Each checkBox In checkBoxes
Debug.WriteLine("CheckBox ID: {0} Checked: {1} ", checkBox.ID, checkBox.Checked)
Next

Add new Column to existing Dataview

I wanted to add New Column to the Existing Dataview as shown in below image. the new column should have value 1 or 0 base upon the value of DocumentType,HouseOrigin and StorageDate. basically i am checking if Document type and HouseOrigin is same then put 0 for the latest storageDate and put 1 for other row.
Hope below image will help to understand.
How i will do it ? TIA.
this is how i am get it. Hope that helps !!
If DetailsView.Count > 0 Then
Dim col As DataColumn = New DataColumn("DisableFlag", GetType(Boolean))
col.DefaultValue = True
DetailsView.Table.Columns.Add(New DataColumn("DisableFlag", GetType(Boolean)))
Dim LocalDV As DataView = DetailsView
Dim result As List(Of DataTable) = DetailsView.Table.AsEnumerable.GroupBy(Function(row) row.Field(Of String)("DocumentType")).[Select](Function(g) g.CopyToDataTable()).ToList()
For Each rowView As DataRowView In DetailsView
Dim row As DataRow = rowView.Row
For Each DivByDocTypedt As DataTable In result
Dim maxDate As Object = DivByDocTypedt.Compute("MAX(StorageDate)", Nothing)
For Each DivByDocTyperow As DataRow In DivByDocTypedt.Rows
If (DivByDocTyperow.Item("Guid") = row.Item("Guid")) Then
If (maxDate = row.Item("StorageDate")) Then
row.SetField("DisableFlag", False)
Else
row.SetField("DisableFlag", True)
End If
End If
Next '' DivByDocTyperow
Next ''DivByDocTypedt
Next ''rowView of DetailsView

Building a Grdivew with Web Service Data

Hi i am trying to build a Gridview Based on my Web Service Data.
I am not not getting any errors when I compile the code, however the gridview does not display. Not sure if this is because I am building the gridview wrong or the data is not being pulled correctly. The web service does work because I have tested it.
Here is the code i am using to bind the gridview:
Private Sub BindGrid()
Dim objVen As New ISISVendor.VendorInterfaceClient
Dim LoginResp As New ISISVendor.LoginResponse
Dim POResp As New ISISVendor.PoSummaryResponse
Dim pod As New ISISVendor.PoDetailResponse
Dim dt As New DataTable
dt.Columns.Add("Status")
dt.Columns.Add("Sender")
dt.Columns.Add("PO Number")
dt.Columns.Add("Date")
dt.Columns.Add("Action")
gvv.DataSource = dt
POResp = objVen.GetOpenPos("3274")
Dim dr As DataRow
If POResp.Pos.Count > 0 Then
For j As Integer = 0 To POResp.Pos.Count - 1
dr = dt.NewRow
dr.Item("Status") = POResp.Pos(j).Status
dr.Item("Sender") = "COMPANY TEST"
dr.Item("PO Number") = POResp.Pos(j).PoNumber
dr.Item("Date") = POResp.Pos(j).PoDate
dr.Item("Action") = ""
Next
End If
gvv.DataBind()
End Sub
Seem you are not adding the datarow to the data table
Please try as below
POResp = objVen.GetOpenPos("3274")
Dim dr As DataRow
If POResp.Pos.Count > 0 Then
For j As Integer = 0 To POResp.Pos.Count - 1
dr = dt.NewRow
dr.Item("Status") = POResp.Pos(j).Status
dr.Item("Sender") = "COMPANY TEST"
dr.Item("PO Number") = POResp.Pos(j).PoNumber
dr.Item("Date") = POResp.Pos(j).PoDate
dr.Item("Action") = ""
dt.Rows.Add(dr)
Next
End If
gvv.DataSource = dt
gvv.DataBind()

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

ASP.NET DataSet loses data?

The following code adds, dynamically, tabs to a tab control. In each row of datatable, it calls a function CarregaAvaliacoes that adds some controls to each tab. Dataset dtGruposCompetencias has 4 rows. However, after first row (after calling CarregaAvaliacoes), it looses data (IndexOutOfRangeException). Any ideias?
EDIT: I have debugged it, followed each step. After finishing CarregaAvaliacoes first time, dataset has no longer data. Don't understand why.
Thanks!
Dim dtGruposCompetencias As New DataTable
Dim nivel As Integer = 4
dtGruposCompetencias = dal.CarregaAvaliacoesPorNivel(nivel)
lblTemp.Text = dtGruposCompetencias.Rows.Count
If dtGruposCompetencias.Rows.Count > 0 Then
Dim tb As AjaxControlToolkit.TabPanel
For i As Integer = 0 To dtGruposCompetencias.Rows.Count - 1
tb = New AjaxControlToolkit.TabPanel
tb.HeaderText = dtGruposCompetencias.Rows(i)("designacao").ToString()
Dim grupo As String = dtGruposCompetencias.Rows(i)("idGrupoCompetencia").ToString()
CarregaAvaliacoes(tb, grupo)
Next
tabAvaliacoes.ActiveTabIndex = 0
Else
lblSemAvaliacoes.Visible = True
End If
Public Sub CarregaAvaliacoes(tab As AjaxControlToolkit.TabPanel, idGrupoCompetencia As String)
Dim dtAtributos As New DataTable
dtAtributos = dal.CarregaAtributosAvaliacao(idGrupoCompetencia)
If dtAtributos.Rows.Count > 0 Then
Dim tdCompetencia As TableCell, tdAtributo As TableCell
Dim tr As TableRow
Dim tblAvaliacao As New Table
For i As Integer = 0 To dtAtributos.Rows.Count - 1
tdCompetencia = New TableCell
tdCompetencia.Controls.Add(fRetTexto(dtAtributos.Rows(i)("competencia").ToString()))
tdAtributo = New TableCell
tdAtributo.Controls.Add(fRetTexto(dtAtributos.Rows(i)("atributo").ToString()))
tr = New TableRow
tr.Cells.Add(tdCompetencia)
tr.Cells.Add(tdAtributo)
tblAvaliacao = New Table
tblAvaliacao.Rows.Add(tr)
Next
tab.Controls.Add(tblAvaliacao)
Else
Dim lblSemRegistos As New Label
lblSemRegistos.Text = "Sem dados para avaliação."
tab.Controls.Add(lblSemAvaliacoes)
End If
End Sub

Resources