displaying data in list box

  • Thread starter Thread starter Derrick
  • Start date Start date
D

Derrick

Hi

I have a form that has a list box bound to a Data view.

When I add a record to the data table in run time, it is displayed in the
list box as System.Data.DatarowView. When the app is saved and reloaded,
the data appears properly. Any ideas on how to work around?

Code Snippet to add the row to the data table
<Snip>
DataSetInfo.DetailsRow dr = this.dataSetInfo.Details.NewDetailsRow();
.... code to fill the fields
this.dataSetInfo.Details.AddDetailsRow(dr);
</Snip>

Derrick
 
Hi Derrick,

You may check the value of DataSource and DisplayMember property of the
listbox, when displaying "System.Data.DataRowView",
probably this issue is caused by you set DataSource to a certain DataTable
however the DisplayMember property was not set to a data column. In this
scenario, Listbox usesToString() method to convert the object into string
before adding it.

Please feel free to let me know if your problem is not the case, Thanks!
Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Thanks for the response.

the DataSource is set to the dataView - and DisplayMember is set to the
correct field, so that is not the problem in this case.

Derrick
 
Hi Derrick,

If DataSource and DisplayMember is set correctly,
this issue needs some further investigation, could you seperate this issue
into a small project and send me to let me take a look?

Also , Here is a small code snippet I tried without problem on my side, I
pasted it below, maybe you can tell me the difference between my test and
your reall scenario.
//I generated a strong-type dataset from Customer table,
//there are two fields in it, "CustID"(string), and "Name"(string)
//I only put a listbox and a button on the Form.
private void button1_Click(object sender, system.EventArgs e)
{
DataView dv = new DataView( dataSet11.Customers );
listBox1.DataSource = dv;
listBox1.DisplayMember = "CustID";
DataSet1.CustomersRow dr = dataSet11.Customers.NewCustomersRow();
dr["CustID"] = "1234";
dr["Name"] = "aaaa";
dataSet11.Customers.AddCustomersRow( dr );
}

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Sorry for the delay in following up to your response - I had trouble
creating the objects in a simple app that would replicate the error.
Until I get that finished, here is the code I am using to put data into the
dataview

private void buttonAdd_Click(object sender, System.EventArgs e)
{
DataSetGolfPool.PicksRow dr = this.DataSetGolfPool.Picks.NewPicksRow();
dr.poolie_ID=Convert.ToInt32(this.ComboBoxPoolies.SelectedValue);
dr.tournament_ID=Convert.ToInt32(this.ComboBoxTournaments.SelectedValue);
dr.golfer_ID=Convert.ToInt32(this.ListBoxGolfers.SelectedValue);
dr.fullName=Convert.ToString(this.ListBoxGolfers.SelectedItem);
this.DataSetGolfPool.Picks.AddPicksRow(dr);
}

The row filter I am using:

this.DataViewPicks.RowFilter= String.Concat( "poolie_id=",
this.ComboBoxPoolies.SelectedValue, " AND tournament_id=",
this.ComboBoxTournaments.SelectedValue);

The ListBox is bound with the following properties:

this.ListBoxPicks.DataSource = this.DataViewPicks;
this.ListBoxPicks.DisplayMember = "fullName";
this.ListBoxPicks.Location = new System.Drawing.Point(616, 80);
this.ListBoxPicks.Name = "ListBoxPicks";
this.ListBoxPicks.Size = new System.Drawing.Size(160, 108);
this.ListBoxPicks.TabIndex = 4;
this.ListBoxPicks.ValueMember = "id";

//

// SqlDataAdapterPicks

//
this.SqlDataAdapterPicks.DeleteCommand = this.sqlDeleteCommand2;
this.SqlDataAdapterPicks.InsertCommand = this.sqlInsertCommand2;
this.SqlDataAdapterPicks.SelectCommand = this.sqlSelectCommand2;
this.SqlDataAdapterPicks.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Picks", new
System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("id", "id"),
new System.Data.Common.DataColumnMapping("poolie_ID", "poolie_ID"),
new System.Data.Common.DataColumnMapping("tournament_ID", "tournament_ID"),
new System.Data.Common.DataColumnMapping("golfer_ID", "golfer_ID")})});

Thanks

Derrick

Derrick said:
I will post it tomorrow AM

thx for the help.

Derrick

"Ying-Shen Yu[MSFT]" said:
Hi Derrick,

If DataSource and DisplayMember is set correctly,
this issue needs some further investigation, could you seperate this issue
into a small project and send me to let me take a look?

Also , Here is a small code snippet I tried without problem on my side, I
pasted it below, maybe you can tell me the difference between my test and
your reall scenario.
//I generated a strong-type dataset from Customer table,
//there are two fields in it, "CustID"(string), and "Name"(string)
//I only put a listbox and a button on the Form.
private void button1_Click(object sender, system.EventArgs e)
{
DataView dv = new DataView( dataSet11.Customers );
listBox1.DataSource = dv;
listBox1.DisplayMember = "CustID";
DataSet1.CustomersRow dr = dataSet11.Customers.NewCustomersRow();
dr["CustID"] = "1234";
dr["Name"] = "aaaa";
dataSet11.Customers.AddCustomersRow( dr );
}

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Hi Derrick,

Thanks for your reply,

I read your code carefully, and made a similiar program to try to reproduce
this issue, but it works fine.

Based on my current investigation, listbox will show
"System.Data.DataRowView" in these two situations:
1. the DisplayMember is null or not exist in underlying datasource.
2. An exception was thrown when listbox tries to retrieve text from the
corresponding column of the current DataRowView object.

So you may try checking the following issues:
1. print out the DisplayMember and ValueMember properties after setting
values.
2. run your program with debugger and enable catching CLR exceptions when
the exception is thrown. (You may see the options by clicking menu
"Debug"->"Exceptions") . If you get any exceptions, please include the call
stacks in your reply.
If you see [Non-User code] in your call stack, you may follow the KB
article below to load symbols to get complete call stack information.

<HOW TO: Use a Symbol Server with the Visual Studio .NET Debugger>
http://support.microsoft.com/?id=319037

If you have any updates to this issue, please feel free to post it in this
thread to let me know.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
I fixed it with this code:

dr.fullName=listBoxGolfers.GetItemText
(this.listBoxGolfers.SelectedItem);

to replace this code:

dr.fullName=this.listBoxGolfers.SelectedItem.ToStr
ing();
 
Back
Top