将两个datagridview列合并为一个新列
我想将两个datagridview列合并为一个新列。
我首先将两个col的Visible属性更改为false,然后我尝试添加新的col,该值必须格式化为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);
但我不知道这是怎么回事! 请帮我。 我的方式是真的吗?
您可以创建自己的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); } }
希望这会有所帮助,问候