答案 0 :(得分:0)
您未指定平台。
如果是 WinForms :
通过点击或其他任何方式添加未绑定的列:
var column = new DataGridViewColumn(new DataGridViewTextBoxCell());
column.Name = "Calculated Column";
column.HeaderText = "Calculated Column";
dataGridView1.Columns.Add(column);
添加datagridview.cellformatting
的处理private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Calculated Column")
{
var data = (DataObj)dataGridView1.Rows[e.RowIndex].DataBoundItem;
e.Value = GetCalculatedColumnValue(data);
e.FormattingApplied = false;
}
}
在列
中添加输入数据的处理private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Calculated Column")
{
var data = (DataObj)dataGridView1.Rows[e.RowIndex].DataBoundItem;
SetCalculatedColumnValue(data, e.Value);
}
}
P.S。我的情况我使用DataObj作为数据有界对象
public class DataObj
{
public float Price { get; set; }
public int Amount { get; set; }
}
如果您只需要显示外部数据(只读列)(不需要p3)