I have a DataTable that I use to add, modify and delete data locally.
The DataTable is also used to populate a Grid.
I need to order the grid according to an integer value (importance).
I wanted to implement two buttons to increase or decrease the importance of each record.
For this reason I wrote this code:
Dim rowNumber As Integer = 0
For i As Integer = 0 To dataTable.Rows.Count - 1
'search for the selected row in the DataGridView by ID
If dataTable.Rows(i)("ID") = ID Then
rowNumber= i
End If
Next
If rowNumber <> 0 Then
dataTable.Rows(rowNumber )("Priority") -= 1
dataTable.Rows(rowNumber - 1)("Priority") += 1
End If
It works fine the first time, it changes the selected record and makes changes to all the others to adjust the order. The second time it generates an error.
This because the DataTable is not automatically sorted according to the new Priority Order.
I would like to know if there is a way to sort the DataTable after changing the original order of the records (field Priority)
Try using a DataView for that:
Dim dv As New DataView(dataTable)
dv.Sort = "Priority"
Then use the dv as the DataSource for the grid.
Based on your comments, I think you need to find the row that has the other priority number:
Dim rowNumber As Integer = 0
Dim priorityNumber As Integer = 0
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)("ID") = ID Then
rowNumber = i
priorityNumber = dt.Rows(i)("Priority")
dt.Rows(i)("Priority") -= 1
End If
Next
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)("ID") <> ID AndAlso dt.Rows(i)("Priority") = priorityNumber Then
dt.Rows(i)("Priority") += 1
End If
Next
Related
I am new to Visual Basic. Net.
I am making one form in which the employee id will be next sequential number after checking the data from the datagridview.
Below is my code:
Sub GetEmployeeiD()
Dim id As Integer
Dim dg As DataGridView = dgvdata
If dg.Rows.Count = 0 Then
id = 1
Me.txtid.Text = id
ElseIf dg.Rows.Count > 0 Then
id = dg.Rows.Count + 1
Me.txtid.Text = id
End If
End Sub
The problem is as of now i have two records in my datagridview and when i click "Add", it should generate the next sequential id i.e. 3, but it is generating 4.
Please advise where i am wrong.
Thanks
Salman
I have created a datatable in my vb.net project and the datatable is the datasourse of a datagridview.
when a user click on the button called btnAdd, datarow will be added to datatable. I could do it as below for a one datarow. but there is another textbox called "txtQuantity" for item quantity and the user can type any number in it. So my question is, how can I add the same datarow to the datatable number of times as user mention in the txtQuantity ?
Dim dr As DataRow = dtOrderingItem.NewRow
dr("ItemCode") = clckdItemCode
dr("ItemName") = clickdItemName
dr("ServiceCode") = srvc
dr("RatePerItem") = txtItemRate.Text
dtOrderingItem.Rows.Add(dr)
dgvCart.datasource=dtOrderingItem
Use Integer.TryParse() to convert the value in your TextBox to an integer, then use that in a For loop as suggested by SSS.
Dim number As Integer
If Integer.TryParse(txtQuantity.Text, number) Then
If number >= 1 Then
Dim dr As DataRow = dtOrderingItem.NewRow
dr("ItemCode") = clckdItemCode
dr("ItemName") = clickdItemName
dr("ServiceCode") = srvc
dr("RatePerItem") = txtItemRate.Text
For i As Integer = 1 To number
dtOrderingItem.Rows.Add(dr)
Next
dgvCart.datasource = dtOrderingItem
End If
End If
I have a datatable that I am adding records to in my windows forms application. this datatable only has 2 columns and the first column is the primary key and is an integer. The second column contains names and I need to automatically add a list of names from a listbox to the table. Most of these names will already have their own record in the table but there will be different names in the listbox each time. I need to check the existing record items against the listbox items to make sure no duplicates get added, but if there is a name in the listbox that does not exist in the datatable, add a new record for that name.
What I have tried so far is this:
Private m_cn As New SqlConnection()
Private m_DA As SqlDataAdapter
Private m_CB As SqlCommandBuilder
Private m_DataTable As New DataTable
Private m_IntRowPosition As Integer = 0
Private Sub btnInsertIntoDatabase_Click(sender As Object, e As EventArgs) Handles btnInsertIntoDatabase.Click
Dim drReadRow As DataRow = m_DataTable.NewRow()
Dim dcReadCol As DataColumn = m_DataTable.Columns.Item(1)
Dim intLoopCounter As Integer = 0
Dim unique As Boolean = True
If m_DataTable.Rows.Count = 0 Then
For m_IntRowPosition = 0 To (lstScannedNames.Items.Count() - 1)
Dim drNewRow As DataRow = m_DataTable.NewRow()
drNewRow("emp_id") = m_IntRowPosition
drNewRow("emp_name") = RTrim(lstScannedNames.Items.Item(RTrim(m_IntRowPosition)))
m_DataTable.Rows.Add(drNewRow)
Next m_IntRowPosition
GoTo SomeWhereElse
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ElseIf m_DataTable.Rows.Count > 0 Then
For m_IntRowPosition = 0 To m_DataTable.Rows.Count
For intLoopCounter = 0 To lstScannedNames.Items.Count - 1
If RTrim(m_DataTable.Rows(m_IntRowPosition).Item(1)) = lstScannedNames.Items.Item(intLoopCounter) Then
unique = False
ElseIf RTrim(m_DataTable.Rows(m_IntRowPosition).Item(1)) <> lstScannedNames.Items.Item(intLoopCounter) Then
unique = True
lstScannedNames.SelectedIndex = intLoopCounter
intLoopCounter = lstScannedNames.Items.Count - 1
End If
Next intLoopCounter
If (unique) = True Then
Dim drNewRow As DataRow = m_DataTable.NewRow()
drNewRow("emp_id") = m_DataTable.Rows.Count()
drNewRow("emp_name") = lstScannedNames.Items.Item(lstScannedNames.SelectedIndex)
m_DataTable.Rows.Add(drNewRow)
Else
unique = True
End If
Next m_IntRowPosition
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End If
SomeWhereElse:
m_DA.Update(m_DataTable)
MessageBox.Show("Operation Completed")
End Sub
The area of focus is the ElseIf statement. When I run this code and there are already records in the database, it adds the first name that is in the list and not in the datatable, to the datatable. It then adds the first name from the list to a new record, for each item in the list, which is about 20 times but it depends on the list length. I've been trying different things and I know I'm close but I've been stuck for a while.
Reverse your logic. Loop over the items in the listbox and check if they are present in the datatable
....
For intLoopCounter = 0 To lstScannedNames.Items.Count - 1
Dim name = lstScannedNames.Items.Item(intLoopCounter)
Dim rowExist = m_DataTable.Select("emp_name = '" & name & "'")
if rowExist Is Nothing OrElse rowExist.Length = 0 Then
Dim drNewRow As DataRow = m_DataTable.NewRow()
drNewRow("emp_id") = m_DataTable.Rows.Count()
drNewRow("emp_name") = name
m_DataTable.Rows.Add(drNewRow)
End If
Next m_IntRowPosition
...
Using the DataTable.Select method avoids to write an explicit loop to find the duplicate. And you can directly add the row when you are certain that there is no duplicate
Protected Sub grdView_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles grdView.PageIndexChanging
grdView.SelectedIndex = -1
grdView.PageIndex = e.NewPageIndex
' To persist DDL values at paging using datatable
Dim Data As New DataTable
Data.Columns.Add("RowIndex", Type.GetType("System.Int32"))
Data.Columns.Add("SelectedValue", Type.GetType("System.String"))
For Each Row As GridViewRow In grdView.Rows
Dim ddl As DropDownList = DirectCast(Row.FindControl("ddlSample"), ComboBox)
If ddl.SelectedValue <> "" Then
Dim Rows As DataRow = Data.NewRow
Rows("RowIndex") = Row.RowIndex
Rows("SelectedValue") = ddl.SelectedValue
Data.Rows.Add(Rows)
End If
Next
' Passing the datatable to session
Session("MYDataTable") = Data
grdView.DataSource = grdDataSource
grdView.DataBind()
If Session("MYDataTable") IsNot Nothing AndAlso Session("MYDataTable") IsNot DBNull.Value Then
Data = CType(Session("MYDataTable"), DataTable)
For Each row In Data.Rows
If e.NewPageIndex = row("PageIndex") Then
Dim ddl As DropDownList = DirectCast(grdView.Rows(row("RowIndex")).FindControl("ddlSample"), DropDownList)
ddl.SelectedValue = row("SelectedValue")
End If
Next
End If
The above Code is the code I'm having for Persisting DropDownList value but it is not working as expected.
In my GridView i have two Columns
Details of First column : I have list of employee names
Details of Second Column : It has a dropDownlist in each row with many items in it.
In my grid i have nearly 100 values, So i have set allowPaging="true" and have set pagesize="10".
The problem what I'm facing is
Step 1 : I'm selecting a value in dropdownlist of rowindex 2 in page 0
Step 2 : changing the page to 5, the value I selected in page 0 for the rowindex 2 also appears in the page 5 for the rowindex 2 as per my code.
But what i want is
Step 1 : I'm selecting a value in dropdownlist of rowindex 2 in page 0
Step 2 : changing the page to 5, no value should be selected as i haven't selected any values in it.
Step 3 : I i go back again to Page 0 the value of ddl in Rowindex 2 should have the previously selected value.
EDIT
I'm peristing the Values in Dictionary as mentioned here..!!
Dim ddlValues As New Dictionary(Of String, Integer)
For rowIndex As Integer = 0 To grdOnlineVoter.Rows.Count
Dim ddl As ComboBox = grdView.Rows(rowIndex).FindControl("ddlsample")
If ddl IsNot Nothing Then
If ddl.SelectedIndex > 0 Then
Dim ddlIndex As Integer = rowIndex
ddlValues.Add(ddl.SelectedValue, ddlIndex)
End If
End If
Next
But i'm unable to repopulate it to the Dropdownlist
Alright, based on what you have displayed here I have reached this conclusion.
You are saving the values to the Dictionary to the specific Row number. That is 0-9. However, row numbers does not take pagenumber into consideration. So I recommend you to do the following: (Change is on row 6)
Dim ddlValues As New Dictionary(Of String, Integer)
For rowIndex As Integer = 0 To grdOnlineVoter.Rows.Count
Dim ddl As ComboBox = grdView.Rows(rowIndex).FindControl("ddlsample")
If ddl IsNot Nothing Then
If ddl.SelectedIndex > 0 Then
Dim ddlIndex As Integer = rowIndex + (GridView1.PageIndex * 10)
ddlValues.Add(ddl.SelectedValue, ddlIndex)
End If
End If
Next
Now page 1 will have values: 0-9
Page 2 will have values 10-19 etc.
Hey guys, so I am creating a List(Of String), always of size 9.
This list contains True/False values. I need to go through this list and find the 3 values that are True (will never be more than 3, but could be less) and then set 3 string values in my code to the 3 index's of those values + 1.
Here is my current code:
Private Sub SetDenialReasons(ByVal LoanData As DataRow)
Dim reasons As New List(Of String)
With reasons
.Add(LoanData.Item("IsDenialReasonDTI").ToString)
.Add(LoanData.Item("IsDenialReasonEmploymentHistory").ToString)
.Add(LoanData.Item("IsDenialReasonCreditHistory").ToString)
.Add(LoanData.Item("IsDenialReasonCollateral").ToString)
.Add(LoanData.Item("IsDenialReasonCash").ToString)
.Add(LoanData.Item("IsDenialReasonInverifiableInfo").ToString)
.Add(LoanData.Item("IsDenialReasonIncomplete").ToString)
.Add(LoanData.Item("IsDenialReasonMortgageInsuranceDenied").ToString)
.Add(LoanData.Item("IsDenialReasonOther").ToString)
End With
Dim count As Integer = 0
For Each item As String In reasons
If item = "True" Then
count += 1
End If
Next
If count = 1 Then
DenialReason1 = (reasons.IndexOf("True") + 1).ToString
ElseIf count = 2 Then
DenialReason1 = (reasons.IndexOf("True") + 1).ToString
DenialReason2 = (reasons.LastIndexOf("True") + 1).ToString
ElseIf count >= 3 Then
Dim tempIndex As Integer = reasons.IndexOf("True")
DenialReason1 = (reasons.IndexOf("True") + 1).ToString
DenialReason2 = (reasons.IndexOf("True", tempIndex, reasons.Count - 1) + 1).ToString
DenialReason3 = (reasons.LastIndexOf("True") + 1).ToString
End If
End Sub
I had 3 True's next to each other in the array and the code failed with an exception saying count must be positive or something.
Now if there are less than 3 True's, it should set the remaining DenialReason's that haven't been set yet as blank (however they are set as blank in the constructor already to account for this).
Any ideas?
Perhaps you could modify your For Each code to handle the assignment of the DenialReasons. This still feels like a hack, but I think it may be cleaner that what you have. If you use this code, you don't need the code that begins with If count = 1...:
Dim count As Integer = 0
Dim index As Integer = 1
For Each item As String In reasons
If item = "True" Then
count += 1
Select Case count
Case 1
DenialReason1 = index.ToString()
Case 2
DenialReason2 = index.ToString()
Case 3
DenialReason3 = index.ToString()
End Select
End If
index += 1
Next
The index variable above assumes a 1-based index. I think this is cleaner than using IndexOf().
I think a better solution might be to have a list of DenialReasons and add to that list as items are true:
Dim count As Integer = 0
Dim index As Integer = 1
Dim denialReasons As New List(Of String)()
For Each item As String In reasons
If item = "True" Then
denialReasons.Add(index)
End If
index += 1
Next
Then you can simply iterate through your list of denialReasons. This is flexible so that if, for whatever reason, you have more than three DenialReasons, you don't have to add another hard-coded variable.