Binding an xml to a StackedColumns chart - asp.net

I have two questions.
How do I construct my XML- file which i want to output in a Stacked Column chart using the code-behind?
And secondly,
How do I put it in the code-behind in vb?
This is the code-behind at the moment:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Chart1.Width = 200
Chart1.Height = 300
Dim XMLFile As XElement = XElement.Load(Server.MapPath("~/xml/XMLFile.xml"))
Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
Chart1.Series(0).Palette = DataVisualization.Charting.ChartColorPalette.BrightPastel
Chart1.Series("Series1").IsValueShownAsLabel = True
For Each node As XElement In XMLFile.Elements("something")
Dim string1 As String = node.Element("whatever1").Value
Dim string2 As String = node.Element("whatever2").Value
Chart1.Series("Series1").Points.AddXY(string1, string2)
Chart1.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True
Chart1.ChartAreas("ChartArea1").Area3DStyle.Inclination = 15
Chart1.ChartAreas("ChartArea1").Area3DStyle.Rotation = 15
Next
End Sub
The XMLFile looks something like this at the moment:
<root>
<something>
<whatever1>One</whatever1>
<whatever2>1</whatever2>
</something>
<something>
<whatever1>Two</whatever1>
<whatever2>2</whatever2>
</something>
</root>
I need two columns with three different values stacked in each column.
How do I do that?

Related

How can I create a self-incrementing column for a DataGridView?

I need to add a self-incrementing column to a DataGridView. Every time a row is added to the grid I want to increment the No column.
My Form_Load code:
Private Sub SAP_OrdenVenta_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.ColumnCount = 6
DataGridView1.Columns(0).Name = ("No")
DataGridView1.Columns(1).Name = ("NoArticulo")
DataGridView1.Columns(2).Name = ("Descripcion")
DataGridView1.Columns(3).Name = ("Cantidad")
DataGridView1.Columns(4).Name = ("Precio")
DataGridView1.Columns(5).Name = ("Total")
End Sub
The DataGridView Button Click event for the add button:
Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
Dim articulo, cantidad As Integer
Dim precio, total1 As Double
Dim i As Integer = 0
articulo = txtArticulo.Text.Trim()
cantidad = txtCantidad.Text.Trim()
precio = txtPrecio.Text.Trim()
total1 = txtPrecio.Text.Trim()
'Agrego Linea a DataGridView
Dim row As String() = New String() {1, articulo, "No disponible", cantidad, precio, total1}
DataGridView1.Rows.Add(row)
End Sub
How can I make this work?
Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
Dim LastIndex As Integer = DataGridView1.Rows.Count - 1
Dim LastNo As Integer = Integer.Parse(DataGridView1.Rows(LastIndex).Cells(0).Value)
Dim NewNo As String = (LastNo + 1).ToString()
Dim row As String() = New String() {NewNo, txtArticulo.Text, "No disponible", txtCantidad.Text, txtPrecio.Text, txtPrecio.Text}
DataGridView1.Rows.Add(row)
End Sub
Take note, however: if this view is loaded from a real database table, you almost always want to rely on your database's ability to generate ID values. Otherwise, this is a huge race condition waiting to blow up.

Gridview not sorting properly on DateTime coloumn

Private Sub GridViewLog_StartSorting(ByVal sender As Object, ByVal e As EventArgs) Handles GridViewLog.StartSorting
SetDefaultSort()
End Sub
Private Sub SetDefaultSort()
Dim col1 As DevExpress.XtraGrid.Columns.GridColumn
Dim col2 As DevExpress.XtraGrid.Columns.GridColumn
Dim sortIndex As Integer
Dim order As DevExpress.Data.ColumnSortOrder
Try
col1 = GridViewLog.Columns("Date_Field")
col2 = GridViewLog.Columns("Time_Field")
If Not ((GridViewLog.SortedColumns.Contains(col1) OrElse GridViewLog.GroupedColumns.Contains(col1)) AndAlso (GridViewLog.SortedColumns.Contains(col2) OrElse GridViewLog.GroupedColumns.Contains(col2))) Then
Me.GridViewLog.BeginSort()
If col1.SortOrder = DevExpress.Data.ColumnSortOrder.None Then
order = col2.SortOrder
Else
order = col1.SortOrder
End If
If order = DevExpress.Data.ColumnSortOrder.None Then
order = DevExpress.Data.ColumnSortOrder.Descending
End If
sortIndex = GridViewLog.SortedColumns.Count
Me.GridViewLog.Columns("Date_Field").SortIndex = sortIndex - 1
Me.GridViewLog.Columns("Date_Field").SortOrder = order
Me.GridViewLog.Columns("Time_Field").SortIndex = sortIndex
Me.GridViewLog.Columns("Time_Field").SortOrder = order
Me.GridViewLog.EndSort()
End If
Catch ex As Exception
End Try
End Sub
Changed some times to 10:31 and they didn't re-sort. I then hit Time at the top of the column and it still didn’t change. Please check the attached screenshot
I think there are two options:
Set the columns' SortMode property to ColumnSortMode.Value
Use CustomColumnSort event and set the columns' SortMode property to ColumnSortMode.Custom

