הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, October 18, 2016 5:15 PM
Hey,
How to check if the row has data or not in the datatable? for example my rows are : Name / Age. I tried to work with this code:
If Row("Name") or Row("Age") = "" then
else
AddNewRows()End If
But it didn't work, and it gives me error "Use the new keyword to create an object distance"
Any help?
Greetings,
John.
All replies (13)
Tuesday, October 18, 2016 5:20 PM
Hi
A DataTable Rows can be indexed with an Integer and not a String.
Are you maybe meaning to check if the column of a row is empty?
Please post the code that defines and fills the datatable.
Regards Les, Livingston, Scotland
Tuesday, October 18, 2016 5:39 PM
'dt is DataTable
For i As Integer = 0 To dt.Rows.Count - 1
Dim lst As New List(Of Object)(dt.Rows(i).ItemArray)
Dim valuesarr As String = String.Empty
For Each s As Object In lst
valuesarr &= s.ToString
Next
If String.IsNullOrEmpty(valuesarr) Then
MsgBox("Row " & i & " is blank")
End If
Next
http://www.codeproject.com/Questions/246050/Remove-empty-rows-from-datatable
Paul ~~~~ Microsoft MVP (Visual Basic)
Tuesday, October 18, 2016 6:10 PM
Hey,
How to check if the row has data or not in the datatable? for example my rows are : Name / Age. I tried to work with this code:
If Row("Name") or Row("Age") = "" then else AddNewRows()End If
But it didn't work, and it gives me error "Use the new keyword to create an object distance"
Any help?
Greetings,John.
Hi
Here is an illustration of a DataGridView with a DataSource as a DataTable. There is a button which fills a list with the indexes of any empty rows and displays a MessageBox of the total. This example is in the form of an example |Project with an empty default Form1.
Option Strict On
Option Infer Off
Option Explicit On
Public Class Form1
Dim dt As New DataTable()
Dim DataGridView1 As New DataGridView
Dim b As New Button
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Size = New Size(500, 400)
dt.Columns.AddRange(New DataColumn() _
{
New DataColumn("ColA", GetType(Integer)),
New DataColumn("ColB", GetType(String)),
New DataColumn("ColC", GetType(String)),
New DataColumn("ColD", GetType(Double))
}
)
dt.TableName = "MyTable"
dt.Rows.Add(New Object() {1, "John", "McIntosh", 123.45})
dt.Rows.Add(New Object() {Nothing, Nothing, Nothing, Nothing})
dt.Rows.Add(New Object() {2, "Brian", "Jones", 11.3455})
dt.Rows.Add(New Object() {3, "Andrew", "Jones", 123.45})
dt.Rows.Add(New Object() {Nothing, Nothing, Nothing, Nothing})
dt.Rows.Add(New Object() {4, "John", "Smith", 321.1})
dt.Rows.Add(New Object() {5, "John", "Jones", 321.1})
dt.Rows.Add(New Object() {Nothing, Nothing, Nothing, Nothing})
dt.Rows.Add(New Object() {Nothing, Nothing, Nothing, Nothing})
dt.Rows.Add(New Object() {6, "Mary", "Clark", 11.3455})
dt.Rows.Add(New Object() {7, "Mary", "Smith", 543.23456})
dt.Rows.Add(New Object() {8, "Mary", "Duke", 7.89})
dt.Rows.Add(New Object() {9, "Johny", "McIntosh", 321.1})
With DataGridView1
.RowHeadersVisible = False
.Size = New Size(460, 300)
.DataSource = dt
End With
With b
.Text = "Check Empty Rows"
.AutoSize = True
.BringToFront()
.Location = New Point(20, DataGridView1.Bottom + 8)
End With
Me.Controls.AddRange({DataGridView1, b})
AddHandler b.Click, AddressOf DGVclick
End Sub
Private Sub DGVclick(sender As Object, e As EventArgs)
Dim lst As New List(Of Integer)
For r As Integer = 0 To dt.Rows.Count - 1
Dim s As String = Nothing
For Each item As Object In dt(r).ItemArray
s &= item.ToString
Next
If s = Nothing Then lst.Add(r)
Next
MessageBox.Show("There are " & lst.Count.ToString & " empty rows")
End Sub
End Class
Regards Les, Livingston, Scotland
Wednesday, October 19, 2016 7:05 AM
Hi John Zack,
The DataRow.IsNull Method (String) gets a value that indicates whether the named column contains a null valus. Hope it will be helpful to you.
Best Regards,
Neda Zhang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Wednesday, October 19, 2016 10:02 AM | 1 vote
Hello,
What I see in your code is that the check for Name has no condition, you need to place a condition on both Name an age rather than just Age. So a simple fix.
If Row("Name").ToString = "" OrElse Row("Age").ToString = "" Then
MessageBox.Show("Add new row")
End If
There are several methods (and twist on them) to do this. Here the first checks for row 1 if either column is empty working with strings, second, check a specific row for both fields empty (which is not what you are looking for but wanted to show), last check, do all rows and returns the index of each row meeting the conditions for name and age.
Dim table As New DataTable()
table.Columns.Add("Name", GetType(String))
table.Columns.Add("Age", GetType(String))
table.Rows.Add("Karen", "1")
table.Rows.Add("", "34")
table.Rows.Add("Anne")
table.Rows.Add("Jane", "")
table.Rows.Add("", "")
table.Rows.Add(Nothing, "1")
table.Rows.Add("Anne", "1")
' specific columns are empty
If String.IsNullOrWhiteSpace(table.Rows(1).Field(Of String)("Name")) OrElse String.IsNullOrWhiteSpace(table.Rows(1).Field(Of String)("Age")) Then
' add row
End If
' is a specific row totally empty
Dim emptyRow As Object() = {"", ""}
Console.WriteLine(table.Rows(4).ItemArray.SequenceEqual(emptyRow))
' get all rows meeting our condition index
Dim results As List(Of Integer) = table.AsEnumerable _
.Select(Function(row, index) New With {.Row = row, .Index = index}) _
.Where(Function(item) String.IsNullOrWhiteSpace(item.Row.Field(Of String)("Name")) OrElse String.IsNullOrWhiteSpace(item.Row.Field(Of String)("Age"))) _
.Select(Function(item) item.Index) _
.ToList
If results.Count > 0 Then
Console.WriteLine(String.Join(",", results))
End If
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Wednesday, October 19, 2016 3:18 PM
You didn't get my point, will make a new thread with more info!
Wednesday, October 19, 2016 3:56 PM
You didn't get my point, will make a new thread with more info!
Please do not start another thread, instead alter the original question to reflect what was missing. If you start a new thread with the same topic myself or another moderator will remove it or merge it with this thread.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Wednesday, October 19, 2016 5:11 PM
Okay, so, i have a program that locks and unlocks folder(s). This program has btnLock and btnUnlock and Text for the path and text for the folder name. When the user press "btnLock" it locks the folder and add rows to the data table. Now, the problem is that the code i use only adds 1 row. I wanna make it as i said before, wanna check if there is data in data table if there is then make a new row with the folder path.text and foldername.text if there isn't then DoNothing..
Wednesday, October 19, 2016 5:38 PM
Okay, so, i have a program that locks and unlocks folder(s). This program has btnLock and btnUnlock and Text for the path and text for the folder name. When the user press "btnLock" it locks the folder and add rows to the data table. Now, the problem is that the code i use only adds 1 row. I wanna make it as i said before, wanna check if there is data in data table if there is then make a new row with the folder path.text and foldername.text if there isn't then DoNothing..
That doesn't make sense.
Maybe you mean when btnlock is selected if the text for a path and text for a folder already exist in a DataTable then don't do anything and if it doesn't already exist then add a row and lock the folder.
And if btnUnlock is selected then remove the row btnUnlock pertains to after unlocking the folder.
La vida loca
Wednesday, October 19, 2016 6:19 PM
Okay, so, i have a program that locks and unlocks folder(s). This program has btnLock and btnUnlock and Text for the path and text for the folder name. When the user press "btnLock" it locks the folder and add rows to the data table. Now, the problem is that the code i use only adds 1 row. I wanna make it as i said before, wanna check if there is data in data table if there is then make a new row with the folder path.text and foldername.text if there isn't then DoNothing..
Well this project sounds familiar. OK, so if I understand your question correctly you want to add a row containing the name of the folder path and the folder name to the DataTable when the folder is locked. Why do you care whether there are any rows currently in the DataTable? Also, if you want to do this you can simply check the Count property of DataTable.Rows to see if it's a value of zero.
Paul ~~~~ Microsoft MVP (Visual Basic)
Wednesday, October 19, 2016 6:22 PM
Well this project sounds familiar...
It does...
"Everybody in this country should learn how to program a computer... because it teaches you how to think." (Steve Jobs)
Friday, October 21, 2016 1:45 PM
Sorry for being late. Was busy. Paul, i care because when i lock the folder it goes to the data table. If i locked another one, it will just replace it, i want it to make another Row. In the same Row"Folder" or Row"Destination another path and name under it.
Friday, October 21, 2016 2:47 PM
Sorry for being late. Was busy. Paul, i care because when i lock the folder it goes to the data table. If i locked another one, it will just replace it, i want it to make another Row. In the same Row"Folder" or Row"Destination another path and name under it.
I think we went through this before in another thread. A new row should not replace a current row unless your code is explicitly doing so, or, if you are removing all the rows from the DataTable and then adding the new row.
If the current row is being removed when a new row is added then you need to look at this section of code to see why, or at least post it so we can see what you are doing.
Paul ~~~~ Microsoft MVP (Visual Basic)