gpt4 book ai didi

c# - DataGridView 使用 DataSourceNullValue : enter either nothing, 或 nullValue

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:29 25 4
gpt4 key购买 nike

我有一个 DataGridView,它数据绑定(bind)到一个 DataSource。

DataGridView 的其中一个列绑定(bind)到 decimal 类型的属性? (可为空的十进制)。此列是可编辑的。

如果属性值为 null,则底层对象应使用从另一个对象检索到的默认值。

如果此列什么都不显示,运算符(operator)似乎会感到困惑。因此我想显示字符串“<default>”,表示使用了默认值。

当编辑这个值时,我想给运算符(operator)留下一个空字段或输入类似“<default>”的自由,如果他想表明应该使用默认值。所以:

  • 运算符输入字符串“<default>”:属性获取值(十进制?)null
  • 运算符输入空字符串:属性获取值(十进制?)null
  • 运算符(operator)输入任何其他值:验证它是否为小数,并使用该值。

我试过的(这总是第一个被要求的:-)

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com