DevExpress - xtragrid column running totals - devexpress

I am using DevExpress xtragrid that is bound to a data source... all is fine there. I am adding 1 unbound column (balance) that will hold the result of a calculation. The 'balance' column MUST recalculate when the debit and / or credit column changes anywhere in the grid. Given there may be a large amount of records I am hoping a loop statement will not be my only option. Rather I was hoping for a solution using the
Expression editor.
example:
dr cr balance
100 0 100
0 50 50
0 45 5

Create a new column in your data source called Amount. In this column you will store both the debits and credits as positive and negative values
Add a new column to your xtragrid. Name it colRunningBalance
Set the UnboundType of the column to decimal. This tells the grid
you will be processing the data yourself
Add the following event to your form for the grid. The code is in VB.NET but it should be pretty easy to convert to any language
Private Sub gridView_CustomUnboundColumnData(sender As System.Object, e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles
gvCash.CustomUnboundColumnData
Dim view = DirectCast(sender, GridView)
If e.Column.FieldName = "colRunningBalance" And e.IsGetData Then
Dim total = 0D
For i As Integer = -1 To e.ListSourceRowIndex - 1
total += CDec(view.GetListSourceRowCellValue(i + 1, "Amount"))
Next
e.Value = total
End If
End Sub

Related

getting values from two dynamic textboxes in different Panels

I have two dynamic textboxes in different Panels. When I click the button save it saves multiple records on the system. for example: I have two textbox on each panel. On the Panel 1 lets say it has (a, b) and On the Panel 2 lets say it has (1 ,2). What I'm expecting a result is a1 and b2. but the result goes like this a1 a2 b1 b2 which is wrong. Please help. here's the code of the button.
For Each text2 As TextBox In pnlTextBoxes.Controls.OfType(Of TextBox)()
For Each text1 As TextBox In pnlTextdesc.Controls.OfType(Of TextBox)()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("prod").ToString)
con.Open()
Try
Using cmd As New SqlCommand("INSERT INTO tblProduct (ProductName,productdescription) VALUES (#ProductName,#productdescription) ")
cmd.Parameters.AddWithValue("#ProductName", text2.text)
cmd.Parameters.AddWithValue("#productdescription", text1.text)
cmd.Connection = con
cmd.ExecuteNonQuery()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
Next
Next
You get that result because you are using nested For Each loops. If you have two letters and two numbers and, for each letter, you combine it with each number, of course you'll end up with four combinations. What you should do is create two lists of TextBoxes from the two Panels, then use a single For loop to combine them by index, e.g.
Dim list1 = TextBox In pnlTextBoxes.Controls.OfType(Of TextBox)().ToArray()
Dim list2 = TextBox In pnlTextdesc.Controls.OfType(Of TextBox)().ToArray()
For i = 0 To list1.GetUpperBound(0)
Dim text1 = list1(i)
Dim text2 = list2(i)
'...
Next
This assumes that there will be the same number of TextBoxes in each Panel and that they are in corresponding order.

ASP Format to percentage specific row of gridview

I have a fixed size table databound to a gridview from a stored procedure:
Note: the table is 17 columns long.
Currently I'm using a For..Next statement to alter the background colour of the percentage row. Can I adapt this to use DataFormatString="{0:0.0%}" instead of the backcolor attribute or should I be doing this another way?
Here's my code which currently turns the background of the row purple:
For colCount As Integer = 1 To 17
For rowCount As Integer = 1 To 1
gvAttainmentGrid.Rows(rowCount).Cells(colCount).Attributes = Drawing.Color.FromArgb(177, 160, 199)
Next
Next
EDIT
I've made some progress. I have updated my For..Next and although this displays OK on page load, when I select a different value in the control or add the element to my pre-render procedure it errors out with the message:
Input string was not in a correct format.
My code now looks like the following:
For colCount As Integer = 1 To 17
For rowCount As Integer = 1 To 1
gvAttainmentGrid.Rows(rowCount).Cells(colCount).Text = FormatPercent(Convert.ToDecimal(gvAttainmentGrid.Rows(rowCount).Cells(colCount).Text), 1)
Next
Next

How to get count of repeated values in datatable in asp.net

I want total count of repeated values in a datable.
as shown in image total count should be : 2 because repeated values are only 1 and 2.
I tried as given below:
DataView dv = new DataView(dtTemp);
int iRowCount = dv.ToTable(true, "Column1").Rows.Count;
but it returns 3 which is incorrect.
Does anyone knows how to do it.
I don't want to use loop becoz sometimes data table contains 4000-5000 rows so if we use loop it will take much more time to get the total count.
Thanks.

Data set skip column

I have one dataset in which i have say 10 rows..
I want to skip data of first two column means whatever data is there in that first 2 column
sholud not present in that dataset how to do this..?
What is the reason? Are you trying to do this, because you did not want it to appear in the DataGrid... Or are you trying to do some merge operations...
If for datagrid, you can just hide those columns in the DataGrid/Gridview...
If it is otherwise you can remove the columns as stated by Tim...
If you mean a Datatable instead of a dataset you can remove colums:
Dim removeCount As Int32 = 2
For i As Int32 = 1 To removeCount
For ii As Int32 = 0 To myDataTable.Columns.Count - 1
If myDataTable.Columns.CanRemove(myDataTable.Columns(ii)) Then
myDataTable.Columns.RemoveAt(ii)
Exit For
End If
Next
Next
Removes the two first columns(if they are removable and not primary keys).
Or even simpler, when removing the primary key from the datatable is acceptable:
For i As Int32 = 1 To removeCount
If Not myDataTable.Columns.CanRemove(myDataTable.Columns(0)) Then
myDataTable.PrimaryKey = Nothing
End If
myDataTable.Columns.RemoveAt(0)
Next

Randomize numbers in VB.NET without using a range i.e. Min and Max

I was wondering if anyone can point me in the right direction please? Say I am pulling the following table, I would then like to select an ID randomly. I understand how to select a random number using a Randomize() call followed by the relevant syntax but I want to pre-define the range.
i.e. Table Data
ID | Name
4345 Mike
3456 Lee
4567 John
There will be many more names but for this example you could use 3 or 4 etc..
Please help I'm starting to itch :o|
Just to make sure I understand what you want:
Given a table, you want to randomly select one of the ID values from that table.
If so, this should do it:
Dim rand As New Random()
Dim record As Integer = rand.[Next](0, myDataTable.Rows.Count)
Dim randomID As Integer = CInt(myDataTable.Rows(record)("ID"))
We have all the information we need to randomly select a row, and by extension randomly select one of the ID values in the table.
In old Vb you would do
Dim i as integer
i = (Rnd * (maxval-minval)) + minval
Since rnd returns a random number between 0 and 1 you would scale the number to the right range.

Resources