Hi all.
I am trying to allow the editing of Hex values in a datagridview cell.
The datagridview is bound to a list of objects which have a uint16 parameter, say "Address".
I can get the datagridview cell to *display* the uint16 values as hex by simply setting the CellStyle.Format string = "X04".
column =
newDataGridViewTextBoxColumn();column.DataPropertyName =
"Address";column.Name =
"PAddress";column.HeaderText =
"Address(H)";column.DefaultCellStyle.Format =
"X04";dataGridView1.Columns.Add(column);
However, the default dgv cell behaviour does not allow hex value strings to be entered by the user.
trying to enter say "FF" in the causes a "Input string not in correct format" exception, as the dgv tries to parse the input as a non-hex string.
So my question is this:
How do I modify the cell/dgv events to allow entry of hex format strings?
I have tried handling the CellParsing event, but this does not seem to override the default/initial parsing, as I still get the format exceptions from the dgv.
private
void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e){
if (dataGridView1.Columns[e.ColumnIndex].Name == "PAddress")
{
if (e != null&& e.Value != null&& e.DesiredType.Equals(typeof(UInt16)))
{
try
{
/// Convert to a hex value
Int16.Parse((string)e.Value, System.Globalization.NumberStyles.AllowHexSpecifier);e.Value =
e.ParsingApplied =
true;}
catch
{
MessageBox.Show("Input not a hex value");
}
}
}
}
TIA,
Simon.