[英]How to make calculations of column 1 and 2 in column 3 in data grid view windows form
I want the subtraction of column 1
and column 2
in column 3
for a data grid view in windows form using C#.我想要使用 C# 在 Windows 窗体中为数据网格视图减去第 3
column 1
和第column 2
column 3
。 As of my knowledge we need to give cell content click event据我所知,我们需要给单元格内容点击事件
//how to specify click only for column3
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e) {
//here i need to give like column3 = column2 - column1
}
I give here a code example demonstrating the use of a calculated column in a DataTable.我在这里给出一个代码示例,演示如何在 DataTable 中使用计算列。 This may be sufficient for your purposes.
这可能足以满足您的目的。
public partial class Form1 : Form
{
DataTable dt;
public Form1()
{
InitializeComponent();
dt = new DataTable();
dt.Columns.Add(new DataColumn("col1", typeof(int)));
dt.Columns.Add(new DataColumn("col2", typeof(int)));
dt.Columns.Add(new DataColumn("col3", typeof(int), "col2 - col1"));
var newRow = dt.NewRow();
newRow[0] = 5;
newRow[1] = 7;
dt.Rows.Add(newRow);
newRow = dt.NewRow();
newRow[0] = 12;
newRow[1] = 19;
dt.Rows.Add(newRow);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
dt.Rows[0][0] = 10;
}
}
The button click is deliberately crude, but I hope it is sufficient to demonstrate that if one field in the calculation alters, the calculation is refreshed immediately.按钮点击是故意粗糙的,但我希望它足以证明如果计算中的一个字段发生变化,计算会立即刷新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.