treeview conditional formatting - asp.net

I have a treeview populating from a data set like this
Dim PrSet As New DataSet()
If lblemail.Text.ToString().Equals("ali.saleem#shakarganj.com.pk") Then
PrSet = PDataset("select distinct PEND,to_char(BPV_DTE,'DD MON YYYY') BPV_DTE,BPV_DTE BPV_DTE1,COUNT from chq_dir order by 3 desc")
Else
PrSet = PDataset("select distinct PEND,to_char(BPV_DTE,'DD MON YYYY') BPV_DTE,BPV_DTE BPV_DTE1,COUNT from chq_dte order by 3 desc")
End If
TreeView2.Nodes.Clear()
For Each dr As DataRow In PrSet.Tables(0).Rows
Dim tnParent As New TreeNode()
tnParent.Text = dr("PEND").ToString()
tnParent.Value = dr("BPV_DTE")
tnParent.PopulateOnDemand = True
tnParent.SelectAction = TreeNodeSelectAction.Select
tnParent.ToolTip = tnParent.Text
tnParent.Expand()
TreeView2.Nodes.Add(tnParent)
If dr("COUNT").ToString() = "0" Then
TreeView2.Font.Bold= True
End If
Next dr
There is Column COUNT in which there is 0 in some dates.Problem is that i am trying to bold the treeview where COUNT is 0 but it is not working Any one can give me clear idea to do so.

I have solved My Question Like this
If Not dr("COUNT") = 0 Then
tnParent.Text = "<b>" & dr("PEND").ToString() & "</b>"
End If

I hope I've gethered what you meant by this question...
For Each tn As TreeNode In treeView2.Nodes
If tn.Text = "0" Then
Dim index As Integer
index = tn.Index
treeView2.Nodes(index).NodeFont = New Font(treeView2.Font, FontStyle.Bold)
End If
Next

Apply the style to tnparent and do this before adding the node to parent in treeview, and also from your code it looks like you are changing the fonts to italic not bold.

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

How to build the gridview from 2 tables and save co-ordinates mapping in 3'rd table in asp.net web form?

I am having Component table, Role table, and ComponentRoleMapping table in database. Please look at the image below. I want to populate the column Component names as first column filled with all rows and roles should be the first row of grid from Role table. All cell have to have check box. The matrix co-ordinate have to save that coordination in ComponentRoleMapping table by clicking on any check box. In short to populate many to many relationship among components and Roles I need this view. I am using asp.net web form and gridview control to populate this. how to populate single gridview from 2 tables / datatables ? Or any other control I can use here. Or any article I can follow for exact purpose?
you need a RoleComponent table which is a simple table of pk's that maps a Component to a Role. Sample table:
CREATE TABLE [dbo].[RoleComponent](
[role_pk] [int] NOT NULL,
[component_pk] [int] NOT NULL
) ON [PRIMARY]
A checkbox is initially checked if an entry in this table exists for a specific role and component.
When a user:
Checks a checkbox: insert a row for the given role_pk and component_pk
UnCheck a checkbox: delete a row for the given role_pk and component_pk
To get the grid of checkboxes as you have in your display you are going to need to return a pivot table of bit fields. Pivot tables are not simple in SqlServer. In fact they are downright annoying.
A Repeater might be a better option.
I am answering my own question, It might helpful to others. Rather to use Grid control I used Html table only.
Here Dt1 has Components, Dt2 has Roles and Dt3 used for to check active status. May be some dead code in this but, running perfect.
Private Sub GenerateTable(dt2 As DataTable, dt1 As DataTable, dt3 As DataTable)
Dim table As New Table()
Dim row As TableRow = Nothing
'Add the Headers
row = New TableRow()
'Empty
Dim headerCellEmpty As New TableHeaderCell()
headerCellEmpty.Text = ""
headerCellEmpty.Attributes("style") = "border:1px solid black;text-align:center;"
row.Cells.Add(headerCellEmpty)
For i As Integer = 0 To dt2.Rows.Count - 1
Dim headerCell As New TableHeaderCell()
headerCell.Text = dt2.Rows(i)(0).ToString()
headerCell.ID = dt2.Rows(i)(1).ToString()
'Check All Components
Dim chkAllComponent As New CheckBox()
chkAllComponent.ID = "allcomponents_" + dt2.Rows(i)(1).ToString()
chkAllComponent.Text = dt2.Rows(i)(0).ToString()
headerCell.Controls.Add(chkAllComponent)
headerCell.Attributes("style") = "border:1px solid black;text-align:center;width:15%;"
row.Cells.Add(headerCell)
Next
table.Rows.Add(row)
'Add the Column values
For i As Integer = 0 To dt1.Rows.Count - 1
row = New TableRow()
For j As Integer = 0 To dt1.Columns.Count - 2
Dim cell As New TableCell()
cell.Text = dt1.Rows(i)(j).ToString()
cell.ID = dt1.Rows(i)(j + 1).ToString()
'Check All Roles
Dim chkAllRoles As New CheckBox()
chkAllRoles.ID = "allroles_" + dt1.Rows(i)(j + 1).ToString()
chkAllRoles.Text = dt1.Rows(i)(j).ToString()
cell.Controls.Add(chkAllRoles)
row.Cells.Add(cell)
Next
' Add the TableRow to the Table
table.Rows.Add(row)
Next
If table.Rows.Count > 0 Then
If table.Rows(0).Cells.Count > 0 Then
'Add check boxes
Dim allColumnsCountinTable As Integer = table.Rows(0).Cells.Count
For Each tr As TableRow In table.Rows
Dim rowIndex As Integer = table.Rows.GetRowIndex(tr)
If rowIndex = 0 Then
Else
For f As Integer = 1 To allColumnsCountinTable - 1
Dim cell As New TableCell()
Dim roleid = table.Rows(rowIndex).Cells(0).ID
Dim roleId1 As Guid = New Guid(roleid)
Dim componentid = table.Rows(0).Cells(f).ID
Dim Check1 As New CheckBox()
Check1.ID = "chk_" + roleid + "_" + componentid
Dim configurations = From p In dt3.AsEnumerable() Select p
If (configurations.Any(Function(coffee) c.Field(Of Integer)("ComponentId") = componentid And c.Field(Of Guid)("RoleId") = roleId1)) Then
Dim isActive = configurations.Where(Function(coffee) c.Field(Of Integer)("ComponentId") = componentid And c.Field(Of Guid)("RoleId") = roleId1).FirstOrDefault()("IsActive")
If isActive = True Then
Check1.Checked = True
Else
Check1.Checked = False
End If
Else
Check1.Checked = False
End If
Check1.Attributes("Name") = "checkbox"
cell.Attributes("style") = "border:1px solid black;text-align:center;"
cell.Controls.Add(Check1)
tr.Cells.AddAt(f, cell)
Next
End If
tr.Attributes("style") = "color:red; border:1px solid black;"
Next
End If
End If
'Styling
Panel1.Controls.Add(table)
End Sub

