1 Solution
It's pretty easy to loop through the GridView's rows and get the value from your ID column:
[C#]for (int i = 0; i < gvCustomerContacts.DataRowCount; i++) { object ColumnID = gvCustomerContacts.GetRowCellValue(i, "ID_Column_Name"); if (ColumnID != null && ColumnID != DBNull.Value) MessageBox.Show("This row already exists"); else MessageBox.Show("This row does NOT exist"); }
See also:
* Traversing Rows: http://documentation.devexpress.com/#WindowsForms/CustomDocument742
* Obtaining & Setting Cell Values: http://documentation.devexpress.com/#WindowsForms/CustomDocument753
Update
I've reviewed your code and found out the following line of code:
gvTraps.SetFocusedRowCellValue
Why are you setting the focused row value? It seems, it is better to use the SetRowCellValue method, which will allow you to specify which row value should be set. Also, to save value in the underlying DataSource, use the GridView's UpdateCurrentRow method. Does this help? Apart from this, I do not see other problems in your code.
-
Thanks, Brendon . . .
My looping is working perfectly. But as it loops, I'm attempting to update 2 columns (Trap_Icn, ID_Number) from sequence generators and saving it to the database. It IS saving the entries, but not correctly. I've been testing by adding 2 entries at a time; it's only the 2nd entry that's getting the Trap_Icn and ID_Number updated, but it's with the wrong numbers.
I'm attaching a document for you to hopefully see what I'm attempting.
So, I guess my question is what would be the syntax for saving as I'm looping . . . or is this possible?
-
I've updated the answer, Doris
-
Thanks, Plato . . . I took your suggestions and it is working now. I'm showing you my end code because I converted the [C#] from Brendan to vb.net and I'm not sure if something didn't quite work in the conversion, but it was setting the gridview.DataRowCount down 1 and it appeared to not be finishing the loop for the last record. Anyway, thanks to you and Brendan for your patience and expertise . . .
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
For i As Integer = 0 To gvTraps.DataRowCount
Dim wsID As Object = gvTraps.GetRowCellValue(i, "ID_NUMBER")
Dim wsDivision As Object = gvTraps.GetRowCellValue(i, "DIVISION")If wsID IsNot Nothing AndAlso wsID IsNot DBNull.Value Then
'MessageBox.Show("This row already exists")
Else
'MessageBox.Show("This row does NOT exist")
If wsDivision IsNot Nothing AndAlso wsDivision IsNot DBNull.Value Then
m_TrapIcn = CtrlMgmt.Get_Nextval("trap_seq", My.Settings.CSAnimal)
If wsDivision = "FAIRBORN" Then
m_TrapID = CtrlMgmt.Get_Nextval("id_trap_fbrn_seq", My.Settings.CSAnimal)
Else
m_TrapID = CtrlMgmt.Get_Nextval("id_trap_xenia_seq", My.Settings.CSAnimal)
End If
gvTraps.SetRowCellValue(i, "TRAP_ICN", m_TrapIcn)
gvTraps.SetRowCellValue(i, "ID_NUMBER", m_TrapID)
gvTraps.UpdateCurrentRow()
End If
End If
Next
End Sub -
Doris, in VB, you should use the following code in the loop:
......... For i As Integer = 0 To gvTraps.DataRowCount - 1 .........
-
Plato . . .
I made the change as suggested., BUT it doesn't appear to process the last row. AND it's not saving
the new rows to the database. will 'UpdateCurrentRow' insert a new row into the table?Thanks for your patience.
-
Plato . . .
I added the following and it appears to be working now.
DTATraps.Update(DSTraps)
So I think it's working as needed. Again, thanks for your patience and expertise.
-
Plato . . . one last comment.
I was still having trouble with saving the last entered row. I think it's because until the row is exited, the system doesn't recognize that it's a row. So this is the code that I'm using and it's working perfectly for me now.
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim priorRow As Integer = gvTraps.DataRowCount ' get total number of rows
bsDTTraps.Position = priorRow - 1 ' reposition on a prior row to exit the last new row
For i As Integer = 0 To gvTraps.DataRowCount - 1 ' loop thru gridview
Dim wsID As Object = gvTraps.GetRowCellValue(i, "ID_NUMBER")
Dim wsDivision As Object = gvTraps.GetRowCellValue(i, "DIVISION")If wsID IsNot Nothing AndAlso wsID IsNot DBNull.Value Then
'MessageBox.Show("This row already exists")
Else
'MessageBox.Show("This row does NOT exist")
If wsDivision IsNot Nothing AndAlso wsDivision IsNot DBNull.Value Then
m_TrapIcn = CtrlMgmt.Get_Nextval("trap_seq", My.Settings.CSAnimal)
If wsDivision = "FAIRBORN" Then
m_TrapID = CtrlMgmt.Get_Nextval("id_trap_fbrn_seq", My.Settings.CSAnimal)
Else
m_TrapID = CtrlMgmt.Get_Nextval("id_trap_xenia_seq", My.Settings.CSAnimal)
End If
gvTraps.SetRowCellValue(i, "TRAP_ICN", m_TrapIcn)
gvTraps.SetRowCellValue(i, "ID_NUMBER", m_TrapID)
gvTraps.UpdateCurrentRow()End If
DTATraps.Update(DSTraps)
End If
Next
bsDTTraps.Position = priorRow ' reposition on last new row
End SubOnce again, thanks for all of your patience . . .
-
Hi Doris,
I am glad to hear that you have found the solution and shared it with the community...
Please feel free to contact us in case of any difficulties. We are happy to help you at any time.
Is your intention to post an answer to your own question?
- If so, then proceed.
- If you simply wanted to post additional information, ask for further clarification, or to just say "Thanks!", please click Leave a Comment.
- If you wish to edit your original question, please use the Edit button in the Toolbox at the top right corner of that entry.
Facebook
Twitter
Google+