Click here to Skip to main content
15,917,608 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am using datatable and populating my grid through dataset from datatable..
Can any body tell me who can i apply a check of already exist record while adding value to grid...??????Plz its urgent.
Posted
Comments
Gyaneswar kumar 18-Nov-15 4:52am    
r u adding/inserting the new records from grid into datatable of dataset?
Member 12119549 18-Nov-15 7:30am    
yes i am just practicing it by adding values to a grid by datatable.. here's my code..
'creating dataTable
'Dim dt As New DataTable()
Dim dr As DataRow
dt.TableName = "AuthorBooks"
dt.Columns.Add(New DataColumn("AuthorName", GetType(String)))
dt.Columns.Add(New DataColumn("BookName", GetType(String)))
dt.Columns.Add(New DataColumn("BookType", GetType(String)))
dt.Columns.Add(New DataColumn("Publisher", GetType(String)))
dr = dt.NewRow()
dt.Rows.Add(dr)

ViewState("AuthorBooks") = dt

GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Private Sub AddNewRecordRowToGrid()

If ViewState("AuthorBooks") IsNot Nothing Then

Dim dtCurrentTable As DataTable = DirectCast(ViewState("AuthorBooks"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then

For i As Integer = 1 To dtCurrentTable.Rows.Count
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("AuthorName") = TextBox1.Text
drCurrentRow("BookName") = TextBox2.Text
drCurrentRow("BookType") = TextBox4.Text
drCurrentRow("Publisher") = TextBox5.Text

Next

If dtCurrentTable.Rows(0)(0).ToString() = "" Then
dtCurrentTable.Rows(0).Delete()
dtCurrentTable.AcceptChanges()
End If
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("AuthorBooks") = dtCurrentTable

GridView1.DataSource = dtCurrentTable
GridView1.DataBind()
End If
End If
End Sub

Should i iterate through the loop as i have tried it in a list and got succedded.But here i am not able to do as such..that's how i did it in list..
Dim result As Boolean = True
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
If VAR3 = ListBox1.Items(i).ToString Then
result = False
End If
Next
If result = False Then
Label1.Text = "Item already exits"
End If
If result = True Then
ListBox1.Items.Add(TextBox1.Text + "" + TextBox2.Text)
End If
Try
Label1.Text = ListBox1.SelectedItem.Text
Catch

Need answers plzzzz.........


1 solution

You can loop through all your rows in the DataTable and check; however, I'd suggest you fix your sql so that duplicates are not returned.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900