?
Solved

How to get the datagrid rows and set the combobox listindex

Posted on 2005-04-13
13
Medium Priority
?
641 Views
Last Modified: 2008-01-09
Hi All,
In VB6 I was able to get the total number of data rows that the grid contained and I was also able to set the combobox listindex to -1 (blank) after both items were populated. These features seem to have been ommitted in VB.NET. I need to be able to set the selected row of a bound and populated datagrid to its last row. I also need to be able to set a bound combo box's listindex or equivalent to -1 or to the index before the first item in its collection. I have looked everywhere on this site and in my books for these and can't find them. I'm a newby to VB.NET and don't understand classes yet, so go easy on me.
I have no trouble populating the datagrid from an Access DB and picking out distinct items fot the combobox, but this other thing stumps me!
Thanks in abvance for your help.
0
Comment
Question by:rdlemar
  • 5
  • 5
  • 3
13 Comments
 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13779189
I've not worked with bound combo-boxes...  but if setting it's selectedindex to -1 as you describe doesn't work for deselecting any items i.e.

ComboBox1.SelectedIndex = -1

have you tried setting it's selecteditem to Nothing

ComboBox1.SelectedItem = Nothing

=========================================

As for getting the row count... I'd hazard a guess that you retrieve this depending on what your datasource is... and depending on what type of datasource, e.g. DataTable, DataView, Dataset... you examine that to get the number of rows...  I'll warn you though, I've not used DataGrids so I might be talking nonsense here...  (sorry if i am... just seems like you've been waitin a while for a reply)
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13779304
About the combobox selected index -1 there is a bug in there,

http://support.microsoft.com/default.aspx?scid=kb;en-us;327244

They give 3 workarounds in that article , the easiest would be setting it twice

ComboBox1.SelectedIndex = -1
ComboBox1.SelectedIndex = -1

About the row count, S-Twilley is correct, you have to get row count from the underlaying datasource so if your datasource for the grid is

datagrid1.datasource = ds.tables(0)

to get the row count you would have to do

ds.tables(0).rows.count

there is a visiblerowcount property on the grid aswell but that gives as the names suggest only the visible rows :-)
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13779320
Thanx Ronald...  I hate giving advice I'm unsure of... I've yet to do any real work with Data controls (and datasets)... as well as a few other things in VB.NET... so my knowledge of them is limited...  so I resorted to plucking through the members of the datagrid control till I found a pattern/path to what seemed like a rowcount
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13779363
Good Morning (or maybe it is good afternoon where you are) S-Twilley,

The datagrid that comes with the Framework is pretty limited (too limited)  usually use the one from infragistics.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13779369
I just saw is it indeed good morning where you are :-)
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13779378
correct... although I have to say I kinda lose track of night / day / morning / afternoon... student (although I rarely attend) at the moment and not working so it's more a case of ... just woken up / been awake for a while / need to go to bed / should have gone to bed a long time ago but can't put this game down yet...  :P
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13779414
ahhh, student live, how I miss it :-)
0
 

Author Comment

by:rdlemar
ID: 13782044
Thanks S-Twilley for your quick response. Actually, I'm on the east coast of the U.S. so I posted my question at 11:31 pm my time and went off to sleep. I'm at work now (10:30 am), so I won't be able to try your suggestions until i get home (about 1:00pm pacific time).
Thanks
0
 

Author Comment

by:rdlemar
ID: 13782065
Thanks, RonaldBiemans for your comment. As I told S-Twilley, I will be able to try these things later today. I'm no longer a student either.
Thanks.
0
 

Author Comment

by:rdlemar
ID: 13786243
Hi S_Twilley, The Combobox code worked. Thanks. The Datagrid code was unsuccessful. Since RonaldBiemans answered that question in more detail, I will respond to him about that issue.
0
 

Author Comment

by:rdlemar
ID: 13786655
Hi RonaldBiemans, I tried your suggestion: Dataset11.tables(0).rows.count
Of course the underlying datasource is: CheckReg1.datasource = Dataset11.tables(0)
(I named datagrid1 "CheckReg1")

Here is some of the code I used:

Dim mysum As Integer
mysum = DataSet11.Tables(0).Rows.Count()

When I halted execution right after this statement and checked the value of mysum it was zero.
Mind you, the dataset was already populated with about 26 rows.

So, this did not work for me.
What I did to get it to work was a bit clunky but effective for a small number of rows:

I looped through the rows in the dataset until I got an error and then used that loop value -1 as the number of rows?

 Dim mysum As Integer
        mysum = 0
        Try
countup:
            CheckReg1.IsSelected(mysum)
            mysum = mysum + 1
            GoTo countup
        Catch
            'MsgBox("Number of rows is " & mysum - 1)
        End Try
        CheckReg1.CurrentRowIndex = mysum - 1

The message box was just in there for testing purposes and has been commented out for actual use.
This works, but is there a better way?

Thanks
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13788625
hi rdlemar,

The code I gave should work, so my guess is at the moment you used the code the dataset11.table(0) wasn't filled yet.
0
 

Author Comment

by:rdlemar
ID: 13790774
Hi RonaldBiemans,

You may be right or I may have done something else that prevented me from getting that value. Anyway, for now, I will just save the last row count into the database that I'm already using and retrieveit each time the app starts. This way I don't have to worry or count rows manually.

Thanks for your help. I will, when I get time, try to figure out what I did wrong and switch to your method when I nab it. It's important that I just get something up and running quickly. Why is the business world always like this?

rdlemar
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

850 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