Link to home
Start Free TrialLog in
Avatar of dapple
dappleFlag for Japan

asked on

Strange databinding problem with DataTextField and custom class

hi,
I have a custom class derived from collectionBase  as follows.

public class categoriesCollection : CollectionBase , IBindingList, IList
{




public int Add (categories value)
      {
            return List.Add(value);
      }
}
and so on .

Also a custom class that holds the data , which has public properties
public class categories : IComparable
{
private Int16              _id = 0;
      //keep a copy of the original so it can be used for editable primary keys.
      private Int16 _Originalid;
      private string
  _name = string.Empty;
      public string name
      {
            get { return _name; }
            set
            {
                  if (_name == value)
                        return;
                  _name = value;
                  _isdirty = true;
            }
      }
 }
I databind this to a drop down list , i can set the DataValueField , but anything i put in the DataTextField comes out blank. I understood that you have to have public propertys on the class so the databinding can get the values, which i have so i am at a loss why its blank. Is it a bug?

David

Avatar of mmarinov
mmarinov

Hi dapple,

have you tried:

dropdownlist1.DataTextField = categories.name ?

Regards!
B..M
mmarinov
Seems good to me. Are you sure the collection is filled? Can you show how you are binding to the dropdownlist?
Avatar of dapple

ASKER

no mmarinov , that doesnt work.
The collection is filled because the Value field gets set no problem, i have also looked in debug mode as well and their are values there. Databinding code:

ddlCategory.DataSource = categoriesDB.GetAll(connString);
ddlCategory.DataBind();

private static categoriesCollection GetAll(TransactionManager trans, string ConnectionString, int start, int pagelen)
      {
            //Declare Varibles
            SqlDataReader reader;
            if(trans != null)
                  reader = SqlHelper.ExecuteReader(trans.TransactionObject, "Categories_GetList");
            else
                  reader = SqlHelper.ExecuteReader(ConnectionString, "categories_GetList");
            //Create Collection
            categoriesCollection rows = new categoriesCollection();
            Fill(reader, rows, start, pagelen);
            reader.Close();
            return rows;
      }//end getall
protected static categoriesCollection Fill(SqlDataReader reader, categoriesCollection rows, int start, int pagelen)
      {
            int recordnum = 0;
            while (reader.Read() && (pagelen != 0))
            {
                  if(recordnum >= start)
                  {
                        categories c = new categories();
                        c.id = (Int16)reader["id"];
                        c.Originalid = (Int16)reader["id"];
                        c.name = (Convert.IsDBNull(reader["name"]))?string.Empty:(string)reader["name"];
                        c.AcceptChanges();
                        rows.Add(c);
                        pagelen -= 1;
                  }
                  recordnum += 1;
            }
            return rows;
      }
Avatar of dapple

ASKER

any ideas, another dead question? I have three open questions now i havent had solutions to.
I seem to have missed your comment with code.

Still, your code looks correct to me.

Did you do something like:

ddlCategory.DataSource = categoriesDB.GetAll(connString);
ddlCategory.DataValueField = "Originalid";
ddlCategory.DataTextField = "name";
ddlCategory.DataBind();

?
Avatar of dapple

ASKER

well i solved it by just directly using the reader rather than the collection.
im still not sure why it didnt work.

public static void GetAll( string ConnectionString  , System.Web.UI.WebControls.DropDownList ddl)
      {
                  GetAll(null,ConnectionString  , ddl) ;
      }
      
      public static void GetAll(TransactionManager trans,string ConnectionString  , System.Web.UI.WebControls.DropDownList ddl)
      {
            SqlDataReader reader;
            if(trans != null)
                  reader = SqlHelper.ExecuteReader(trans.TransactionObject, "Categories_GetList");
            else
                  reader = SqlHelper.ExecuteReader(ConnectionString, "Categories_GetList");
            ddl.DataSource = reader;
            ddl.DataBind();
            reader.Close();
      }
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial