Add new Column to existing Dataview - asp.net

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

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

Adding Rows dynamically to GridView in 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?

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

gridview PageIndexChanging issue

I am trying to to loop through a dataset's value through the rows in a gridview and color in the text if that row matches.
The code below works however whenever I change the page through the PageIndexChanging and this function is ran again, the coloring doesn't work anymore. It still loops through the gridview if there is a match but the effects are not shown.
--variable initialization class instantiation--
--code to connect to db here--
mySQLCommand.CommandText = "SELECT ..."
mySQLAdapter = New SqlDataAdapter(mySQLCommand)
mySQLAdapter.Fill(myDataset)
Me.MainPageGridView.DataSource = myDataset
Me.MainPageGridView.DataBind()
mySQLCommand.CommandText = "SELECT ... The ID's to be matched"
mySQLAdapter = New SqlDataAdapter(mySQLCommand)
mySQLAdapter.Fill(myDatasetNew)
Me.MainPageGridView.DataSource = myDatasetNew
For Each dataRow In myDataset.Tables(0).Rows
thisID = dataRow("ID").ToString
For Each gvRow In Me.MainPageGridView.Rows
If gvRow.Cells(2).Text = thisID Then
For column = 0 To 14 Step 1
gvRow.Cells(column).ForeColor = Drawing.Color.RosyBrown
Next
Exit For
End If
Next
Next
Why don't you use MainPageGridView_RowDataBound event to match the id? I have re-factored your original code to something like below, please check and let me know if it works:
'In DataBind or some other method
'Load(myDataSet)
mySQLCommand.CommandText = "SELECT ..."
mySQLAdapter = New SqlDataAdapter(mySQLCommand)
mySQLAdapter.Fill(myDataset)
'Load myDatasetNew and bind it to grid
mySQLCommand.CommandText = "SELECT ... The ID's to be matched"
mySQLAdapter = New SqlDataAdapter(mySQLCommand)
mySQLAdapter.Fill(myDatasetNew)
Me.MainPageGridView.DataSource = myDatasetNew
Me.MainPageGridView.DataBind()
and perform id matching in
Protected Sub MainPageGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MainPageGridView.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim id As String = DataBinder.Eval(e.Row.DataItem, "ID") 'The name of ID column in "myDatasetNew"
Dim dv As System.Data.DataView = myDataset.Tables(0).DefaultView
dv.RowFilter = "ID = " & id
If dv.Count > 0 Then 'id matches
'Change foreclor of entire row
e.Row.ForeColor = Drawing.Color.RosyBrown
End If
End If
End Sub
You really need to do your data comparison in the GridView.RowDataBound event.

How to correctly filter a datatable (datatable.select)

Dim dt As New DataTable
Dim da As New SqlDataAdapter(s, c)
c.Open()
If Not IsNothing(da) Then
da.Fill(dt)
dt.Select("GroupingID = 0")
End If
GridView1.DataSource = dt
GridView1.DataBind()
c.Close()
When I call da.fill I am inserting all records from my query. I was then hoping to filter them to display only those where the GroupingID is equal to 0. When I run the above code. I am presented with all the data, the filter did not work. Please can you tell me how to get this working correctly. Thanks.
dt.Select() returns an array of DataRows.
Why don't you use a DataView?
DataView dv = new DataView(dt);
dv.RowFilter = "GroupingID = 0";
GridView1.DataSource = dv;
Junt in case... Think you've got a small typo in your VB.NET code. It should be dv.RowFilter instead of dv.RowStateFilter, so:
Dim dt As New DataTable
Dim dv As New DataView(dt)
dv.RowFilter = "GroupingID = 0"
DataGridView1.DataSource = dv
The accepted answer is correct, though it should have been given in vb.net to better benefit the one that asked the question. Here it is in VB.Net:
Dim dt As New DataTable:Dim dv As New DataView(dt):dv.RowStateFilter = "GroupingID = 0":DataGridView1.DataSource = dv

Resources