Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Problem with binding custom column type (DataGridView)
 

Problem with binding custom column type (DataGridView)

I made a custom column type consist of a button and a textbox , so when a user click on button I show him a dialog form for selecting a record (in the designer I get a formName and I make an instance of form with reflection) for example each person have departmentId inside, I change the column type of the departmentId in the datagridview to my custom columntype, so when a user select a department from dialog form I want to show departmentTitle inside my control but I want to bind selected departmentId to my grid. Now I encounter an error because the datatype of departmentId is int but I am trying to put the departmenttitle to textbox.

How can I do this? J

Here is my classes I made::

[ToolboxItem(false)]
publicclassTextBox:System.Windows.Forms.TextBox
{
publicTextBox():base()
{
}
publicstringDisplayMember{get;set;}
publicstringValueMember{get;set;}
}
[ToolboxItem(false)]
publicclassButton:System.Windows.Forms.Button
{
publicButton():base()
{
}
publicstringFormName{get;set;}
}
[ToolboxItem(false)]
publicpartialclassLookUp:UserControl
{
publicLookUp()
{
InitializeComponent();
}
publicTextBoxLookUPTextBox
{
get
{
returnthis.lookUpTextBox;
}
}
publicButtonLookUPButton
{
get
{
returnthis.lookUpButton;
}
}
publicboolReadOnly{get;set;}
publicnewstringText
{
get
{
returnthis.lookUpTextBox.Text;
}
set
{
this.lookUpTextBox.Text=value;
}
}
}
publicclassCustomLookUpCell:DataGridViewTextBoxCell
{
publicCustomLookUpCell()
{
}
publicoverridevoidInitializeEditingControl(introwIndex,objectinitialFormattedValue,DataGridViewCellStyledataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex,initialFormattedValue,dataGridViewCellStyle);
CustomLookUpConrolcustomLookUpConrol=this.DataGridView.EditingControlasCustomLookUpConrol;
if(this.Value!=null)
customLookUpConrol.LookUPTextBox.Text=this.Value.ToString();
CustomLookUpColumncustomLookUpColumn=this.OwningColumnasCustomLookUpColumn;
customLookUpConrol.LookUPButton.FormName=string.Format("{0}{1}{2}",customLookUpColumn.NameSpace,".",customLookUpColumn.FormName);
customLookUpConrol.LookUPTextBox.DisplayMember=customLookUpColumn.DisplayMember;
customLookUpConrol.LookUPTextBox.ValueMember=customLookUpColumn.ValueMember;
}
publicoverrideTypeEditType
{
get
{
returntypeof(CustomLookUpConrol);
}
}
publicoverrideTypeValueType
{
get
{
returntypeof(string);
}
}
publicoverrideobjectDefaultNewRowValue
{
get
{
returnstring.Empty;
}
}
privateCustomLookUpConrolEditingCustomLookUpConrol
{
get
{
returnthis.DataGridView.EditingControlasCustomLookUpConrol;
}
}
publicoverrideobjectClone()
{
CustomLookUpCellcustomLookUpCell=base.Clone()asCustomLookUpCell;
returncustomLookUpCell;
}
publicoverrideobjectParseFormattedValue(objectformattedValue,DataGridViewCellStylecellStyle,System.ComponentModel.TypeConverterformattedValueTypeConverter,System.ComponentModel.TypeConvertervalueTypeConverter)
{
returnbase.ParseFormattedValue(formattedValue,cellStyle,formattedValueTypeConverter,valueTypeConverter);
}
}
publicclassCustomLookUpColumn:DataGridViewColumn
{
publicCustomLookUpColumn()
:base(newCustomLookUpCell())
{
}
[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
publicoverrideDataGridViewCellCellTemplate
{
get
{
returnbase.CellTemplate;
}
set
{
if((value==null)||!(valueisCustomLookUpCell))
{
thrownewArgumentException("CanonlycontainCustomLookUpCell");
}
base.CellTemplate=value;
}
}
privateCustomLookUpCellCustomLookUpCellTemplate
{
get
{
return(CustomLookUpCell)this.CellTemplate;
}
}
publicoverridestringToString()
{
StringBuilderstringBuilder=newStringBuilder(100);
stringBuilder.Append("DataGridViewCustomLookUpColumn{Name=");
stringBuilder.Append(this.Name);
stringBuilder.Append(",Index=");
stringBuilder.Append(this.Index.ToString(CultureInfo.CurrentCulture));
stringBuilder.Append("}");
returnstringBuilder.ToString();
}
privatestringformName;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Category("Misc")]
[Description("Thenameoftheformwhichmustbeshow.")]
publicstringFormName
{
get
{
returnthis.formName;
}
set
{
this.formName=value;
}
}
privatestringnameSpace;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Category("Misc")]
[Description("Thenamespacetheofformwhichmustbeshow.")]
publicstringNameSpace
{
get
{
returnthis.nameSpace;
}
set
{
this.nameSpace=value;
}
}
privatestringdisplayMember;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Category("Misc")]
[Description("Thenameofthepropertywhichmustbeshowinsidethecontrol.")]
publicstringDisplayMember
{
get
{
returnthis.displayMember;
}
set
{
this.displayMember=value;
}
}
privatestringvalueMember;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Category("Misc")]
[Description("Thenameofthepropertywhichmustbebind.")]
publicstringValueMember
{
get
{
returnthis.valueMember;
}
set
{
this.valueMember=value;
}
}
publicoverrideobjectClone()
{
CustomLookUpColumncustomLookUpColumn=base.Clone()asCustomLookUpColumn;
customLookUpColumn.FormName=formName;
customLookUpColumn.NameSpace=nameSpace;
customLookUpColumn.DisplayMember=displayMember;
customLookUpColumn.ValueMember=valueMember;
returncustomLookUpColumn;
}
}
[ToolboxItem(true)]
[ToolboxBitmap(typeof(CustomLookUpConrol),"CustomLookUpConrol.bmp")]
publicclassCustomLookUpConrol:LookUp,IDataGridViewEditingControl
{
publicCustomLookUpConrol()
{
this.LookUPTextBox.TextChanged+=newEventHandler(LookUPTextBox_TextChanged);
this.LookUPButton.Click+=newEventHandler(LookUPButton_Click);
}
voidLookUPTextBox_TextChanged(objectsender,EventArgse)
{
//LettheDataGridViewknowaboutthevaluechange.
this.NotifyDataGridViewOfValueChange();
}
voidLookUPButton_Click(objectsender,EventArgse)
{
//CustomLookUpColumn.ButtonClickHandler(sender,e,this.LookUPTextBox);
Formform=(Form)Assembly.GetExecutingAssembly().CreateInstance((senderasButton).FormName.ToString());
if(form.ShowDialog()==DialogResult.OK)
{
PropertyInfodisplayMember=form.Tag.GetType().GetProperties().Single(c=>c.Name==this.LookUPTextBox.DisplayMember.ToString());
PropertyInfovalueMemeber=form.Tag.GetType().GetProperties().Single(c=>c.Name==this.LookUPTextBox.ValueMember.ToString());
this.LookUPTextBox.Text=displayMember.GetValue(form.Tag,null).ToString();
}
}
privatevoidInitializeComponent()
{
this.SuspendLayout();
//
//CustomLookUpConrol
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,13F);
this.Name="CustomLookUpConrol";
this.ResumeLayout(false);
this.PerformLayout();
}
privateDataGridViewdataGridView;
privateintrowIndex;
privateboolvalueChanged;
#regionIDataGridViewEditingControlMembers
publicvoidApplyCellStyleToEditingControl(DataGridViewCellStyledataGridViewCellStyle)
{
this.Font=dataGridViewCellStyle.Font;
this.BackColor=dataGridViewCellStyle.BackColor;
this.ForeColor=dataGridViewCellStyle.ForeColor;
}
publicDataGridViewEditingControlDataGridView
{
get
{
returnthis.dataGridView;
}
set
{
this.dataGridView=value;
}
}
publicobjectEditingControlFormattedValue
{
get
{
returnthis.Text;
}
set
{
this.Text=value.ToString();
}
}
publicintEditingControlRowIndex
{
get
{
returnthis.rowIndex;
}
set
{
this.rowIndex=value;
}
}
publicboolEditingControlValueChanged
{
get
{
returnthis.valueChanged;
}
set
{
this.valueChanged=value;
}
}
publicboolEditingControlWantsInputKey(KeyskeyData,booldataGridViewWantsInputKey)
{
returntrue;
}
publicCursorEditingPanelCursor
{
get
{
returnCursors.Default;
}
}
publicobjectGetEditingControlFormattedValue(DataGridViewDataErrorContextscontext)
{
returnthis.EditingControlFormattedValue;
}
publicvoidPrepareEditingControlForEdit(boolselectAll)
{
System.Windows.Forms.TextBoxtextBox=this.LookUPTextBox;
if(textBox!=null)
{
if(selectAll)
textBox.SelectAll();
else
textBox.SelectionStart=textBox.Text.Length;
}
}
publicboolRepositionEditingControlOnValueChange
{
get
{
returnfalse;
}
}
#endregion
privatevoidNotifyDataGridViewOfValueChange()
{
if(!this.valueChanged)
{
this.valueChanged=true;
this.dataGridView.NotifyCurrentCellDirty(true);
}
}
protectedoverridevoidOnKeyPress(KeyPressEventArgse)
{
base.OnKeyPress(e);
//LettheDataGridViewknowaboutthevaluechange.
this.NotifyDataGridViewOfValueChange();
}
}

So Thanks Inadvance.

Alimardani
Ali Asghar Alimardani  Tuesday, March 17, 2009 6:28 AM
Hi Ali Asghar Alimardani,

I have something not very clear. Did you bind the departmentID to a column other than your custom column? or you just want to save the departmentID as the value of the TextBox?


Best regards,
Bruce Zhou

Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Thursday, March 19, 2009 3:41 AM
We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post editor window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.


Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Monday, March 23, 2009 3:29 AM

You can use google to search for other answers

Custom Search

More Threads

• Error HRESULT E_FAIL has been returned from a call to a COM component.
• Change <TAB> to <ENTER> to skip another control
• Changing the Layout mode of custom designer
• Designer Support when Control has abstract base class
• TabControl - problem
• Editing inherited controls in Design View
• Treeview orientation
• ControlDesigner at runtime
• container control with fixed Header
• Compona ComboBox Control ?