将两个 datagridview 列合并为一个新列 [英] merging two datagridview columns into one new column
问题描述
我想将两个 datagridview 列合并为一个新列.
i want to merge two datagridview columns into one new column.
我首先将两个 col 的 Visible 属性更改为 false,然后我尝试添加新的 col,该值必须格式化为 col1Value 和 col2Value 是上述列的值:
i first change Visible property of two col to false, then i try to add new col which that value must be formatted as this which col1Value and col2Value is value of above columns:
string.Format("{0} per {1}", col1Value, col2Value);
我的代码
reportResultForm.dgvResult.Columns["Height"].Visible = false;
reportResultForm.dgvResult.Columns["Width"].Visible = false;
DataGridViewColumn col = new DataGridViewColumn();
col.DefaultCellStyle.Format = "{0} per {1}";
col.CellTemplate = new DataGridViewTextBoxCell();
dgvResult.Columns.Add(col);
但是我不知道怎么做!请帮我.我的方法是真的吗?
but i dont know how do this! please help me. my way is true?
推荐答案
您可以自己实现 DataGridViewTextBoxCell 并为其覆盖 GetFormattedValue 方法.您可以在此处返回列的格式化值,例如:
You can made your own implementation of the DataGridViewTextBoxCell and override GetFormattedValue method for it. There you can return the formatted value for your column below is an example:
// use custom DataGridViewTextBoxCell as columns's template
col.CellTemplate = new MyDataGridViewTextBoxCell();
...
// custom DataGridViewTextBoxCell implementation
public class MyDataGridViewTextBoxCell : DataGridViewTextBoxCell
{
protected override Object GetFormattedValue(Object value,
int rowIndex,
ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter,
TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
return String.Format("{0} per {1}",
this.DataGridView.Rows[rowIndex].Cells[0].Value,
this.DataGridView.Rows[rowIndex].Cells[1].Value);
}
}
希望能帮到你,问候
这篇关于将两个 datagridview 列合并为一个新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!