Loop through the datalist to get data - asp.net

I'm stuck in converting the rate in the datalist. My page contain one dropdownlist(currency converter), one datalist - inside contain the price of bags in labels. Now I uses the dropdownlist.selectedIndexchange
Protected Sub ddl_rate_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_rate.SelectedIndexChanged
Dim lbl_rate As Label = roomList.Items.Item(0).FindControl("lbl_rate")
Dim a As Integer = Convert.ToInt32(lbl_rate.Text)
If ddl_rate.Text = "Australia Dollars (AUD)" Then
Dim b As Integer = (a * 0.8029)
lbl_rate.Text = b.ToString()
ElseIf ddl_rate.Text = "Brazil Reais (BRL)" Then
Dim b As Integer = (a * 1.3024)
lbl_rate.Text = b.ToString()
End If
Althought it did convert the rate, but it only convert the first row.
Any suggestion?

this is a guess without seeing the rest of the code:
Protected Sub ddl_rate_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_rate.SelectedIndexChanged
foreach(c as Control in roomList.Items)
Dim lbl_rate As Label = c.FindControl("lbl_rate")
Dim a As Integer = Convert.ToInt32(lbl_rate.Text)
If ddl_rate.Text = "Australia Dollars (AUD)" Then
Dim b As Integer = (a * 0.8029)
lbl_rate.Text = b.ToString()
ElseIf ddl_rate.Text = "Brazil Reais (BRL)" Then
Dim b As Integer = (a * 1.3024)
lbl_rate.Text = b.ToString()
End If
next

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.

Move grid rows up or down on button click

I have a grid that I am trying to move the grid rows either up or down based on button click. Here is what I have so far.
Protected Sub imgBtnMoveUp_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
Dim imgBtn As ImageButton
Dim FirstRow As GridViewRow = gvQuoteSo.Rows(0)
Dim btnUp As Button = DirectCast(FirstRow.FindControl("MoveUp"), Button)
Dim gvrow As GridViewRow
Dim previousRow As GridViewRow
Dim index As Integer = 0
imgBtn = CType(sender, ImageButton)
If imgBtn.CommandName = "MoveUp" Then
index = Convert.ToInt32(imgBtn.CommandArgument)
gvrow = gvQuoteSo.Rows(index)
previousRow = gvQuoteSo.Rows(index - 1)
UpdatePanelGrid.Update()
End If
End Sub
When I click the button for up nothing hapens. I know this is just the move up function but if I can get help with that then the move down will answer itself. Thanks so much!
Explanations are given in comments.
Private Sub btnUp_Click(sender As Object, e As EventArgs)
Dim grid As DataGridView = Grid_view
Try
Dim totalRows As Integer = grid.Rows.Count ' will count the number of rows
Dim idx As Integer = grid.SelectedCells(0).OwningRow.Index ' row index of the selected cell
If idx = 0 Then ' for no rows selected
Return
End If
Dim col As Integer = grid.SelectedCells(0).OwningColumn.Index ' columns corresponding to the selected row
Dim rows As DataGridViewRowCollection = grid.Rows
Dim row As DataGridViewRow = rows(idx)
rows.Remove(row) ' Remove the corrent row
rows.Insert(idx - 1, row)' Re-insert the row
grid.ClearSelection()
grid.Rows(idx - 1).Cells(col).Selected = True
Catch
End Try
End Sub
Private Sub btnDown_Click(sender As Object, e As EventArgs)
Dim grid As DataGridView = Grid_view
Try
Dim totalRows As Integer = grid.Rows.Count
Dim idx As Integer = grid.SelectedCells(0).OwningRow.Index
If idx = totalRows - 2 Then
Return
End If
Dim col As Integer = grid.SelectedCells(0).OwningColumn.Index
Dim rows As DataGridViewRowCollection = grid.Rows
Dim row As DataGridViewRow = rows(idx)
rows.Remove(row)
rows.Insert(idx + 1, row)
grid.ClearSelection()
grid.Rows(idx + 1).Cells(col).Selected = True
Catch
End Try
End Sub

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

Access Multiple Dynamic Dropdownlists selected Value - ASP.Net

I am adding multiple dynamic dropdownlists to my form.
I'm wanting to access the selected values of the dropdownlists on click of a button.
Private pnlDropDownList As Panel
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreInit
'Create a Dynamic Panel
pnlDropDownList = New Panel()
pnlDropDownList.ID = "pnlDropDownList"
pnlDropDownList.BorderWidth = 1
pnlDropDownList.Width = 300
Me.form1.Controls.Add(pnlDropDownList)
'Create a LinkDynamic Button to Add TextBoxes
Dim btnAddDdl As New Button
btnAddDdl.ID = "btnAddDdl"
btnAddDdl.Text = "Add DropDownList"
AddHandler btnAddDdl.Click, AddressOf btnAdd_Click
Me.form1.Controls.Add(btnAddDdl)
'Recreate Controls
RecreateControls("ddlDynamic", "DropDownList")
End Sub
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim cnt As Integer = FindOccurence("ddlDynamic")
CreateDropDownList("ddlDynamic-" & Convert.ToString(cnt + 1))
End Sub
Private Function FindOccurence(ByVal substr As String) As Integer
Dim reqstr As String = Request.Form.ToString()
Return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length)
End Function
Private Sub RecreateControls(ByVal ctrlPrefix As String, ByVal ctrlType As String)
Dim ctrls As String() = Request.Form.ToString().Split("&"c)
Dim cnt As Integer = FindOccurence(ctrlPrefix)
If cnt > 0 Then
For k As Integer = 1 To cnt
For i As Integer = 0 To ctrls.Length - 1
If ctrls(i).Contains((ctrlPrefix & "-") + k.ToString()) AndAlso Not ctrls(i).Contains("EVENTTARGET") Then
Dim ctrlID As String = ctrls(i).Split("="c)(0)
If ctrlType = "DropDownList" Then
CreateDropDownList(ctrlID)
End If
Exit For
End If
Next
Next
End If
End Sub
Private Sub CreateDropDownList(ByVal ID As String)
Dim ddl As New DropDownList()
ddl.ID = ID
ddl.DataSource = Me.odsNames
ddl.DataTextField = "Name"
ddl.DataValueField = "ID"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("All", -1))
ddl.SelectedIndex = 0
ddl.AutoPostBack = True
AddHandler ddl.SelectedIndexChanged, AddressOf OnSelectedIndexChanged
pnlDropDownList.Controls.Add(ddl)
Dim lt As New Literal()
lt.Text = "<br />"
pnlDropDownList.Controls.Add(lt)
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
'Not used, want to get Values from button click
End Sub
Protected Sub cmdAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdAdd.Click
Dim cnt As Integer = FindOccurence("ddlDynamic")
pnlDropDownList.Controls.Remove(pnlDropDownList.FindControl("ddlDynamic-" & Convert.ToString(cnt)))
End Sub
This is where I would like to check all dynamic dropdownlists and extract their selected values
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
'' Check All dynamic Dropdownlists and retrieve selected values
End Sub
End Class
Any assistance would be much appreciated. thanks
You could use Linq to get the references of your dynamic DropDowns in the panel:
Dim allDdls = pnlDropDownList.Controls.OfType(Of DropDownList)()
For Each ddl In allDdls
Dim selectedValue = ddl.SelectedValue
Next

Resources