Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to delete cell value in datagrid

Status
Not open for further replies.

HRHK

Programmer
Jun 5, 2005
83
US
I cannot delete a cell value in the datagrid using the backspace key.
I have to use Ctrl+0(zero) to null the value. Is there easier
way to fix this? I also tried overriding the base class member(below)
but it doesn't work. Any help appreciated very much.

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

If msg.WParam.ToInt32() = CInt(Keys.Enter) Then

SendKeys.Send("{Tab}")

Return True

End If

If msg.WParam.ToInt32() = CInt(Keys.Back) Then

SendKeys.Send("{ControlKey + 0}")

Return True

End If

Return MyBase.ProcessCmdKey(msg, keyData)

End Function 'ProcessCmdKey
 
I'm glad you asked that question because I was not previously aware of the Ctrl+0 feature.

I assume you mean that when you leave a DataGrid cell from which you have deleted the entry, that it goes back to it's original value but instead you want it to enter a null value (This may vary depending on the column data, if it contains a string then it does leave it empty i.e. "" so I assume your value is numeric.

Assuming that you still need the backspace and delete keys to work normally when editing within the cell it might be best to overide the Commit function.

I had to do something similar when I needed the empty cell to force a specific value into the database but this should still do what you want if you just change it to let your values go through unmodified.

I did it based on the example given here:


So you end up with the critical part something like:

Class CRTextBoxColumn
Inherits DataGridTextBoxColumn

Protected Overrides Function Commit(ByVal cm As CurrencyManager, ByVal RowNum As Integer) As Boolean
'
' Parse the data and write to underlying record.
'
Me.HideEditBox() ' return focus to the DataGrid control
Dim box As DataGridTextBox = CType(Me.TextBox, DataGridTextBox), Value As Decimal 'Decimal, Integer or whatever
' Do not write data if not editing.
If box.IsInEditOrNavigateMode Then Return True
If TextBox.Text = "" Then ' in this example, "" maps to DBNull
SetColumnValueAtRow(cm, RowNum, DBNull.Value)
Else
' Parse the data.
Try
Value = Decimal.Parse(TextBox.Text) 'Decimal, _ Integer or whatever
Catch
Return False ' Exit on error and display old "good" value.
End Try
SetColumnValueAtRow(cm, RowNum, Value) ' Write new value.
End If
Me.EndEdit() ' Let the DataGrid know that processing is completed.
Return True ' success
End Function

End Class

There is probably a much easier way because I am only just starting to get the DataGrid to bend to my will but I thought I'd mention it since none of the experts have replied.

Actually the way I've struggled with the DataGrid so far I'm suspecting that the experts are just using something far better (that I can't afford).
 
Actually the way I've struggled with the DataGrid so far I'm suspecting that the experts are just using something far better (that I can't afford).

Indeed.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top