开发者

Databinding gives "System.Data.DataRowView" instead of actual values

开发者 https://www.devze.com 2022-12-26 19:56 出处:网络
This is my code: string SQL = \"SELECT email FROM members WHERE subscribe=true\"; string myConnString = Application[\"ConnectionString\"].ToString();

This is my code:

        string SQL = "SELECT email FROM members WHERE subscribe=true";
        string myConnString = Application["ConnectionString"].ToString();

        OleDbConnection myConnection = new OleDbConnection(myConnString);
        OleDbCommand myCommand = new OleDbCommand(SQL, myConnection);
        myConnection.Open();
        OleDbDataAdapter MyAdapter = new OleDbDataAdapter(myCommand);
        DataSet ds = new DataSet();
        MyAdapter.Fill(ds);

        MailsListBox.DataSource = ds;
        MailsListBox.DataBind();

        GridView1.DataSource = ds;
        GridView1.DataBind();

        myConnection.Close();

And so it looks:

Databinding gives "System.Data.DataRowView" instead of actual values

As you开发者_Python百科 see, the GridView shows the dataset just fine, and the ListBox fails it. What happened? How can I fix it?


If you don't tell your control what specific properties you want to use for display, it just calls ToString() on the items it binds to, and uses that for display. Which for most objects, ToString() by default just returns the type name.

Try setting MailsListBox.DataTextField and MailsListBox.DataValueField to "email".


When you do MailsListBox.DataSource = ds; you are not actually setting the Dataset as DataSource but rather the default DataView. The Grid knows how to deal with it.

In both cases, Grid and Listbox, you should set DataMember as well. Use someting like:

MyAdapter.Fill(ds, "Foo");

MailsListBox.DataSource = ds;
MailSlistBox.DataMember = "Foo"; 
MailsListBox.DisplayMember = "email";
MailsListBox.DataBind();
0

精彩评论

暂无评论...
验证码 换一张
取 消