Datagrid Current Position

N

No_So_Clever

Hi there,

I have th following Code to Get my Current Position and show it in a text
box, as "Record 1 of 129" for Example.
") + cmSystemUsers.Count.ToString) <<

I have a Datagrid on my Winform, When I click on one of the rows in My
datagrid I would like it to to indicate which record it is on e.g "Record 1
or 129", "Record 54 or 129", etc, etc, but it dosn`t update my
lblNavLocation.Text. How would I get it to update whn I click on a row in my
datagrid?

Many Kind Thanks in Avance
Regards
NSC
 
S

S.M. Altaf [MVP]

Me.Label1.Text = Me.DataGrid1.CurrentRowIndex.ToString() & " of " &
CType(Me.DataGrid1.DataSource, DataTable).Rows.Count - 1
 
N

No_So_Clever

Hi there,

Thanks for you reply.
Im getting errors on: CType(Me.DataGrid1.DataSource, DataTable).

I take it I change this to CType(me.dgdSystemUsers.dsSystemUsers, Passwords)

dgdSystemUsers been my DataGrid, dsSysetmUsers been my DataSet and Passwords
been my tabel. Is that right?

Many Thanks
Cheers
NSC
 
W

Wizard

Hi Cor,

Im Totally Confused now, why Currency Manager? I`ve been looking on the net
and it seems i need to use DataGrid1.CurrentCellChanged also some place say
use Dataviews. I don`t mean to knock you or your knowlage, or anything like
that as im sure you know more than a mear newbie like myself but im just
getting a little confused with everything at the moment:(

I can understand looking at the DataGrid1.CurrentCellChanged but i can`t see
anything on how to call it, or even any real coding for it which would work
in my Mini App.

Hope you can help.
Many Thanks
NSC
 
C

Cor Ligthert [MVP]

Wizard,

My expirience is that this one works on places where the ones you name don't
work.

You can by the way that one as well use with the datatview inside the
datatable.

Than the datasource becomes dt.defaultview and the setting to cma as well
dt.defaultview. (What I prefer, however I had written simple so I kept it as
simple as possible).

So try it and you see how easy it is in use.

:)

Cor
 
N

No_So_Clever

Hi Cor,

Sorry Now im really confused. How do I use this example on my project? You
example dosn`t even have a datagrid on it:(
Ive found this code that i`ve been playing with, but this on`t work either,
searching the net for Datagrid, Textbox Binding with Currency Manager dosn`t
throw me much help either:(
I could post my full code if that would help?:
Would it help of shold i use your example? Only thing with the example below
is I don`t know how to call it:( I mean it sits there, but what triggers it
when i click on a Row in my datagrid?

Private Sub dgdSystemUsers_CurrentCellChanged(ByVal sender As System.Object,
ByVal ne As System.Windows.Forms.NavigateEventArgs)
'Get the row number on the DataGrid
Dim iRownr As Integer = Me.dgdSystemUsers.CurrentCell.RowNumber
'Get the column number on the DataGrid
Dim iColnr As Integer = Me.dgdSystemUsers.CurrentCell.ColumnNumber
'Get the content of the cell in the clicked cell on the Datagrid
Dim cellvalue1 As Object = Me.dgdSystemUsers(iRownr, iColnr)
'Get the next cell content in the same row
Dim cellvalue2 As Object = Nothing
Try
cellvalue2 = Me.dgdSystemUsers(iRownr, iColnr + 1)
'Display (cellvalue1+cellvalue2) in TextBox "textBox1"
Me.lblNavLocation.Text = cellvalue1.ToString() + " " + cellvalue2.ToString()
Catch ex As Exception
'The exception occurs here because we increment iColnr+1
'Delete or comment MessageBox.Show-line you won´t get the error message
MessageBox.Show("No further columns after the last column-->> " +
ex.Message, "STOP")
cellvalue2 = Me.dgdSystemUsers(iRownr, iColnr - 1)
'Display this time (cellvalue2+cellvalue1) in TextBox
Me.lblNavLocation.Text = cellvalue2.ToString() + " " + cellvalue1.ToString()
End Try
End Sub

Many Thanks
NSC
 
C

Cor Ligthert [MVP]

No so clever,

Create a new project.
Drag a datagrid on the form
Drag 3 textboxes on the form
Go go the code
Paste in the code from the webpage
change at the error that you than sees datagridview in datagrid
at the 4 other ones you change label in textbox
start debug run

You will see in textbox 3 all the time the current row possition.

This should run and use less code than you are using now.

Cor
 
C

Cor Ligthert [MVP]

Than to make it complete for if somebody wants to sort.

make from the two time dt on the page dt.defaultview
DataGrid1.DataSource = dt.defaultview
Dim cma As CurrencyManager = DirectCast(BindingContext(dt.defaultview),
CurrencyManager)

Cor
 
N

No_So_Clever

Hi Cor,

Excellent - Got you Eample Working:))) Cheers Mate.

OK So in My Project I need to Add:

