合并两个DataGridView列到一个新列 [英] merging two datagridview columns into one new column

查看:476
本文介绍了合并两个DataGridView列到一个新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想两个DataGridView列合并成一个新列。

i want to merge two datagridview columns into one new column.

我第一次修改两处山坳假的Visible属性,然后我尝试添加新的山坳这是值的格式必须为这里面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);



我的代码

my code

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



希望这可以帮助,至于

hope this helps, regards

这篇关于合并两个DataGridView列到一个新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