• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1801
  • Last Modified:

adding data for selected row in datagrid to listbox

First of all i don't want to just bind the column in dataset to listbox and have it display all the data, i just want it to display list of items that in a column for a selected row(and it can have multiple rows under same id) appear. So table looks like:

id  Note
1   firstnote
2   second note
2   third note
2   4th note
3   5th note
....

so if a user selects a row with id =2 in datagid, then listbox should add Nots related to all the rows that have that selected it. In this case it should add those 3 notes if any of the id=2 is selected.

If i just bind listbox to datasource, set valuemember and display member, they just dump entire table's notes in that listbox. How can i make it so it will only do as i described above?
0
MellowD0c
Asked:
MellowD0c
  • 2
  • 2
1 Solution
 
jaynusCommented:
foreach(DataRow row in MyDataSet.Tables[0].Rows) {
     if(row.ItemArray[0] == MySelectedIdNumber) {                  // Check if the ID and the selected ID match
           MyListBox.Items.Add(row.ItemArray[1].ToString());      // Add notes to the list
     }
}
0
 
MellowD0cAuthor Commented:
private void dgBugTracker_SelectedCellChanged(object sender, System.EventArgs e)
            {

                  foreach(DataRow row in myDS2.Tables[0].Rows)
                  {
                        if(Convert.ToString(row.ItemArray[0]) == Convert.ToString(txtIssue.Text))
                        {   // Check if the ID and the selected ID match
                              lstNoteH.Items.Clear();
                              lstNoteH.Items.Add(row.ItemArray[41].ToString());      // Add notes to the list
                        }
                  }            
            }

Ok. What the above does is that if i select any row from datagrid it adds data for that selected row from required column, but its still not doing what i wanted. Actually right now its doing as if i would bind that row to some text box and have text box display value for that row with the exception that it doesn't get updated on selecting a different row with same id cause ostensibly i have check to match for id and since id doesn't change, therefore, it doesn't update the listbox either. Since everything else but that particular column value is different between same id's, is there any way else i could have it make a check for row change and add that particular column value from all rows of same id to the list box? Right now it just adds item of selected row to listbox and values from other exact same id's.

So back to my original example:

id  Note
1   firstnote
2   second note
2   third note
2   4th note
3   5th note
....

 It is adding only that id value that i select first of the 3, but i want to select id 2 and have it dump all the related notes from other two id's as well. So if i select id 2, i should have the following in listbox:

second note
----
third note
----
4th note
---

But as the code is right now, it only adds which ever "2" id i select first and doesn't change or add on selecting other same ids'.
0
 
MellowD0cAuthor Commented:
jaynus, it worked just had to place items.clear method elsewhere and i am all set. Thanks a bunch!
0
 
jaynusCommented:
Oops!!! Yah sorry I forgot to mention to clear it every change, my bad!! Apologies for that, Mellowdoc. Glad it works!

-jaynus
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now