Nov 7, 2009

VB.NET: Problem With Listview Controls

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > BASIC / Visual Basic (.NET)

VB.NET: Problem With Listview Controls

vhortex
Hello, I am kinda new to Vb.net and I need some help regarding listview.

I want to add a certain value at a certain location for a listview. Like adding a new row at the current selected area.

What the program do as of now was to add new rows at location 0.

can anyone help me..

thanks..

Comment/Reply (w/o sign-up)

miCRoSCoPiC^eaRthLinG
QUOTE(vhortex @ Nov 12 2005, 08:25 PM)
Hello, I am kinda new to Vb.net and I need some help regarding listview.

I want to add a certain value at a certain location for a listview. Like adding a new row at the current selected area.

What the program do as of now was to add new rows at location 0.

can anyone help me..

thanks..
*




What do you mean by new row at the currently selected area ? Can you be a little more clear on that ?

ListView object won't add rows at location 0, as far as I know. Instead it would put it at the bottom.

If you mean that you want to add a row ABOVE or BELOW a single or a set of items in the listview, then you can take the following approach:

For a ListView you can set it's property MultiSelect to true. If so, an user might select a single row or multiple rows at a time. If set to false a user ca select only a SINGLE line.

Say, I want to insert a row below a selected row.
There's a method called SelectedIndices() under ListView class which returns an array containing the Index of the selected items in the list. The index of the first item is ZERO.

Now in case MultiSelect is set to false - SelectedIndices will return only a single row, but when MultiSelect is true, this will return an array.

If false, we can get the selected row (lets say, Max) by simply looking for it's index in the SelectedIndices() array's position ZERO - why we can be sure that this item will be in position ZERO is - that this array will containing ONLY ONE item, based on MultiSelect to be false.
So two cases:

Case 1: MultiSelect = false
CODE

Dim Max as Integer = lsvReceipts.SelectedIndices.Item (0)


OR,

Case 2: MultiSelect = true
If not, from that array you have to determine the highest index of the SelectedItems, to know which position you've to insert this new value.
Lets use a short example with this list of receipts ( lsvReceipts ):
CODE

           'We start by creating a variable called Max and store the first index in
           'SelectedIndices Array.
           Dim Max As Integer = lsvReceipts.SelectedIndices.Item(0)

           'We go through the SelectedIndices Array and find the highest index stored in it.
           'We loop through 0 to SelectedIndices.Count - 1, as we're counting from '0' .
           For Counter = 0 To lsvReceipts.SelectedIndices.Count - 1

               'Check with the item in each position - see if it's greater than Max
               If Max < lsvReceipts.SelectedIndices.Item(Counter) Then

                   'Set Max equal to it
                   Max = lsvReceipts.SelectedIndices.Item(Counter)

               End If

           Next

           'Now we know the Index of the Last Selected Item ( stored in Max )



So armed with the last selected item's position, we ADD a '1' to it, since what we want to insert, goes at a position below it. So
CODE

'Increase Max by '1'
Max += 1


Now, simply issue an insert statement:
CODE

'Insert new item at that position.
lsvReceipts.Items.Insert ( Max, "New Item" )


There you go... smile.gif





 

 

 


Comment/Reply (w/o sign-up)

vhortex
Oh, I am sorry, this should go to the VB.net portion.

I am going to edit the post and topic until I found out someone have mess with the proxy server and block the word listview.

here is the more detailed problem.


I have a listview in vb.net. The purpose of the listview is to display data to the screen which is fetch from a mySQL database.

The listview needs an Insert function where the purpose of the insert is to add a new row in the listview at the exact location where the user clicks [selected item] and move the rows displayed 1 line below down by 1 row.

this is a crude example of the code that I am facing
CODE
LVI = uiListViewSData.Items(0)
       indx = uiListViewSData.SelectedItems(0).Index

       uiListViewSData.Items().Insert((indx + 1), "")


I guess that the problem is with VB.net, for some reason the same code now runs.

There is no modification whatsoever..

Can you kindly move this topic to vb.net section for there are more people who may need this answer.


Comment/Reply (w/o sign-up)

miCRoSCoPiC^eaRthLinG
QUOTE(vhortex @ Nov 13 2005, 03:34 PM)
this is a crude example of the code that I am facing
CODE
LVI = uiListViewSData.Items(0)
       indx = uiListViewSData.SelectedItems(0).Index

       uiListViewSData.Items().Insert((indx + 1), "")


I guess that the problem is with VB.net, for some reason the same code now runs.

There is no modification whatsoever..
*



First - read the first post I made on this thread.

Now, your command, indx = uiListViewSData.SelectedItems(0).Index - fetches you the index of SelectedItems(0) - which should be okay, but sometimes I've had problems with the SelectedItems method - so I resort to using SelectedIndices() instead. See for yourself in this case, uiListViewSData.SelectedItems(0).Index is returning '0' as an index to your SelectedItems(0). Hence, quite obviously, your data WILL get added to the ZERO-eth position.

Since you're choosing just one row at a time, set MultiSelect of ListView to false and then use,
indx = uiListViewSData.SelectedIndices(0)
to fetch the selected row correctly.

Insertion is exactly what you're come up with:
uiListViewSData.Items().Insert((indx + 1), "New Item....")

Comment/Reply (w/o sign-up)

vhortex
I think you have mis-interpreted my post, anway the index stuff returns non zero because the requirements is that you must click the row first..

I used to use msgbox() to see what the returned value was.

---

it is working now without any modification..

my next question is if you still have time..

are there any method to make the listview editable by clicking a certain column and row in the listview?

---

about the indices i have coded a newer one with the indices that you have recommended but i have not yet rebuild the project .

Comment/Reply (w/o sign-up)

miCRoSCoPiC^eaRthLinG
QUOTE(vhortex @ Nov 13 2005, 05:37 PM)
my next question is if you still have time..

are there any method to make the listview editable by clicking a certain column and row in the listview?

---
*



Hehe.. I've used so much of listview during past two months, I could write a walkthrough blindfolder...

Anyways, basically there are 2 controls in VS.NET that can produce similar LIST outputs:
1. ListView
2. Datagrid

Both have advantages and disadvantages. Datagrid is way more complex but immensely powerful. ListView far easier (and works faster) - but limited in features.

With DataGrid, you can easily achieve what you want to do - EDIT any particular COLUMN in a ROW.

With ListView, unfortunately - you can edit ONLY the FIRST column of each ROW - which itself is an ITEM (which you add to the ListView)... all other columns associated with this ITEM are SUBITEMS, which are NON-EDITABLE. Anyways, to go ahead and edit only the first item, you've to set the ListView property called LabelEdit to TRUE.

In case you want to do the same with DataGrid (but be able to edit ALL) - let me know. I can help you out there too.

Regards,
m^e

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for vb, net, problem, listview, controls
advertisement



VB.NET: Problem With Listview Controls

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com