Hi all,
i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps:
1. i have a datagridview with two columns (LoginName,UserName)
2. the datagridview sorting is set to automatic, so when i click on the column header is sorts well.
3. i put in an event handler for the CellEndEdit Event, so whenever the user of the program changes the content of a cell in the LoginName column - the event handle code - checks if there is a similar name in the login names, or if the program user had input an empty string (Null) instead of a valid name, if both checks passed the user table is updated with the new login name.
4. because this process should be done before passing the cursor to a new cell in the grid, i keep the value of the row where the cell under checking belongs in a text box, lets say row (3), and i do all the checks in the beginning of the CellEndEdit Event refering to the text box row number, if everything is ok, the code first applies the update to the table and then changes the text box value to the current cell value.
5. MY PROBLEM IS, once i change the cell value, the grid sorting takes effect immediatly giving me wrong references.
Please see my Code below , i think it is difficult to descibe.
' DGusers1 IS MY DATAGRIDVIEW
' I PUT IN THIS EVENT HANDLE TO STOP THE DATAGRID SORTING BUT IT DOESNOT WORK
Private Sub DGUsers1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventAr gs) Handles DGUsers1.CellBeginEdit
DGUsers1.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
DGUsers1.Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable
End Sub
Private Sub DGUsers1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGUsers1.CellEndEdit
Dim x As Integer
Dim xx As String
Dim xy As String
Dim xz As Integer
Dim xz1 As Object
Dim xz2 As Object
Dim xz3 As Integer
Dim xz4 As Object
Dim xy4 As Object
Dim xz5 As Integer
Dim vx As Integer
' I AM TRYING HERE TO STOP THE DATAGRID SORTING ALSO BUT IT DOESNOT WORK
DGUsers1.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
DGUsers1.Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable
' HERE I GET THE VALUE OF THE DATAGRIDROW -FOR THE CELL UNDER CHECKING -FROM A TEXT BOX NAMED DGusersrowid. THE CELL UNDER CHECKING IS THE CELL I HAVE JUST LEFT TO CALLTHIS PROCEDURE.
'HERE IS MY PROBLEM
Dim Valx As Object
Dim Valy As Object
Valx = DGUsers1.Rows(DGusersRowID.Text).Cells(1).Value
Valy = DGUsers1.Rows(DGusersRowID.Text).Cells(2).Value
' THE VALX AND VALY VALUES REFLECTS THE VALUES OF THE LOGIN NAME AND USER NAME . ONCE I LEAVE THE CELL AFTER CHANGING ITS VALUE, AND BEFORE THIS SUB RUNS, THE DATAGRID SORTS ITSELF , SO I GET THE VALUE OF THE LOGIN NAME AT ROW REPRESENTING DGusersRowID.Text BUT AFTER THE SORTING HAD TOOK EFFECT.
IN BRIEF, I NEED THE SORTING BUT ON THE OTHER HAND I GET WRONG VALUES.
OleDbConnection1.Open()
OleDbDataAdapter1.UpdateCommand.CommandText = "UPDATE USERS SET LOGINAME = '" & LogName.Text & "', USERNAME = '" & Valy & "', EPROFID = " & ComboBox1.SelectedValue & ", EMAIL = '" & Email.Text & "', PHONE = '" & Phone.Text & "' WHERE (USERID = " & UserID.Text & ")"
OleDbDataAdapter1.UpdateCommand.ExecuteNonQuery()
OleDbConnection1.Close()
'HERE I CHANGE THE TEXTBOX CELL VALUE TO THE CURRENT CELL VALUE
x = DGUsers1.CurrentCell.RowIndex
DGusersRowID.Text = x
xx = DGUsers1.Rows(x).Cells(1).Value
Dim mychkxx As Boolean
mychkxx = IsDBNull(xx)
If mychkxx = True Then
DGUsers1.Rows(x).Cells(1).Value = LogName.Text
xx = DGUsers1.Rows(x).Cells(1).Value
End If
xy = DGUsers1.Rows(x).Cells(2).Value
mychkxx = IsDBNull(xy)
If mychkxx = True Then
DGUsers1.Rows(x).Cells(2).Value = UserName.Text
xy = DGUsers1.Rows(x).Cells(2).Value
End If
xz = DGUsers1.Rows(x).Cells(0).Value
xz1 = DGUsers1.Rows(x).Cells(5).Value
xz2 = DGUsers1.Rows(x).Cells(6).Value
xz3 = DGUsers1.Rows(x).Cells(4).Value
If IsDBNull(xz1) = True Then
xz1 = ""
End If
If IsDBNull(xz2) = True Then
xz2 = ""
End If
' HERE I FILL UP SOME OTHER TEXT OXES WITH THE NEW VALUES
LogName.Text = xx
UserName.Text = xy
UserID.Text = xz
Email.Text = xz1
Phone.Text = xz2
ComboBox1.SelectedValue = xz3
DupMsg.Text = 0
GetData()
start:
DsUserEdit1.Tables.Remove("LNDupVal")
Beg:
End Sub