NPOI set cell style "HSSFFont.BOLDWEIGHT_BOLD" is not working

I'm using NPOI to output excel from Asp.Net. I want to set bold and normal style to my cell but it is working for few cell and not for remaining cell.Please have look on following example:
Dim hssfworkbook As New HSSFWorkbook()
Dim sheetOne As HSSFSheet = hssfworkbook.CreateSheet("Sheet1")
hssfworkbook.CreateSheet("Sheet2")
hssfworkbook.CreateSheet("Sheet3")
Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
cellStyle.Alignment = HSSFCellStyle.ALIGN_CENTER
Dim font As HSSFFont = _hssfworkbook.CreateFont()
font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
cellStyle.SetFont(font)
For i = 0 To 9 Step 1
'I want to add cell style to these cells
If i Mod 2 = 0 Then
Sheet1.CreateRow(i).CreateCell(1).SetCellValue(i)
font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
cellStyle.SetFont(font)
Sheet1.GetRow(i).GetCell(1).CellStyle = cellStyle
Else
Sheet1.CreateRow(i).CreateCell(1).SetCellValue(i)
font.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL
cellStyle.SetFont(font)
Sheet1.GetRow(i).GetCell(1).CellStyle = cellStyle
End If
Next
Actually code is working fine but i don't know the particular situation from where and why its stops working for remaining few rows. Its not working properly for all cells and from that particular cell the bold and normal property stops working on whole sheet like sheet2 and sheet3.
You have to use multiple styles because a change to a style affects everywhere that style is referenced in the sheet.
Dim hssfworkbook As New HSSFWorkbook()
Dim sheetOne As HSSFSheet = hssfworkbook.CreateSheet("Sheet1")
hssfworkbook.CreateSheet("Sheet2")
hssfworkbook.CreateSheet("Sheet3")
Dim BoldFont As HSSFFont = hssfworkbook.CreateFont()
BoldFont.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
Dim NormalFont As HSSFFont = hssfworkbook.CreateFont()
NormalFont.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL
Dim cellStyleBold As HSSFCellStyle = hssfworkbook.CreateCellStyle()
With cellStyleBold
.setFont(BoldFont)
.Alignment = HSSFCellStyle.ALIGN_CENTER
End With
Dim cellStyleNormal As HSSFCellStyle = hssfworkbook.CreateCellStyle()
With cellStyleNormal
.setFont(NormalFont)
.Alignment = HSSFCellStyle.ALIGN_CENTER
End With
For i - 0 To 9 Step 1
If i Mod 2 = 0 Then
With Sheet1.CreateRow(i).CreateCell(1)
.SetCellValue(i)
.cellStyle = cellStyleBold
End With
Else
With Sheet1.CreateRow(i).CreateCell(1)
.SetCellValue(i)
.cellStyle = cellStyleNormal
End With
End If
Next

Loop through controls and change LINQ columns

I have the following code:
Dim i As Integer
For i = 1 To 20
Dim captionTextBox As TextBox = DirectCast(Me.FindControl("news_Image" + i.ToString() + "CaptionTextBox"), TextBox)
Dim deleteCheckBox As CheckBox = DirectCast(Me.FindControl("Image" + i.ToString() + "DeleteCheckBox"), CheckBox)
If Not deleteCheckBox.Checked = False Then
If Not captionTextBox.Text = "" Then
'Insert the value of the textbox into the column like news_Image1Caption, news_image2Caption nased on the value of i
End If
End If
Next
Where the comment is in the code above, I need to insert the value of the textbox (text) into Linq columns. For example: articleToUpdate.news_Image1Caption, news_Image2Caption, but I need to change the number after news_ to the value of i in the for loop. How can I do this?
Thanks
Luke
Did you try to set the ID column to Auto-Generated Value = true ?
INSERT INTO [dbo].[s_Tbl]([ID], [news_Image1Caption], [news_image2Caption]) VALUES (...)

Resources