Binding a DataRow to a custom class - how set values?

From: Ole Storm (olise_at_mobilixnet.dk)
Date: 12/02/04


Date: Thu, 02 Dec 2004 10:40:22 +0100

I am using a DataGrid to show the contents of an underlying DataTable,
generated at runtime (not bound to a database table or anything).

Everything works well when binding the DataColumn's of my table to
simple types, but I have problems binding a column to a custom class,
MyClass (see code below). The values of this column displays nicely (due
to th ToString method), but I am not able to modify the contents of the
column. All other columns are editable, and the underlying value of
objects in each cell are changed when I edit the cell value.

How do I make columns bound to custom classes editable???

Any help will be appreciated,

Ole Storm.

Code sample:

(...)
// Initializing the DataTable
DataTable dt = new DataTable("Table1");
dt.Columns.Add("First_Col", typeof(string));
dt.Columns.Add("Second_Col", typeof(int));
dt.Columns.Add("Thrid", typeof(double));
dt.Columns.Add("Fourth", typeof(System.DateTime));
DataColumn col = new DataColumn("Fifth", typeof(MyClass));
col.ReadOnly = false;
dt.Columns.Add(col);

Object[] obs = new Object[5]{"test", 2, 3.14, new DateTime(1969,5,5),
new MyClass("one")};
dt.Rows.Add(obs);
obs = new Object[5]{"abcdef", 17, 3.14*2.71, new DateTime(1967,6,26),
new MyClass("two")};
dt.Rows.Add(obs);
obs = new Object[5]{"defg", 37, 3.14*2.71, new DateTime(1967,6,26), new
MyClass("aaaabbb")};
dt.Rows.Add(obs);
DataView dv = new DataView(dt);
dv.AllowNew = false;
dv.AllowDelete = false;
dataGrid1.SetDataBinding(dv, "");
(...)

public struct MyClass : IComparable
{
        private string s;
        public MyClass(string a)
        {
                s=a+a;
        }
        public string Val
        {
                get
                {
                        return s;
                }
                set
                {
                        s=value;
                }
        }

        public override string ToString()
        {
                return s;
        }
        #region IComparable Members

        public int CompareTo(object obj)
        {
                if(obj is MyClass)
                {
                        return this.s.Length.CompareTo(((MyClass)obj).s.Length);
                }
                else
                        throw new Exception("Incompatible objects");
                return 0;
        }

        #endregion
}



Relevant Pages

  • Binding a DataRow to a custom class - how set values?
    ... simple types, but I have problems binding a column to a custom class, ... objects in each cell are changed when I edit the cell value. ... Code sample: ... public struct MyClass: IComparable ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Setting HtmlTableCell attributes from code behind
    ... i dont know what you are after, and why you need a table cell with an ... however I assure you that your code sample works and ... If your trying to make the cell clickable then you are definately going ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • DataGrid cell multiselect
    ... Has anyone got any good examples on how to do simple cell multiselect in a ... in order to do some custom cell coloring. ... had to code this type of thing, so if anyone has a code sample to toss my ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • ASP classic - variable formatting
    ... I've included a code sample below. ... I am trying to format a table cell so that if the value is outside some ... the bgcolor parameter is different. ...
    (microsoft.public.inetserver.asp.db)

Loading