[英]How to add two mysql table columns to one column in datagridview?
[英]merging two datagridview columns into one new column
我想將兩個 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);
}
}
希望這有幫助,問候
試試這個(它對我有用):
reportResultForm.dgvResult.Columns.Add("newColumn", "New Column");
foreach (DataGridViewRow row in reportResultForm.dgvResult.Rows)
{
string height= row.Cells["Height"].Value.ToString();
string width= row.Cells["Width"].Value.ToString();
string formattedCol= string.Format("{0} per {1}", height, width);
row.Cells["newColumn"].Value = formattedCol;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.