?
Solved

System.Data.DataRowView in the listbox

Posted on 2005-03-23
26
Medium Priority
?
4,811 Views
Last Modified: 2012-08-14
My list box shows System.Data.DataRowView instead of the display member. I am posting some code. Someone please help me with this.

mtCommand.CommandText = "select Distinct b.la_value, a.la_model_type from model a, l_attributes b where a.la_model_type = b.l_attributes_id and a.track_id  = '"+txtTrack.Text+"'" ;
daOracleDataAdapter.SelectCommand = mtCommand;
DataSet ds = new DataSet();
conn.Open();
ds.Clear();
daOracleDataAdapter.Fill(ds, "ModelTypes");
                  
//Model Types DataTable
DataTable mtTable = ds.Tables["ModelTypes"];
                  
//Load the Model types in the listbox
                  
lstModels.DataSource = ds.Tables["ModelTypes"].DefaultView;
lstModels.DisplayMember = "LA_VALUE" ;
lstModels.ValueMember = "LA_MODEL_TYPE";


Also on selecting a value from lstModels listbox another listbox (lstSteps) and a datagrid should populate. I have the code written in the lstModels selected index changed. Even the listbox lstSteps does not show the display member.

On populating the listbox the selection defaults to the first item in the listbox. Is it possible to not make the selection to the first item unless the user clicks on the item in the listbox?
            
0
Comment
Question by:Sirees
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 13
26 Comments
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13611165
This will happen any of the datasource, DisplayMember or ValueMember properties are not set rightly.
ds.Tables["ModelTypes"].DefaultView can be replaced with ds.Tables["ModelTypes"]

Also check if the table has column names "LA_VALUE" and  "LA_MODEL_TYPE

Cheers
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13611189
Is it possible to not make the selection to the first item unless the user clicks on the item in the listbox?
This is not possible as far as i know. If you can do  this let me know.

cheers
0
 
LVL 20

Author Comment

by:Sirees
ID: 13611260
I tried replacing the ds.Tables["ModelTypes"]. I also made sure the table has those columns.

I mean to say on populating the listbox the focus (blue bar) is on first item. Can we take the focus off?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:rajaloysious
ID: 13611277
try giving listbox1.selectedindex = -1. It may work...
i encountered the problem with a combobox rather
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13611297
Also you need to set the connection object for the adapter or the command object...
this will solve your first problem.
if you want a working example let me know...

cheers
0
 
LVL 20

Author Comment

by:Sirees
ID: 13611325
It did not work out.
0
 
LVL 20

Author Comment

by:Sirees
ID: 13611365
The strange thing is I have another list box with different query but the display member and value member are set in the same way as the lstModels listbox and it works just fine. What could be the problem?
0
 
LVL 20

Author Comment

by:Sirees
ID: 13611374
I already set the connection object to the command object.
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13611437
ok fine. What you can do is that to check if the table gets filled up
use ds.Tables["ModelTypes"].Rows.Count to find if there are any rows which are returning

Also you can check if there are columns by names "LA_VALUE" and  "LA_MODEL_TYPE"
use ds.Tables["ModelTypes"].Column[0].Name and  ds.Tables["ModelTypes"].Column[1].Name

0
 
LVL 20

Author Comment

by:Sirees
ID: 13611554
ds.Tables["ModelTypes"].Rows.Count is fetching me 8 rows

It is also giving me Column names "LA_VALUE" and "LA_MODEL_TYPE" but still showing System.Data.DataRowView.
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13611602
Oops the value member should be numeric and display member should be string
This is what i think is causing the problem.

cheers
0
 
LVL 20

Author Comment

by:Sirees
ID: 13611662
I have that right. My display member is string and value member is numeric. I also tried displaying the listbox1.Selecteditem in the label box. It just workd fine showing my LA_VALUE but it is not showing that in the listbox.
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13611699
just can you do this last one...

put in the value member before setting the display member

lstModels.DataSource = ds.Tables["ModelTypes"].DefaultView;
lstModels.ValueMember = "LA_MODEL_TYPE";
lstModels.DisplayMember = "LA_VALUE" ;

Also if you are  clearing the listbox, do so in this manner

lstModels.ValueMember = null;
lstModels.DisplayMember = null;
lstModels.DataSource = null;
lstModels.Clear.

If you dont do this when you clear you will end yp having System.Data.Datarowview

cheers

0
 
LVL 20

Author Comment

by:Sirees
ID: 13611784
I tried putting the value member before setting the display memeber...but no luck.

I am not clearing the listbox. I tried that also.
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13611868
Replace this for your code and try...
i have given all known issues i have come accross. this i guess would catch the issue. i have modified the query as well..

mtCommand.CommandText = "select Distinct b.la_value  "LA_VALUE", a.la_model_type "LA_MODEL_TYPE" from model a, l_attributes b where a.la_model_type = b.l_attributes_id and a.track_id  = '"+txtTrack.Text+"'" ;
daOracleDataAdapter.SelectCommand = mtCommand;
DataSet ds = new DataSet();
conn.Open();
ds.Clear();
daOracleDataAdapter.Fill(ds, "ModelTypes");
               
