Csharp/C#教程:将两个datagridview列合并为一个新列分享


将两个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); } } 

希望这会有所帮助,问候

上述就是C#学习教程:将两个datagridview列合并为一个新列分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/985277.html

(0)
上一篇 2021年12月21日
下一篇 2021年12月21日

精彩推荐