2009-12-15 32 views
3

我想将两个datagridview列合并到一个新列中。将两个datagridview列合并到一个新列

我第一次修改两处山坳假的Visible属性,然后我尝试添加新的山坳其中该值的格式必须为这里面col1Value和col2Value是上述列的值:

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); 

但我不知道这是怎么做的!请帮帮我。我的方式是真的吗?

+0

你怎么能期望于两列合并成一个柱?数据是不同的,那么你将如何处理不同的数据? – 2009-12-16 16:17:04

+0

我想用col1和col2的值在顶层格式的newcol中显示。 newcolValue = string.Format(“{0} per {1}”,col1Value,col2Value) – Sadegh 2009-12-16 17:14:42

回答

4

您可以自己实现DataGridViewTextBoxCell并为其覆盖GetFormattedValue方法。在那里,你可以在下面为您列返回格式化的值是一个例子:

// 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); 
    } 
} 

希望这会有所帮助,至于