//Model Types DataTable
DataTable mtTable = ds.Tables["ModelTypes"];
               
//Load the Model types in the listbox
MessageBox.Show(ds.Tables["ModelTypes"].Rows.Count.Tostring()); // to find if there are any rows which are returning

//Also you can check if there are columns by names "LA_VALUE" and  "LA_MODEL_TYPE"
MessageBox.Show(ds.Tables["ModelTypes"].Column[0].Name);
MessageBox.Show(ds.Tables["ModelTypes"].Column[1].Name);

lstModels.DataSource = ds.Tables["ModelTypes"].DefaultView;
lstModels.ValueMember = "LA_MODEL_TYPE";
lstModels.DisplayMember = "LA_VALUE" ;

lstModels.Refresh(); //if this doesnt complie comment it



0
 
LVL 20

Author Comment

by:Sirees
ID: 13611979
hey I tried all this but still no luck. It gives me the correct row count and the correct column names. But still displays System.Row.RowDataView
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13612013
Again i am asking you
is LA_MODEL_TYPE of an numeeric type
and LA_VALUE is of string type..

i give up because, i have solved this type of issues upteen number of times and have given you all the ways in which i will troubleshoot
0
 
LVL 20

Author Comment

by:Sirees
ID: 13612091
LA_MODEL_TYPE is numeric and LA_VALUE is string.

private void lstModels_SelectedIndexChanged(object sender, System.EventArgs e)
{
     //Fill the label box with the Model type
     lblSample.Text = ((DataRowView)lstModels.SelectedItem)["LA_VALUE"].ToString();
}

This is displaying the text in the label box but not listbox.

Even I am lost in this. The same logic works for a different listbox and not this one.

Anyhow thanks for trying to help me.
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13612139
if you can show me the working code and the non working which i have, i can try to figure out...
There is a simple thing we are missing...
when i looked at your question, i thought of solving it in the first shot...
0
 
LVL 20

Author Comment

by:Sirees
ID: 13612233
ok here is the working code: (on selecting an item in the lstModels lisbox it populates the second listbox lstSteps

OracleCommand mtCommand = conn.CreateCommand();
mtCommand.CommandText = "select a.la_child, b.la_value from la_parent_child a, l_attributes b where a.la_parent = '"+lstModels.SelectedValue+"' and b.l_attributes_id =a.la_child" ;
daOracleDataAdapter.SelectCommand = mtCommand;
            
DataSet ds1 = new DataSet();
ds1.Clear();
                  
//Fill the Dataset with Model types
                  
daOracleDataAdapter.Fill(ds1, "StepTypes");
DataTable mtTable = ds1.Tables["StepTypes"];
                  
lstSteps.DataSource = ds1.Tables["StepTypes"];
lstSteps.DisplayMember = "LA_VALUE" ;
lstSteps.ValueMember = "LA_CHILD";
                  
0
 
LVL 8

Accepted Solution

by:
rajaloysious earned 1500 total points
ID: 13612251
mtCommand.CommandText = "select Distinct b.la_value  "LA_VALUE", a.la_model_type "LA_MODEL_TYPE" from model a, l_attributes b where a.la_model_type = b.l_attributes_id and a.track_id  = '"+txtTrack.Text+"'" ;

i have put in column captions.

also try this
private void lstModels_SelectedIndexChanged(object sender, System.EventArgs e)
{
     //Fill the label box with the Model type
     lblSample.Text = ((DataRowView)lstModels.SelectedItem)["LA_MODEL_TYPE"].ToString();
}

Also check if the query is returning nulls
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13612279
Ooooooooi get it. Can you declare this as a private class level variable on the top
DataSet ds1 = new DataSet();

then try. It should work then on

cheers
0
 
LVL 20

Author Comment

by:Sirees
ID: 13612981
I tried but no luck.
I think the prob is with the listbox1.SelectedIndexChanged() event. This event is firing immediately after listbox1.DataSource before setting the value and display member. Do you have any idea?
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13613184
I suggest you use seperate data adapters for different queries and see if that works.
I think this is what is causing the issue.
i havent used dataadapters in real projects.. that is where is the missing link
Also i suggest you use a dataset which is declared as a class variable instead of defining then and there.
Scoping may come into picture as well.

You can also comment all functionality but for filling up the troubled listbox and see...

cheers.
0
 
LVL 20

Author Comment

by:Sirees
ID: 13617847
Thanks for spending time on my issue. It was helpful. The problem was with the listbox1.selectedindexchanged event. It was firing the event  before populating the listbox. So I had to write a event handler for this.

0
 
LVL 20

Author Comment

by:Sirees
ID: 13617858
I will give you all the points for working on my issue for long time.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question