日期:2014-05-19  浏览次数:20982 次

Winform 中datagrid 的列怎样实现自动计算,计算的结果列也可以进行修改成任意数值 (高分)
比如:   Column1     Column2     Column3

              10               20               200

Column3   的值是   Column1   *   Column2
但是Column3也可以自行修改成任意数。
请问Winform中能这样处里吗?


------解决方案--------------------
没看懂你的意思
你说的是不是column3的值让它不能修改啊!!
创建一个tablestyle设置columns为readOnly!!
这样不就是不用修改了嘛?
示例如下:
private void Header订单()
{
DataGridTableStyle tableStyle = new DataGridTableStyle();

DataGridTextBoxColumn columnStyle = newDataGrid TextBoxColumn();
columnStyle.MappingName = "订单ID ";
columnStyle.HeaderText = "订单ID ";
columnStyle.Width = 80;
//就是这样里的属性
columnStyle.ReadOnly = true;
tableStyle.GridColumnStyles.Add(columnStyle);

columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName = "公司名称 ";
columnStyle.HeaderText = "公司名称 ";
columnStyle.Width = 80;
columnStyle.ReadOnly = false;
tableStyle.GridColumnStyles.Add(columnStyle);

tableStyle.RowHeadersVisible =true;
tableStyle.MappingName = "订单 ";
dtg订单.TableStyles.Add(tableStyle);
}
自动计算的话:
获取选中的行的行号:
rowcell 为当前的行号
dtg订单[rowcell,2] =dtg订单[rowcell,0] * dtg订单[rowcell,1]


//以下是判断行的状态是否已经被修改
if(dt订单.Rows[rowcell].RowState==DataRowState.Modified )
//以下是判断行的状态是否已经被新增
if(dt订单.Rows[rowcell].RowState==DataRowState.Added)

具体自己看真做吧,也有点不明白你要求的意思
是增加一行记录的时候还是修改一行的记录的时候呢?

------解决方案--------------------
用 DataColumn class的Expression property

DataTable table = new DataTable ();

// Create the first column.
DataColumn priceColumn = new DataColumn();
priceColumn.DataType = System.Type.GetType( "System.Decimal ");
priceColumn.ColumnName = "price ";
priceColumn.DefaultValue = 50;

// Create the second, calculated, column.
DataColumn taxColumn = new DataColumn();
taxColumn.DataType = System.Type.GetType( "System.Decimal ");
taxColumn.ColumnName = "tax ";
taxColumn.Expression = "price * 0.0862 ";

// Create third column.
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType( "System.Decimal ");
totalColumn.ColumnName = "total ";
totalColumn.Expression = "price + tax ";

// Add columns to DataTable.
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);

DataRow row = table.NewRow();
table.Rows.Add(row);
DataView view = new DataView(table);
dataGrid1.DataSource = view;

------解决方案--------------------
好像自行修改不行 楼主再看看