(I already have the following in my project) Dim WithEvents cmSystemUsers As
CurrencyManager

So Change:

Dim cma As CurrencyManager = DirectCast(BindingContext(dt), CurrencyManager)
To:

cmSystemUsers.DirectCast(BindingContext(dt), CurrencyManager) (What would go
in the Place of (DT) would this be my Dataset? i.e dsSystemUsers?

Then add: AddHandler cma.CurrentChanged, AddressOf CurrentChanged - into my
Forms Load

How would I change: Label3.Text = DirectCast(sender,
CurrencyManager).Position.ToString

To be the Equivilant of : Me.lblNavLocation.Text = (((cmSystemUsers.Position
+ 1).ToString + " of ") + cmSystemUsers.Count.ToString)

Many Thanks Again
NSC
 
C

Cor Ligthert [MVP]

Hi,

I wrote it already in the message I had send seperaat, however in the cma
datasource is the same as your datasource from your datagrid.

Cor
 
N

No_So_Clever

Hi Cor,

Sorry im been real stupid here. OK i have my App with your code in now and
it works great when I lick on a row it chnged and tells me it Record "?" of
Record "?"

My Code is as follows:-

'Set Databining
txtFirstname.DataBindings.Add("Text", dsSystemUsers, "Passwords.User_First")
txtSurname.DataBindings.Add("Text", dsSystemUsers, "Passwords.User_Last")
txtUsername.DataBindings.Add("Text", dsSystemUsers, "Passwords.Users_Name")
txtPassword.DataBindings.Add("Text", dsSystemUsers,
"Passwords.Users_Password")

'Initialize CurrencyManager for the table "Passwords"
cmSystemUsers = Me.BindingContext(dsSystemUsers, "Passwords")
cmSystemUsers = DirectCast(BindingContext(dsSystemUsers.Passwords),
CurrencyManager)
AddHandler cmSystemUsers.CurrentChanged, AddressOf CurrentChanged
and also

Private Sub dsSystemUsers_PositionChanged()
Me.lblNavLocation.Text = (((cmSystemUsers.Position + 1).ToString + " of ") +
cmSystemUsers.Count.ToString)
End Sub

Private Sub CurrentChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.lblNavLocation.Text = (((cmSystemUsers.Position + 1).ToString + " of ") +
cmSystemUsers.Count.ToString)
End Sub

Now when I Insert a row and add the Data it just inserts Null into all my
feilds:( When I check my database it hasn`t updated it with my inputs just
empty fields.

My Insert Code is:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Try
'Clear out the current edits
cmSystemUsers.EndCurrentEdit()
cmSystemUsers.AddNew()
Catch eEndEdit As System.Exception
System.Windows.Forms.MessageBox.Show(eEndEdit.Message)
End Try
Me.dsSystemUsers_PositionChanged()
txtFirstname.Enabled = True
txtSurname.Enabled = True
txtUsername.Enabled = True
txtPassword.Enabled = True
End Sub

Then I click a button to call Me.UpdateDataSet. Also if I say Edit Row 33
and change the First_Name from Mark to Say Bob it makes the changes in Row
1, no mater what Row I change it always updates Row 1. I was thinking maybe
I need to call the CurrentChanged in my Update code to get the correct Row
but not sure if i do or even how. Also now I have CurrenctChanged do i Still
need dsSystemUsers_PositionChanged() as I have got rid of my Navigation
buttons on my Form. I'm Also not sure totally about my CurrentChanged as it
hasn`t got the DirectCast bit in but i don`t know how to get that workin
with my code. Also how would I call CurrentChanged from my Code as
Me.CurrentChanged errors.

Any Ideas?
And Again Thnks for your Help So far.
NSC
 
N

No_So_Clever

Hi Cor,

Sorry but another prob i`ve noticed:(

The Following Code:

'Set Datagrid Data Source
dgdSystemUsers.DataSource = dsSystemUsers.Passwords
'dgdSystemUsers.SetDataBinding(dsSystemUsers, "Passwords")
'Set Databining
txtFirstname.DataBindings.Add("Text", dsSystemUsers, "Passwords.User_First")
txtSurname.DataBindings.Add("Text", dsSystemUsers, "Passwords.User_Last")
txtUsername.DataBindings.Add("Text", dsSystemUsers, "Passwords.Users_Name")
txtPassword.DataBindings.Add("Text", dsSystemUsers,
"Passwords.Users_Password")

'Initialize CurrencyManager for the table "Passwords"
cmSystemUsers = Me.BindingContext(dsSystemUsers, "Passwords")
cmSystemUsers = DirectCast(BindingContext(dsSystemUsers.Passwords),
CurrencyManager)
AddHandler cmSystemUsers.CurrentChanged, AddressOf CurrentChanged

When I have: 'dgdSystemUsers.SetDataBinding(dsSystemUsers, "Passwords")
enabled, as I click from Row to Row it shows what record im on in my
Nav.textBox. but doesn`t change my Texboxs tht are mapped to coloums in my
table. If I disable that and Enable:

txtFirstname.DataBindings.Add("Text", dsSystemUsers, "Passwords.User_First")
txtSurname.DataBindings.Add("Text", dsSystemUsers, "Passwords.User_Last")
txtUsername.DataBindings.Add("Text", dsSystemUsers, "Passwords.Users_Name")
txtPassword.DataBindings.Add("Text", dsSystemUsers,
"Passwords.Users_Password") It Changes my textbox`s but dosn`t upate my
Nav.Texbox.

If I enable both it tells me Bindings have Failed.

Any Clues or Ideas?

Would it be better if I mailed you my or put it somehwhere on the Web?

Many Thanks
NSC
 
C

Cor Ligthert [MVP]

No So Clever,

There is no need to sent all code, try it with snippets, if there is needed
more than it is asked mostly. This is to much for a newsgroup.
There is to much in it, but that you will see in future.

Nevertheless.
cmSystemUsers.EndCurrentEdit()
cmSystemUsers.AddNew()

It adds always forever at the end of the datasource. Therefore you know that
you can add the key add
thedatasource(thedatasource.Rowcount - 1)(KeyField) = theTnewKey

I hope this helps,

Cor
 
N

No_So_Clever

Hi Cor,

OK i`ve gone back to very Basics with my Code:

Private Sub frmSystemUsers_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'File Datasets
dsSystemUsers.EnforceConstraints = False
Me.odcSystemUsers.Open()
odaSystemUsers.Fill(dsSystemUsers)
'Set Datagrid Data Source
dgdSystemUsers.DataSource = dsSystemUsers.Passwords
'Set Databining
txtFirstname.DataBindings.Add("Text", dsSystemUsers,
"Passwords.User_First")
txtSurname.DataBindings.Add("Text", dsSystemUsers,
"Passwords.User_Last")
txtUsername.DataBindings.Add("Text", dsSystemUsers,
"Passwords.Users_Name")
txtPassword.DataBindings.Add("Text", dsSystemUsers,
"Passwords.Users_Password")
'Initialize CurrencyManager for the table "Passwords"
cmSystemUsers = Me.BindingContext(dsSystemUsers, "Passwords")
cmSystemUsers = DirectCast(BindingContext(dsSystemUsers.Passwords),
CurrencyManager)
AddHandler cmSystemUsers.CurrentChanged, AddressOf CurrentChanged
Me.odcSystemUsers.Close()
End Sub

Private Sub CurrentChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.lblNavLocation.Text = (((cmSystemUsers.Position + 1).ToString + " of
") + cmSystemUsers.Count.ToString)
End Sub

Any Ideas why the Above won`t work, its OK and calls my CurrentChanged and
tells me my Record of Record, but my Text Boxs don`t change when I click on
a rown in my Grid. They simply Stay showing Row one:(

Any Ideas
Many Thanks
NSC
 
C

Cor Ligthert [MVP]

Probably because your datagrid has another datasource than that you use in
the binding of the textboxes.

dsSystemUsers.Passwords

I hope this helps,

Cor
'Set Datagrid Data Source
dgdSystemUsers.DataSource = dsSystemUsers.Passwords
'Set Databining
txtFirstname.DataBindings.Add("Text", dsSystemUsers,
"Passwords.User_First")

Cor
 
N

NOT_SO_CLEVER

Hi Cor,

Thanks for you reply. Sorry im lost again. Both my Datasource`s are
dsSystemUsers.Passwords.All I have on my App is 1 DataSet. Could you explain
a bit more?

Im very sorry about been sooooooo Stupid Cor, but im a newbie and really
finding it hard to understand some of these things. I have some books on the
way tho:)

Many Thanks
Si
 
C

Cor Ligthert [MVP]

Si,

No see as I showed you.

You have
Datagrid dsSystemUsers.Passwords
Normal Controls: dsSystemUsers

So in my opinion they have to be all
Datagrid dsSystemUsers.Passwords
(The table)

:)

Cor
 
N

NOT_SO_CLEVER

Excellent, Now its working, good im so daft:). Cheers for that Cor

Tell me if im been in pain now, but its just my insert code now thats not
working:(

You said "It adds always forever at the end of the datasource. Therefore you
know that
you can add the key add
thedatasource(thedatasource.Rowcount - 1)(KeyField) = theTnewKey"

So if my Insert code is:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
cmSystemUsers.EndCurrentEdit()
cmSystemUsers.AddNew()
Me.dsSystemUsers_PositionChanged()
txtFirstname.Enabled = True
txtSurname.Enabled = True
txtUsername.Enabled = True
txtPassword.Enabled = True
End Sub

Do I just add that before the .EndCurrentEdit?

dsSystemUsers(dsSystemUsers.Passwords.RowCount -1)(?????) = ????? - What
do I need to add in the ????? bits?

Many Thanks Again, and again sorry for been a pest

Cheers
 
C

Cor Ligthert [MVP]

Si,

Greath it works now,

Can you make a new post for this, now this becomes a kind of endless help,
this is another problem you know?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top