C# 如何在C语言中生成两个datagridview之间的公式#
如何连接两个数据网格视图,以便自动更新它们的数据。例如,当DataGridView1中的Columb1增加时,DatagridView2中的Columb2增加。分享我的想法,以实现一个解决方案,其中更改单元格中的一个值会更新另一个值 首先,创建一个类似CellRelation的类-其目的是在两个网格单元之间建立关系C# 如何在C语言中生成两个datagridview之间的公式#,c#,mysql,.net,visual-studio,visual-studio-2010,C#,Mysql,.net,Visual Studio,Visual Studio 2010,如何连接两个数据网格视图,以便自动更新它们的数据。例如,当DataGridView1中的Columb1增加时,DatagridView2中的Columb2增加。分享我的想法,以实现一个解决方案,其中更改单元格中的一个值会更新另一个值 首先,创建一个类似CellRelation的类-其目的是在两个网格单元之间建立关系 class CellRelation { DataGridCell SourceCell; DataGridCell DestinationCell;
class CellRelation
{
DataGridCell SourceCell;
DataGridCell DestinationCell;
Func<DataGridCell, DataGridCell, decimal> Formula;
}
类单元关系
{
DataGridCell-SourceCell;
DataGridCell DestinationCell;
Func公式;
}
第二,初始化
工作样本 我制作了一个简短的工作示例(没有数据集)来演示我的方法。 我制作了一个datagridview,其中有一行和三列—总计、售出和剩余 因此,每次对销售单元进行更改时,剩余的项目都会得到更新 我用一个网格制作,但同样可以扩展到两个网格。 它有很大的改进空间,尤其是表达式部分,理想情况下,它应该能够计算表达式树
class CellRelation
{
public DataGridViewCell SourceCell;
public DataGridViewCell DestinationCell;
public CellFormula Formula;
}
class CellFormula
{
public Func<DataGridViewCell, DataGridViewCell, decimal> Operator;
public DataGridViewCell Operand1;
public DataGridViewCell Operand2;
public decimal Evaluate()
{
return Operator(Operand1, Operand2);
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<CellRelation> cellRelations = new List<CellRelation>();
private void Initialise_Click(object sender, EventArgs e)
{
var soldCell = this.dataGridView1[1, 0];
var remainingCell = this.dataGridView1[2, 0];
var totalCell = this.dataGridView1[0, 0];
// datagid values --- In your case this is from a dataset
totalCell.Value = 10;
soldCell.Value = 0;
remainingCell.Value = 10;
// initialise the relation / formula
CellRelation relation = new CellRelation();
relation.SourceCell = soldCell;
relation.DestinationCell = remainingCell; // thats the dependent cell
relation.Formula = new CellFormula();
// here is a sample of subtraction formula : Subtracting Sold items for total items
relation.Formula.Operator = new Func<DataGridViewCell, DataGridViewCell, decimal>((p, v) => { return ((decimal.Parse(p.Value.ToString()))) - ((decimal.Parse(v.Value.ToString()))); });
relation.Formula.Operand1 = totalCell;
relation.Formula.Operand2 = soldCell;
cellRelations.Add(relation);
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//look up if there is an destination cell for the cell being updated
var cellReln = cellRelations.FirstOrDefault(item => item.SourceCell.RowIndex == e.RowIndex && item.SourceCell.ColumnIndex == e.ColumnIndex);
if (cellReln != null)
{
cellReln.DestinationCell.Value = cellReln.Formula.Evaluate();
}
}
类单元关系
{
公共DataGridViewCell SourceCell;
公共DataGridViewCell DestinationCell;
公共细胞公式;
}
类细胞公式
{
公共函数运算符;
公共DataGridViewCell操作数1;
公共DataGridViewCell操作数2;
公共十进制求值()
{
返回运算符(操作数1、操作数2);
}
}
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
列表单元格关系=新列表();
私有无效初始化\u单击(对象发送方,事件参数e)
{
var soldCell=this.dataGridView1[1,0];
var remainingCell=this.dataGridView1[2,0];
var totalCell=this.dataGridView1[0,0];
//datagid值——在您的例子中,这是一个数据集
totalCell.Value=10;
soldCell.Value=0;
剩余单元格。值=10;
//初始化关系/公式
CellRelationship=新的CellRelationship();
relation.SourceCell=soldCell;
relation.DestinationCell=remainingCell;//这是依赖单元格
relation.Formula=新的CellFormula();
//下面是一个减法公式的示例:减去总项目的已售出项目
relation.Formula.Operator=newfunc((p,v)=>{return((decimal.Parse(p.Value.ToString()))-((decimal.Parse(v.Value.ToString())));});
relation.Formula.operans1=总单元格;
relation.Formula.Operator 2=soldCell;
单元格关系。添加(关系);
}
私有void dataGridView1\u CellValueChanged(对象发送方,DataGridViewCellEventArgs e)
{
//查找是否有要更新的单元格的目标单元格
var cellReln=cellRelations.FirstOrDefault(item=>item.SourceCell.RowIndex==e.RowIndex&&item.SourceCell.ColumnIndex==e.ColumnIndex);
if(cellReln!=null)
{
cellReln.DestinationCell.Value=cellReln.Formula.Evaluate();
}
}
}
编辑:请注意-我建议的方法是使用CellRelation&CellFormula的属性类型为DataGridViewCell。因此,它与UI技术(本例中为winform)紧密相连。
理想情况下,这样的解决方案应该独立于UI技术。如果您需要一个单独的业务层中的示例,请给我写一条评论。有什么帮助吗?为你做一个节目?这是一个非常广泛的问题。请询问我们在这里需要什么样的问题。您不希望在DataGridView之间生成c#而不是excel的公式,您需要的是计算/操作/添加/删除DGVIEWS中显示的对象。在这两者之间,我们要讨论WPF、Winforms或ASP应用程序吗?您需要excel之类的应用程序吗?假设你在一个单元格中更改一个值,然后执行一个公式,结果显示在目标单元格中?是的,我想我理解你的观点。。因此,您有两个网格,数据源是两个网格的公共数据集&现在您需要某种事件机制,每当您更改一个值时,另一个值都会得到更新。到目前为止是否正确?更准确地说,您正在代码中寻找这样的接线吗?-Grid one对dataset进行更改->执行一个公式->公式的结果更新dataset的另一个值->Grid 2(链接到dataset的此值)更新,因为其数据源已更改。好的,我将告诉您