ASP.Net Gridview paging of columns

I am using ASP gridview in my program to show the records of the employees. The data contains about 60 columns. I have converted the columns into rows using the following code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
FillGrid1()
End If
End Sub
Public Sub FillGrid1()
Dim strUnit As String
Dim intMonthYr As Integer
strUnit = Session("Unit")
intMonthYr = Session("MonthYr")
objReports.Unit = strUnit
objReports.ForMonth = intMonthYr
ds = objReports.MonthlyPayBill
Dim new_ds As New DataSet
new_ds = FlipDataSet(ds)
Dim my_DataView As DataView = new_ds.Tables(0).DefaultView
GridView1.DataSource = my_DataView
GridView1.DataBind()
End Sub
Public Function FlipDataSet(ByVal my_DataSet As DataSet) As DataSet
Dim ds1 As New DataSet
Dim table As New DataTable()
For Each dt As DataTable In my_DataSet.Tables
For i As Integer = 0 To dt.Rows.Count
table.Columns.Add(Convert.ToString(i))
Next
Dim r As DataRow
For k As Integer = 0 To dt.Columns.Count - 1
r = table.NewRow()
r(0) = dt.Columns(k).ToString()
For j As Integer = 1 To dt.Rows.Count
r(j) = dt.Rows(j - 1)(k)
Next
table.Rows.Add(r)
Next
ds1.Tables.Add(table)
Next
Return ds1
End Function
The 60 columns are placed as 60 rows and each person is represented as a column.There are about 50 person's record and I want to go for paging in the columns to show 10 persons in each page rather than 10 field in the gridview.

Repeater forgetting how many items to display when next button is clicked

