数据库我正在为我的应用程序连接"%"列使用小于1的值(即15%在数据库中存储为0,15),所以我使用格式=" .00%"在DataGrid列中显示值。
问题是如何正确地让用户插入大于1的值 - 在单元格中写入50或50% - 并在数据库中将其转换为0.5。
我正在寻找DataGridViewCellValidatingEventHandler并添加了事件levDGV_PercentCellValidating,但我不知道如何更改值......
private void levDGV_PercentCellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
string dataPropertyName = levDGV.Columns[e.ColumnIndex].DataPropertyName; // deliveryProcent
// Abort validation if cell is not in the "deliveryProcent" column.
if (!dataPropertyName.Equals("deliveryProcent")) return;
levDGV.Rows[e.RowIndex].ErrorText = "";
string testValue = e.FormattedValue.ToString();
char lastChar = testValue[testValue.Length - 1];
if (lastChar == (char)37)
{
//TODO: Remove % character from entered string
}
//TODO: divide the value by 100 and set as currentCell value
}
如何将输入的值转换为经过适当验证的值(除以100)?
答案 0 :(得分:0)
为其他人留下解决方案,解析似乎对我有用:
private void levDGV_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
string dataPropertyName = levDGV.Columns[e.ColumnIndex].DataPropertyName;
if (dataPropertyName.Equals("deliveryProcent"))
{
if (e.Value != null)
{
try
{
string testValueStr = e.Value.ToString();
char lastChar = testValueStr[testValueStr.Length - 1];
if (lastChar == (char)37)
{
//Remove % character from entered string
testValueStr = testValueStr.Substring(0, testValueStr.Length - 1);
}
//divide the value by 100 and set as currentCell value
double testValue = (Convert.ToDouble(testValueStr)) / 100;
e.Value = testValue;
e.ParsingApplied = true;
}
catch (FormatException)
{
// Set to false in case another CellParsing handler
// wants to try to parse this DataGridViewCellParsingEventArgs instance.
e.ParsingApplied = false;
}
}
}
}