Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Problem with DataGridView CustomControl
 

Problem with DataGridView CustomControl

I Made a Custom Control to use for Converting the Fields which value is Int in database to corresponding Title Value. For example “Male�, ��& “Female�, ��br>
for this :
-------------------------------
public class EnumCell : DataGridViewTextBoxCell
{
public EnumCell(){}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
string Title = string.Empty;
EnumColumn enumColumn = OwningColumn as EnumColumn;
Title = enumColumn.Collection.Single(c => c.Value == (
string)value).Title;
return Title;
}
---------------------------------

public class EnumColumn : DataGridViewColumn
{
public EnumColumn(): base(new EnumCell())
{
}
private List<Fieldvalue> M_Collection;
public List<Fieldvalue> Collection
{
get{
return M_Collection;
}
set
{
M_Collection =
value;
}
}
public override object Clone()
{
EnumColumn column = base.Clone() as EnumColumn;
column.Collection = M_Collection;
return column;
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if ((value == null) || !(value is EnumCell))
{
throw new ArgumentException("Invalid" +
" cell type, EnumColumn " +
"can only contain EnumCell");
}
}
}
}

---------------------
And Also A class for Value And Title

public class Fieldvalue
{
public Fieldvalue()
{}
public string Value { get; set; }
public string Title { get; set; }
}

----------------------------

The problem is I can not set the Collection Property Of My EnumClass in the Disgner so it is null and i get null exeption errror during Loading Data in datagridview.

although if i use an Enum I can set it in the desigmer but i dont want to use enum.

am i doing something wrong?

So Thanks Inadvance


Alimardani
Ali Asghar Alimardani  Thursday, March 05, 2009 8:37 AM
HiAlimardani,

That's because M_Collection object isn't initialized. As a result you get exception. I suggest you serialize the code in the designer so that the item you add for the collection will not lose after rebuilding. You can serialize the code by applying theDesignerSerializationVisibility Attibute for the Collection Property.

The changed code:

publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
this.dataGridView1.RowCount=5;
}
}
publicclassEnumCell:DataGridViewTextBoxCell
{
publicEnumCell(){}
protectedoverrideobjectGetFormattedValue(objectvalue,introwIndex,refDataGridViewCellStylecellStyle,System.ComponentModel.TypeConvertervalueTypeConverter,System.ComponentModel.TypeConverterformattedValueTypeConverter,DataGridViewDataErrorContextscontext)
{
stringTitle=string.Empty;
EnumColumnenumColumn=OwningColumnasEnumColumn;
if(value!=null)
Title=enumColumn.Collection.Single(c=>c.Value==(string)value).Title;
returnTitle;
}
publicclassEnumColumn:DataGridViewColumn
{
publicEnumColumn()
:base(newEnumCell())
{
}
privateList<Fieldvalue>M_Collection=newList<Fieldvalue>();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
publicList<Fieldvalue>Collection
{
get
{
returnM_Collection;
}
set
{
M_Collection=value;
}
}
publicoverrideobjectClone()
{
EnumColumncolumn=base.Clone()asEnumColumn;
column.Collection=M_Collection;
returncolumn;
}
publicoverrideDataGridViewCellCellTemplate
{
get
{
returnbase.CellTemplate;
}
set
{
if((value==null)||!(valueisEnumCell))
{
thrownewArgumentException("Invalid"+
"celltype,EnumColumn"+
"canonlycontainEnumCell");
}
}
}
}
}
publicclassFieldvalue
{
publicFieldvalue()
{}
publicstringValue{get;set;}
publicstringTitle{get;set;}
}

The code is just changed to get rid of the exception. I doesn't try to add any code to implement you want to achieve.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, March 06, 2009 12:56 PM
HiAlimardani,

That's because M_Collection object isn't initialized. As a result you get exception. I suggest you serialize the code in the designer so that the item you add for the collection will not lose after rebuilding. You can serialize the code by applying theDesignerSerializationVisibility Attibute for the Collection Property.

The changed code:

publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
this.dataGridView1.RowCount=5;
}
}
publicclassEnumCell:DataGridViewTextBoxCell
{
publicEnumCell(){}
protectedoverrideobjectGetFormattedValue(objectvalue,introwIndex,refDataGridViewCellStylecellStyle,System.ComponentModel.TypeConvertervalueTypeConverter,System.ComponentModel.TypeConverterformattedValueTypeConverter,DataGridViewDataErrorContextscontext)
{
stringTitle=string.Empty;
EnumColumnenumColumn=OwningColumnasEnumColumn;
if(value!=null)
Title=enumColumn.Collection.Single(c=>c.Value==(string)value).Title;
returnTitle;
}
publicclassEnumColumn:DataGridViewColumn
{
publicEnumColumn()
:base(newEnumCell())
{
}
privateList<Fieldvalue>M_Collection=newList<Fieldvalue>();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
publicList<Fieldvalue>Collection
{
get
{
returnM_Collection;
}
set
{
M_Collection=value;
}
}
publicoverrideobjectClone()
{
EnumColumncolumn=base.Clone()asEnumColumn;
column.Collection=M_Collection;
returncolumn;
}
publicoverrideDataGridViewCellCellTemplate
{
get
{
returnbase.CellTemplate;
}
set
{
if((value==null)||!(valueisEnumCell))
{
thrownewArgumentException("Invalid"+
"celltype,EnumColumn"+
"canonlycontainEnumCell");
}
}
}
}
}
publicclassFieldvalue
{
publicFieldvalue()
{}
publicstringValue{get;set;}
publicstringTitle{get;set;}
}

The code is just changed to get rid of the exception. I doesn't try to add any code to implement you want to achieve.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, March 06, 2009 12:56 PM
Hi

Thanks. The Problem Solved. Your suggestion was the best.

Another question?

I am new in making Controls And CustomControl. I also want to make a custom control which is a TextBox with a Button. i dont want to add to datagrid aDataGridViewTextBoxColumn and aDataGridViewButtonColumn.

But for making this i think i have to override Paint Method. but really i have no experience in GDI .

So Thanks Inadvance.
Alimardani
Ali Asghar Alimardani  Saturday, March 07, 2009 7:13 AM
Hi Ali Asghar Alimardani,

I'd like to suggest you first learn the basic knowledge in GDI+. Like drawLine, drawRectangle method, you need to know how to use them before startingmore complex work related to drawing.

To create such a custom DataGridViewColumn, drawing ourselves is an essential work unless some one has implemented the exactly one for you.

Here is just ansample for implementing the DataGridViewNumericUpDown control, thesample hasmost ofthe steps to create a custom DataGridViewColumn. You can read over the article and do step by step. After you understand the purpose of each of the steps, it will be easy for you to customize your own.

If you meet any problem hard to resolve, you can take advantage of the online resources, such as MSDN forum.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Saturday, March 07, 2009 9:40 AM

You can use google to search for other answers

Custom Search

More Threads

• Access key. button is invoked without pressing Alt
• How can we avoid user to select node from diffrent category in trre control.
• Using WMI to get date and time on a remote computer
• Custom component selection problem
• AutoValidation problem with windows forms
• Lost MSWinsck.ocx. Help needed.
• Overriding the default Windows Forms IRootDesigner
• Draggin out my app without freezing UI (C++)
• Design Time Controls
• Mouse wheel and scroll bar