I have a repeater list that displays results in sets of 15. When you click the next button it shows the next 15 and so on.
I have added some buttons that will then filter the display to show the results in sets of 10, 25, 50.
When you click these it does work but when you click the next button it resets the display value to 15.
Below is the chunk of script:
Public Property CurrentPage() As Integer
Get
' look for current page in ViewState
Dim o As Object = Me.ViewState("_CurrentPage")
If o Is Nothing Then
Return 0
Else
' default to showing the first page
Return CInt(o)
End If
End Get
Set
Me.ViewState("_CurrentPage") = value
End Set
End Property
Protected Sub ItemsGet()
Dim pageSize As Integer = 15
ItemsGet(pageSize)
End Sub
Private Sub ItemsGet(ByVal pageSize As Integer)
' Read sample item info from XML document into a DataSet
' Populate the repeater control with the Items DataSet
Dim objPds As New PagedDataSource()
Dim selectedCategory As String = ddlCategory.SelectedValue.ToString()
Dim selectedCategoryIndex As Integer = ddlCategory.SelectedIndex
Dim selectedCategoryMonth As String = ddlCategoryMonth.SelectedValue.ToString()
Dim selectedCategoryMonthIndex As Integer = ddlCategoryMonth.SelectedIndex
Dim query = GetXmlDataSet()
If (Not String.IsNullOrEmpty(selectedCategory) And selectedCategoryIndex > 0) Then
query = query.Where(Function(x) x("SCategoryName") = selectedCategory)
End If
If (Not String.IsNullOrEmpty(selectedCategoryMonth) And selectedCategoryMonthIndex > 0) Then
query = query.Where(Function(x) x("SCategoryMonth") = selectedCategoryMonth)
End If
If (query.Count() > 0) Then
objPds.DataSource = query.CopyToDataTable().Rows
objPds.AllowPaging = True
objPds.PageSize = pageSize
objPds.CurrentPageIndex = CurrentPage
lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objPds.PageCount.ToString()
' Disable Prev or Next buttons if necessary
cmdPrev.Enabled = Not objPds.IsFirstPage
cmdNext.Enabled = Not objPds.IsLastPage
Display10.Enabled = True
Display25.Enabled = True
Display50.Enabled = True
categories.DataSource = objPds
categories.DataBind()
Else
CurrentPage = 0
categories.Controls.Clear()
cmdPrev.Enabled = False
cmdNext.Enabled = False
Display10.Enabled = False
Display25.Enabled = False
Display50.Enabled = False
lblCurrentPage.Text = "Page: 0 of 0 "
End If
End Sub
Private Sub Display10_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim pageSize As Integer = 10
CurrentPage = 0
ItemsGet(pageSize)
End Sub
Private Sub Display25_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim pageSize As Integer = 25
CurrentPage = 0
ItemsGet(pageSize)
End Sub
Private Sub Display50_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim pageSize As Integer = 50
CurrentPage = 0
ItemsGet(pageSize)
End Sub
Private Sub cmdPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Set viewstate variable to the previous page
CurrentPage -= 1
' Reload control
ItemsGet()
End Sub
Private Sub cmdNext_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Set viewstate variable to the next page
CurrentPage += 1
' Reload control
ItemsGet()
End Sub
Protected Sub ddlCategory_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
CurrentPage = 0
ItemsGet()
End Sub
Protected Sub ddlCategoryMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
CurrentPage = 0
ItemsGet()
End Sub
You need to 'persist' the number of items to get.
There are a few ways to do this....
Re-factor PageSize into a property with its backing kept in the viewstate, remember to initialise with appropriate default values.
Change the ItemsGet sub to use the property instead.
My vb is rusty!
Public Property PageSize() As Integer
Get
If Me.ViewState("PageSize") Is Nothing Then
Me.ViewState("PageSize") = 15
End If
Return CInt( Me.ViewState("PageSize") )
End Get
Set
Me.ViewState("PageSize") = value
End Set
End Property

vb.net - Inserting number of files named XX into a string

Have the following code which creates a table of all the images in a folder.
VB:
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
Dim dirInfo as New DirectoryInfo(Server.MapPath("/images"))
articleList.DataSource = dirInfo.GetFiles("*.jpg")
articleList.DataBind()
End If
End Sub
Body:
<asp:DataGrid runat="server" id="articleList" AutoGenerateColumns="False" ShowHeader="false">
<Columns>
<asp:BoundColumn DataField="Name" />
</Columns>
</asp:DataGrid>
The results look something like this:
aa_01.jpg
aa_02.jpg
aa_03.jpg
bb_01.jpg
bb_02.jpg
cc_01.jpg
cc_02.jpg
cc_03.jpg
cc_04.jpg
...
What I want to do now is group all these by the first two characters and get the total number for each and insert them into individual strings. So for the above example it would be something like:
Dim aa As String = 3
Dim bb As String = 2
Dim cc As String = 4
Any ideas how?
You can do this using Linq. Try this.
dim q = From p in articles _
Group By Name = p.Name.SubString(0,2) into g = group _
Select GroupName = Name, _
Count = g.Count()
Update (to give a bit of context as per comments)
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
Dim dirInfo as New DirectoryInfo(Server.MapPath("/images"))
Dim articles = dirInfo.GetFiles("*.jpg")
articleList.DataSource = articles
articleList.DataBind()
Dim groupings = From p in articles _
Group By Name = p.Name.SubString(0,2) into g = group _
Select GroupName = Name, _
Count = g.Count()
' do whatever you like with the groupings '
End If
End Sub
In C#, using LINQ:
groupedArticleList = (from a in articleList
group a by a.Name.Substring(0, 2) into g
select new
{
GroupName = g.Key,
ArticleCount = g.Count()
});
You can't really extract them into separate variables but you could use groupedArticleList as-is and loop through it or convert it to a Dictionary<string, int>.
Sorry I don't know the VB syntax but hopefully that will help.

Resources