How to change all the values in the column in datagridview - vb.net

I want to set the value in the data grid view if the column "is_active" have the value of 0 then it show "ACTIVE" not 0.
MY CODE:
Dim dt As DataTable
dt = exec("select * from tbl_credentials where is_active = 1 and is_status = 2")
If DataGridView1.Columns(3).ToString = 1 Then
dt.Columns(3).value = "ACTIVE"
End If
DataGridView1.DataSource = dt

use select CASE :
Dim dt As DataTable
dt = exec("select username,password, CASE is_active WHEN 0 then 'ACTIVE' ELSE '' END from tbl_credentials")
DataGridView1.DataSource = dt

Related

how i can sum an column in datatable in different dates?

i am trying to sum an column in datatable by different dates and displaying the value in textbox but only i am getting the first row value in the column not the the sum , i attached an picture and my code is
Dim dt As DataTable = New DAL().selectdatatable(String.Format("
SELECT
SUM(PT.PT_Paid) AS SumOfPT_Paid
, SUM(PT.PT_Remain) AS SumOfPT_Remain
, PT.PT_Date
FROM
PT
GROUP BY
PT.PT_Date
HAVING
(
(
(
PT.PT_Date
)
>=#{0}#
AND
(
PT.PT_Date
)
<=#{1}#
)
)
;", dateshow.Value.ToString("dd/MM/yyyy"), dateshow2.Value.ToString("dd/MM/yyyy")))
If dt.Rows.Count > 0 Then
txtin.Text = dt.Rows(0)(0).ToString
txtremain.Text = dt.Rows(0)(1).ToString
Else
txtin.Text = "0"
txtremain.Text = "0"
End If
Data:

Display number of rows and columns

I have in my table MS Access named ( Table1 ) two fields ( ID1 - Team1 ).
With NumericUpDown1 i select the number of rows that i want to display after randomize in DataGridView2.With NumericUpDown2 i select the number of columns that i want to display after randomize in DataGridView2.If i choose with NumericUpDown2 only one column ( the number 1 ) it work very well with this query :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Con_randomize()
Dim rows As Integer
If Not Integer.TryParse(NumericUpDown1.Value, rows) Then
MsgBox("NUMBER NOT AVAILABLE", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error")
NumericUpDown1.Value = ""
NumericUpDown1.Focus()
Exit Sub
End If
If NumericUpDown2.Value = 1 Then
Dim sql As String = String.Format("SELECT Top {0} ID1,Team1 From Table1 ORDER BY RND(-(100000*ID1)*Time())", rows)
InfoCommand = New OleDbCommand(sql, Con_randomize)
InfoAdapter = New OleDbDataAdapter()
InfoAdapter.SelectCommand = InfoCommand
InfoTable = New DataTable()
InfoAdapter.Fill(InfoTable)
DataGridView2.DataSource = InfoTable
DataGridView2.Columns(0).HeaderText = "NUMERO"
DataGridView2.Columns(1).HeaderText = "CATEGORY1"
End If
End Sub
How to make if i choose with NumericUpDown2 the number 2 or 3 columns i want to display in Datagridview2.
The columns will be named ( CATEGORY2 - CATEGORY3 ) . for example ( 1 Victor - David - Vincent ) ( 2 wiliam- George - Joseph ) ..in my only field named Team1 I have a hundred of the names
I'm not 100% sure why you need a query to initialize the data, but give this a go. It will automatically create the columns the way you need them.
' If NumericUpDown2.Value = 1 Then ' Comment This If Block Out
' Create the Teams String
Dim teamsString as New System.Text.StringBuilder("")
For i as Integer = 1 to Convert.ToInt32(NumericUpDown2.Value)
teamsString.Append(", (SELECT Top 1 Team1 From Table1 ORDER BY RND(-(100000*ID1)*Time())) as Category" + i.ToString())
Next
Dim sql As String = String.Format("SELECT Top {0} ID1" + teamsString.ToString() + " From Table1 ORDER BY RND(-(100000*ID1)*Time())", rows)
InfoCommand = New OleDbCommand(sql, Con_randomize)
InfoAdapter = New OleDbDataAdapter()
InfoAdapter.SelectCommand = InfoCommand
InfoTable = New DataTable()
InfoAdapter.Fill(InfoTable)
DataGridView2.DataSource = InfoTable
DataGridView2.Columns(0).HeaderText = "NUMERO"
' Don't Need This, We Made It the Binding Name
' DataGridView2.Columns(1).HeaderText = "CATEGORY1"

Copy rows between two datatable VB.net

I have two datatable how i could copy targeted rows index to another datatable in the same index, Please check below code.
Dim datatable1 As DataTable = GetEmployeeSummary()
Dim datatable2 As DataTable = GetEmployees()
For i As Integer = 0 To datatable1.Rows.Count - 1 'Datatable1.rows.count = datatable2.rows.count
Select Case i
Case 1, 5, 6, 19, 24
datatable2.Rows(i) = datatable2.Rows(i) 'how i could copy targeted rows index to another datatable in the same index
End Select
Next
You can use the DataRow.ItemArray if both tables have the same columns:
For i As Int32 = 0 To datatable1.Rows.Count - 1
Select Case i
Case 1, 5, 6, 19, 24
If datatable2.Rows.Count - 1 >= i Then
datatable2.Rows(i).ItemArray = datatable1(i).ItemArray
Else
Dim row = datatable2.Rows.Add()
row.ItemArray = datatable1(i).ItemArray
End If
End Select
Next
I recommend of using ImportRow. It will copy the whole row into your DataTable. So Your code would be like below.
Dim datatable1 As DataTable = GetEmployeeSummary()
Dim datatable2 As DataTable = GetEmployees()
For i As Integer = 0 To datatable1.Rows.Count - 1
Select Case i
Case 1, 5, 6, 19, 24
datatable2.ImportRow(datatable2.Rows(i))
End Select
Next

vb.net - How can I change the type of values that the DataTable can store?

I want to add characters (like % and pp) in values stored in a DataTable, but I'm getting an error message:
Input string was not in a correct format.Couldn't store <97.0%> in Actual Column. Expected type is Decimal.
How can I change the type of values that the DataTable can store?
Dim dv As New System.Data.DataView
Dim dt As New System.Data.DataTable
dv = SQL_Customer.Select(DataSourceSelectArguments.Empty)
dt = dv.ToTable()
dt.Rows(1)(1) = CStr(dt.Rows(1)(1)) & "%"
dt.Rows(1)(2) = CStr(dt.Rows(1)(2)) & "%"
dt.Rows(1)(3) = CStr(dt.Rows(1)(3)) & "%"
dt.Rows(1)(4) = CStr(dt.Rows(1)(4)) & "pp"
You can't change the type of a data table column once it's filled.
One work around is to add a new column of type string and then populate that column with the string values you need and then delete the column that is of the type you don't need.
If this helps someone, this is how I resolved the problem:
Dim dv As New System.Data.DataView
Dim dt As New System.Data.DataTable
Dim dt2 As New System.Data.DataTable
Dim SelectedIndex As Object
If datagrid.SelectedIndex = "-1" Then SelectedIndex = 0 Else SelectedIndex = GV_Cust.SelectedIndex
'Populate Dataview with data in SQL Query for Customer KPIs
dv = SQL_Customer.Select(DataSourceSelectArguments.Empty)
'Populate table with data from dataview. Note that the data is formated as per data loaded. So if number is loaded, then format for the cell is number. This creates a problem when adding percentages
dt = dv.ToTable()
'New colums are added as string format in the dataTable so that % and pp can be added
Dim column As DataColumn
For i = 1 To 6
' Create second column.
column = New DataColumn()
column.DataType = System.Type.GetType("System.String")
column.ColumnName = i
column.AutoIncrement = False
column.Caption = i
column.ReadOnly = False
column.Unique = False
' Add the Column to the DataColumnCollection.
dt.Columns.Add(column)
Next
'New columns are populated and calculations for variances are calculated
For j = 0 To 2
For i = 0 To 4
dt.Rows(i)(7 + j) = dt.Rows(i)(1 + j) & "%"
dt.Rows(i)(10) = (dt.Rows(i)(1) - dt.Rows(i)(2)) & "pp"
dt.Rows(i)(11) = (dt.Rows(i)(1) - dt.Rows(i)(3)) & "pp"
dt.Rows(i)(12) = (dt.Rows(i)(2) - dt.Rows(i)(3)) & "pp"
Next
For i = 5 To 6
dt.Rows(i)(7 + j) = dt.Rows(i)(1 + j)
If dt.Rows(i)(2) = 0 Then dt.Rows(i)(10) = 0 & "%" Else dt.Rows(i)(10) = Math.Round((((dt.Rows(i)(1) / dt.Rows(i)(2))) - 1) * 100, 1) & "%"
If dt.Rows(i)(3) = 0 Then dt.Rows(i)(11) = 0 & "%" Else dt.Rows(i)(11) = Math.Round((((dt.Rows(i)(1) / dt.Rows(i)(3))) - 1) * 100, 1) & "%"
If dt.Rows(i)(3) = 0 Then dt.Rows(i)(12) = 0 & "%" Else dt.Rows(i)(12) = Math.Round((((dt.Rows(i)(2) / dt.Rows(i)(2))) - 1) * 100, 1) & "%"
Next
Next
'Old columns are deleted and new wones are renamed
dt.Columns.Remove("A")
dt.Columns("1").ColumnName = "A"
dt.Columns.Remove("B")
dt.Columns("2").ColumnName = "B
dt.Columns.Remove("C")
dt.Columns("3").ColumnName = "C"
dt.Columns.Remove("D")
dt.Columns("4").ColumnName = "D"
dt.Columns.Remove("D")
dt.Columns("5").ColumnName = "E"
dt.Columns.Remove("F")
dt.Columns("6").ColumnName = "F"
'Customer grid is populated, a selection button is added and the first row is selected
datagrid.DataSource = dt
datagrid.AutoGenerateSelectButton = True
datagrid.DataBind()
datagrid.SelectedIndex = SelectedIndex

How to remove all duplicates in a data table in vb.net?

Consider my data table
ID Name
1 AAA
2 BBB
3 CCC
1 AAA
4 DDD
Final Output is
2 BBB
3 CCC
4 DDD
How can i remove the rows in the data table using Vb.Net
Any help is appreciated.
Following works if you only want the distinct rows(skip those with same ID and Name):
Dim distinctRows = From r In tbl
Group By Distinct = New With {Key .ID = CInt(r("ID")), Key .Name = CStr(r("Name"))} Into Group
Where Group.Count = 1
Select Distinct
' Create a new DataTable containing only the unique rows '
Dim tblDistinct = (From r In tbl
Join distinctRow In tblDistinct
On distinctRow.ID Equals CInt(r("ID")) _
And distinctRow.Name Equals CStr(r("Name"))
Select r).CopyToDataTable
If you want to remove the dups from the original table:
Dim tblDups = From r In tbl
Group By Dups = New With {Key .ID = CInt(r("ID")), Key .Name = CStr(r("Name"))} Into Group
Where Group.Count > 1
Select Dups
Dim dupRowList = (From r In tbl
Join dupRow In tblDups
On dupRow.ID Equals CInt(r("ID")) _
And dupRow.Name Equals CStr(r("Name"))
Select r).ToList()
For Each dup In dupRowList
tbl.Rows.Remove(dup)
Next
Here is your sample-data:
Dim tbl As New DataTable
tbl.Columns.Add(New DataColumn("ID", GetType(Int32)))
tbl.Columns.Add(New DataColumn("Name", GetType(String)))
Dim row = tbl.NewRow
row("ID") = 1
row("Name") = "AAA"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 2
row("Name") = "BBB"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 3
row("Name") = "CCC"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 1
row("Name") = "AAA"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 4
row("Name") = "DDD"
tbl.Rows.Add(row)
You can use the DefaultView.ToTable method of a DataTable to do the filtering like this:
Public Sub RemoveDuplicateRows(ByRef rDataTable As DataTable)
Dim pNewDataTable As DataTable
Dim pCurrentRowCopy As DataRow
Dim pColumnList As New List(Of String)
Dim pColumn As DataColumn
'Build column list
For Each pColumn In rDataTable.Columns
pColumnList.Add(pColumn.ColumnName)
Next
'Filter by all columns
pNewDataTable = rDataTable.DefaultView.ToTable(True, pColumnList.ToArray)
rDataTable = rDataTable.Clone
'Import rows into original table structure
For Each pCurrentRowCopy In pNewDataTable.Rows
rDataTable.ImportRow(pCurrentRowCopy)
Next
End Sub
Assuming you want to check all the columns, this should remove the duplicates from the DataTable (DT):
DT = DT.DefaultView.ToTable(True, Array.ConvertAll((From v In DT.Columns Select v.ColumnName).ToArray(), Function(x) x.ToString()))
Unless I overlooked it, this doesn't seem to be in the documentation (DataView.ToTable Method), but this also appears to do the same thing:
DT = DT.DefaultView.ToTable(True)