merging two datagridview columns into one new column

后端 未结 2 1296
情深已故
情深已故 2021-01-14 07:36

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

i first change Visible property of two col to false, then i try to add new col which that value must be

2条回答
  •  死守一世寂寞
    2021-01-14 08:13

    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

提交回复
热议问题