DataColumnChangeEvent

  • Thread starter Thread starter JT
  • Start date Start date
J

JT

Hi,
I have the sub below to handle the DataColumnChange Event
in a datagrid on a Windows Form. I want it to reject any
attempted user changes in the InsID column if the Primary
Key (e.Row(0)) of the datarow is greater than 0 (ie, from
the database and not autogenerated. It seems to work, but
it also shows the message box, which I truncated here,
even if the column selected for change is not the InsID
column. Any thoughts on what I am doing wrong? Also, is
there a better way to specify my column other than
with .ColumnName? Thanks.

JT


Private Shared Sub InsIDColumn_Changed(ByVal sender As
Object, ByVal e As DataColumnChangeEventArgs)

With e
If .Column.ColumnName = "InsID" AndAlso CInt(.Row(0)) >
0 Then
.Row.RejectChanges()
MessageBox.Show
End If
End With
End Sub
 
Hi JT,

I've tried your code on my machine, however, I could not reproduce it.
Please try to set a breakpoint on the if statement and use watch window to
check the value of both e.Column.ColumnName and e.Row(0) to see if the meet
the criteria.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi JT,

Try throwing an Exception within ColumnChanging event if you don't want
modifications.
 
Hi Kevin,
I think I found the problem, but could use some help with
the solution. Column 0 of my datagrid is a
datagridcomboboxcolumn. Some of its code is below:

Protected Overloads Overrides Sub Edit(ByVal Source As
CurrencyManager, ByVal Rownum As Integer, ByVal Bounds As
Rectangle, ByVal [ReadOnly] As Boolean, ByVal InstantText
As String, ByVal CellIsVisible As Boolean)

Try
Combo.Text = String.Empty

Dim OriginalBounds As Rectangle =
Bounds

OldVal = Combo.Text

If CellIsVisible Then
Bounds.Offset(xMargin,
yMargin)
Bounds.Width -= xMargin * 2
Bounds.Height -= yMargin
Combo.Bounds = Bounds
Combo.Visible = True
Else
Combo.Bounds =
OriginalBounds
Combo.Visible = False
End If

Combo.SelectedValue =
GetColumnValueAtRow(Source, Rownum)

If Not InstantText Is Nothing Then
Combo.SelectedValue =
InstantText
End If

Combo.RightToLeft =
DataGridTableStyle.DataGrid.RightToLeft
Combo.Focus()

If InstantText Is Nothing Then
Combo.SelectAll()
Else
Dim [End] As Integer =
Combo.Text.Length
Combo.Select([End], 0)
End If

If Combo.Visible Then

DataGridTableStyle.DataGrid.Invalidate
(OriginalBounds)
End If

InEdit = True
Catch ex As FormatException
MessageBox.Show(ex.Message & " " &
ex.ToString & " " & ex.Source)
End Try
End Sub

Protected Overrides Sub SetColumnValueAtRow(ByVal [source]
As System.Windows.Forms.CurrencyManager, ByVal rowNum As
Integer, ByVal value As Object)

Dim s As Object = value
Try
Dim dt As New DataTable
dt = CType(Combo.DataSource,
DataTable)
Dim dv As DataView = New DataView
(dt)
dv.Sort = dt.Columns(0).ColumnName
Dim intInd As Integer = dv.Find
(Combo.SelectedValue)
If intInd <> -1 Then
s = dv(intInd)(dt.Columns
(0).ColumnName)
Else
s = DBNull.Value
End If

MyBase.SetColumnValueAtRow
([source], rowNum, s)
Catch ex As InvalidCastException
MessageBox.Show(ex.Message & " " &
ex.ToString)
End Try
End Sub

Private Function GetTxt(ByVal s As Object) As
String

If s Is System.DBNull.Value Then Return
NullText

'Create datatable from combobox's
Datasource
Dim dt As New DataTable
dt = CType(Combo.DataSource, DataTable)
'Create DataView to allow sorting
Dim dv As DataView = New DataView(dt)
'Sort on Value Member
dv.Sort = dt.Columns(0).ColumnName
'Return DataRow Index of Value Member 's'
Dim intInd As Integer = dv.Find(s)
If intInd <> -1 Then 'Value found
'Return associated Display Member
from Column 1 of DataRow
Return dv(intInd)(dt.Columns
(1).ColumnName).ToString
Else 'Value NOT found
Return String.Empty
End If

End Function

As you can see, the SetColumnValueAtRow Sub does a sort of
a dataview using Column(0).ColumnName. This is why, when
I did what you suggested and stepped through the code, the
columnName was "InsID". This happens the FIRST time I
make a change in ANY column in a row. The SECOND time I
make a change in any column other than column(0)
("InsID"), the columnName property comes up correctly.
Any suggestions how to work around this and keep my
comboboxcolumn functionality? Thanks.
JT
 
Hi JT,

Are your binding the Combobox to a data source? If so, I think you can get
the position of the current row with the CurrencyManger. Please try to
check the following link for reference:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemWindowsFormsCurrencyManagerClassTopic.asp

If If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top