- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 DataGridView,它数据绑定(bind)到一个 DataSource。
DataGridView 的其中一个列绑定(bind)到 decimal 类型的属性? (可为空的十进制)。此列是可编辑的。
如果属性值为 null,则底层对象应使用从另一个对象检索到的默认值。
如果此列什么都不显示,运算符(operator)似乎会感到困惑。因此我想显示字符串“<default>
”,表示使用了默认值。
当编辑这个值时,我想给运算符(operator)留下一个空字段或输入类似“<default>
”的自由,如果他想表明应该使用默认值。所以:
<default>
”:属性获取值(十进制?)null我试过的(这总是第一个被要求的:-)
public MyForm() // constructor
{
this.InitializeComponent();
// initialize datagridview (part of this could have been done in designer)
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.DataSource = ...;
var valueCellStyle = this.columnNonDefault.DefaultCellStyle;
valueCellStyle.DataSourceNullValue = (decimal?)null;
valueCellStyle.NullValue = "<default>";
}
验证事件处理程序如下:
private void OnCellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.CollumnIndex == this.columnNonDefaultValue.Index)
{ // validating the non-default value
// allow either empty string, or cellStyle.NullValue or decimal
string proposedValue = (string)e.FormattedValue;
// check if empty:
if (String.IsNullOrEmpty(proposedValue)
{ // accept empty:
e.Cancel = false;
}
else
{ // check if "<default>"
var cellToValidate = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
DataGridViewCellStyle style = cellToValidate.InheritedStyle;
if (!style.IsNullValueDefault
&& String.Equals(proposedValue, style.NullValue.ToString()))
{ // accept default value:
e.Cancel = false;
}
else if (this.IsProperDecimal(proposedValue)
{ // accept decimal value
e.Cancel = false;
}
else
{ // all others: don't accept
this.ShowInvalidData(...)
e.Cancel = true;
}
}
}
}
如果人们输入一个正确的十进制值,或者“<default>
”,这会很好地工作。如果输入空字符串,那么一旦数据被推送到数据绑定(bind)项,我就会得到一个字符串格式异常。
我原以为我应该处理 CellValuePushed 事件,VirtualMode = true。 las,因为 GridView 是数据绑定(bind)的,所以不会调用此事件,即使推送了适当的值也不会调用
那么我可以使用什么来接受“”和空字符串作为(十进制?)null?
最佳答案
作为一个选项,您可以使用 CellParsing
和 CellFormatting
处理输入值到数据源值之间的转换并以合适的格式显示数据源值的事件。
CellParsing
:将输入的文本转换为合适的数据源值。
此处您要根据某些条件将输入的值转换为默认值(当输入的文本为空或 ""
或等于默认值时)。
CellFormatting
:将数据源值转换为合适的文本以显示在单元格中。
在这里你想把你的默认值转换成一些像<Default>
这样的文本。 .
这是一个例子:
public class Test
{
public int X { get; set; }
public decimal? Y { get; set; }
}
BindingList<Test> list;
decimal? defaultValue = 100; //Some default value
string defaultFormattedValue = "<Default>"; //Some default formatted value
private void Form1_Load(object sender, EventArgs e)
{
list = new BindingList<Test>(new List<Test>());
this.dataGridView1.DataSource = list;
}
private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
var value = string.Format("{0}", e.Value);
if (value == string.Empty || value == "\"\"")
e.Value = defaultValue;
e.ParsingApplied = true;
}
private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (e.Value is int? && (int?)e.Value == defaultValue)
e.Value = defaultFormattedValue;
}
关于c# - DataGridView 使用 DataSourceNullValue : enter either nothing, 或 nullValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45729547/
我有一个 DataGridView,它数据绑定(bind)到一个 DataSource。 DataGridView 的其中一个列绑定(bind)到 decimal 类型的属性? (可为空的十进制)。此
我是一名优秀的程序员,十分优秀!