我的问题与this完全相同。由于主题没有答案,所以不要投票。
问题简介: 我有一个CustomGridView,一种DataGridView。下面的代码附在它上面。当我在设计时删除datagridview时,我可以看到列都是正确的但是datagridviewcolumn名称的名称是不同的,而不是我给的。
在运行时,列是重复的。无法弄清楚原因。
属性页面看起来像这样
代码:
public class CustomAccountDataGridView : DataGridView
{
private bool _paddingOn = true;
public bool PaddingOn
{
get { return _paddingOn; }
set { _paddingOn = value; }
}
public CustomAccountDataGridView()
{
this.AutoGenerateColumns = false;
this.Columns.Clear();
DataGridViewTextBoxColumn columnAccountID = new DataGridViewTextBoxColumn();
columnAccountID.DataPropertyName = "ACCOUNTID";
columnAccountID.Name = "colACCOUNTID";
columnAccountID.HeaderText = "A/c";
columnAccountID.Width = 110;
columnAccountID.Visible = false;
this.Columns.Add(columnAccountID);
DataGridViewTextBoxColumn columnClientAccountID = new DataGridViewTextBoxColumn();
columnClientAccountID.DataPropertyName = "CLIENTACCOUNTID";
columnClientAccountID.Name = "colCLIENTACCOUNTID";
columnClientAccountID.HeaderText = "Client A/c";
columnClientAccountID.Width = 110;
this.Columns.Add(columnClientAccountID);
DataGridViewTextBoxColumn columnParticipantID = new DataGridViewTextBoxColumn();
columnParticipantID.DataPropertyName = "PARTICIPANTID";
columnParticipantID.Name = "colPARTICIPANTID";
columnParticipantID.HeaderText = "Participant";
columnParticipantID.Width = 60;
this.Columns.Add(columnParticipantID);
DataGridViewTextBoxColumn columnAccountName = new DataGridViewTextBoxColumn();
columnAccountName.Name = "colACCOUNTNAME";
columnAccountName.HeaderText = "A/c Name";
columnAccountName.Width = 200;
columnAccountName.ReadOnly = true;
this.Columns.Add(columnAccountName);
DataGridViewTextBoxColumn columnXsactValue = new DataGridViewTextBoxColumn();
columnXsactValue.DataPropertyName = "XSACTVALUE";
columnXsactValue.Name = "colXSACTVALUE";
columnXsactValue.Width = 110;
columnXsactValue.HeaderText = "Value";
columnXsactValue.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
columnXsactValue.DefaultCellStyle.Format = "N2";
this.Columns.Add(columnXsactValue);
}
protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
{
string sClientID = string.Empty;
foreach (DataGridViewRow dvr in (this.Rows))
{
for (int i = 0; i < dvr.Cells.Count; i++)
{
if (this.Columns[i].DataPropertyName == "ACCOUNTID")
{
if (dvr.Cells["colACCOUNTID"].Value != null)
{
sClientID = dvr.Cells["colACCOUNTID"].Value.ToString();
dvr.Cells["colACCOUNTNAME"].Value = sClientID; //utility.ClientExtention.getAccountName(sClientID);
break;
}
}
}
}
base.OnDataBindingComplete(e);
}
protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
{
(e.Control as TextBox).CharacterCasing = CharacterCasing.Upper;
}
base.OnEditingControlShowing(e);
}
protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
{
string sClientAccountID = string.Empty;
string sParticipantID = "JKB";
if (e.RowIndex >= 0)
{
if (this.Columns[e.ColumnIndex].DataPropertyName == "CLIENTACCOUNTID")
{
if (this[e.ColumnIndex, e.RowIndex].Value != null)
{
if (this[e.ColumnIndex, e.RowIndex].Value.ToString().Length != 0)
{
if (this.PaddingOn)
{
sClientAccountID = this[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper().PadLeft(13, '0');
}
else
{
sClientAccountID = this[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper();
}
this[e.ColumnIndex, e.RowIndex].Value = sClientAccountID;
}
}
}
if (this.Columns[e.ColumnIndex].DataPropertyName == "PARTICIPANTID")
{
if (this[e.ColumnIndex, e.RowIndex].Value != null)
{
if (this[e.ColumnIndex, e.RowIndex].Value.ToString().Length != 0)
{
sParticipantID = this[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper();
}
this[e.ColumnIndex, e.RowIndex].Value = sParticipantID;
}
}
if (sClientAccountID != string.Empty & sParticipantID != string.Empty)
{
this["ACCOUNTNAME", e.RowIndex].Value = (string.Format("{0}-{1}", sParticipantID, sClientAccountID));
}
}
base.OnCellValueChanged(e);
}
}
}
答案 0 :(得分:1)
我找到了似乎有用的东西here:
protected override void InitLayout()
{
base.InitLayout();
SetColumns();
}
SetColumns是添加我想要的列的子例程。
答案 1 :(得分:0)
如果它有用,我刚刚在VS 2015 Express上遇到了这个问题,带有标准的DataGridView。一旦我重命名我的自定义列,问题就出现了。恢复默认名称可以解决问题,就像刷新自定义列并保留重复项一样。我可以随时重现并修复问题,看起来一致。请注意,当原始列和重复列都在时,设计器代码(Designer.cs)中没有重复项的痕迹,请不要问我为什么......