dapple
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
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
Seems good to me. Are you sure the collection is filled? Can you show how you are binding to the dropdownlist?
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(connSt ring);
ddlCategory.DataBind();
private static categoriesCollection GetAll(TransactionManager trans, string ConnectionString, int start, int pagelen)
{
//Declare Varibles
SqlDataReader reader;
if(trans != null)
reader = SqlHelper.ExecuteReader(tr ans.Transa ctionObjec t, "Categories_GetList");
else
reader = SqlHelper.ExecuteReader(Co nnectionSt ring, "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"]))?s tring.Empt y:(string) reader["na me"];
c.AcceptChanges();
rows.Add(c);
pagelen -= 1;
}
recordnum += 1;
}
return rows;
}
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(connSt
ddlCategory.DataBind();
private static categoriesCollection GetAll(TransactionManager trans, string ConnectionString, int start, int pagelen)
{
//Declare Varibles
SqlDataReader reader;
if(trans != null)
reader = SqlHelper.ExecuteReader(tr
else
reader = SqlHelper.ExecuteReader(Co
//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["
c.AcceptChanges();
rows.Add(c);
pagelen -= 1;
}
recordnum += 1;
}
return rows;
}
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(connSt ring);
ddlCategory.DataValueField = "Originalid";
ddlCategory.DataTextField = "name";
ddlCategory.DataBind();
?
Still, your code looks correct to me.
Did you do something like:
ddlCategory.DataSource = categoriesDB.GetAll(connSt
ddlCategory.DataValueField
ddlCategory.DataTextField = "name";
ddlCategory.DataBind();
?
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. DropDownLi st ddl)
{
GetAll(null,ConnectionStri ng , ddl) ;
}
public static void GetAll(TransactionManager trans,string ConnectionString , System.Web.UI.WebControls. DropDownLi st ddl)
{
SqlDataReader reader;
if(trans != null)
reader = SqlHelper.ExecuteReader(tr ans.Transa ctionObjec t, "Categories_GetList");
else
reader = SqlHelper.ExecuteReader(Co nnectionSt ring, "Categories_GetList");
ddl.DataSource = reader;
ddl.DataBind();
reader.Close();
}
im still not sure why it didnt work.
public static void GetAll( string ConnectionString , System.Web.UI.WebControls.
{
GetAll(null,ConnectionStri
}
public static void GetAll(TransactionManager trans,string ConnectionString , System.Web.UI.WebControls.
{
SqlDataReader reader;
if(trans != null)
reader = SqlHelper.ExecuteReader(tr
else
reader = SqlHelper.ExecuteReader(Co
ddl.DataSource = reader;
ddl.DataBind();
reader.Close();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
have you tried:
dropdownlist1.DataTextFiel
Regards!
B..M
mmarinov