DataGrid Data Entry help

yousuf42

Well-known member
Joined
Feb 12, 2006
Messages
101
Programming Experience
1-3
Dear All,

below mentioned code is working and fetches data exactly as I wanted with one exception. That's, the ProductID, I enter goes into next row. all other data(ProdDescription,Uprice) goes into right cells of currentrow. so what's wrong here? why ProdID goes into next row? can any one figureout this please????

Your help is most appreciated..!

'Form level declaration
VB.NET:
Dim ProdIDTexBox As TextBox
Dim dgtbc As DataGridTextBoxColumn


' Code for a Column in Table Style
VB.NET:
dgtbc = dgInvoice.TableStyles(0).GridColumnStyles(1) '
If Not (dgtbc Is Nothing) Then
dgtbc.Width = 110
dgtbc.HeaderText = " Product ID"
dgtbc.TextBox.CharacterCasing = CharacterCasing.Upper
ProdIDTexBox = dgtbc.TextBox
AddHandler dgtbc.TextBox.Leave, AddressOf CellProdIDLeave  
End If


VB.NET:
Private Sub CellProdIDLeave(ByVal sender As Object, ByVal e As [SIZE=2]System.EventArgs[/SIZE]) 
 
Dim GetProductData As TechManagement.DBComponents.ProductDB = New TechManagement.DBComponents.ProductDB
Dim dtProductDetail As New DataTable
Dim dt As DataTable = DirectCast(dgInvoice.DataSource, DataTable)
 
Dim dr As DataRow = dt.NewRow()
If Not IsDBNull(ProdIDTexBox.Text) = False Or ProdIDTexBox.Text.Length > 2 Then
dtProductDetail = GetProductData.GetProductDetail(ProdIDTexBox.Text)
 
 
dr(2) = dtProductDetail.Rows(0).Item(1).ToString
dr(3) = dtProductDetail.Rows(0).Item(5).ToString
End If
dt.Rows.Add(dr)
End Sub
 
Can you post a sample form etc?

I've tried reading through a few times and am struggling to follow what's happening.
 
Thanks for your reply, Project is Attached here

Hi cjard, jwh and all,

Thanks for your time and Reply. Here I attached my project for your review. Your help is most appreciated.

Thanks a lot in advance
 

Attachments

  • TestPrj.zip
    56.6 KB · Views: 31
I dont see any problem.. after fixing the app to the minimum level needed to get it to run, apart from throwing hundreds of "IndexOutOfRange" exceptions, I had no problems entering data all on one row
 
Sorry, I Did not clarify you the real problem!!!

Thanks for your reply cjard,

I think, did not present the problem clearly.!!!

The problem is, When I enter ProductId into ProductID column of DataGrid and Leave(by pressing enter or Tab key) It fetches exact data into Description column and U Price column of DG, But ProductID I entered goes into next row instead of currentRow. That's why?? What is wrong here?

Just Try by entering
'101' or '102' which are productID's into productID column of DataGrid
and press enter
see the wrong results know

Description and U Price goes into right column's of currentrow

ProductID of currentRow goes into nextRow

and ProductID Column of currenrow changes into null Value!!!

(Please note that this application is used for Billing. That's when we enter ProductID and press enterkey, Description and UnitPrice are fetched automatically.)

Can you help me please!!!
Thanks a lot in Advance!!!!!1
 
Last edited:
VB.NET:
     Private Sub ProdIDCellLeave(ByVal sender As Object, ByVal e As System.EventArgs)       'Handles ProdDesc.Enter
        Dim GetProductData As TechManagement.DBComponents.ProductDB = New TechManagement.DBComponents.ProductDB
 
        Dim dtProductDetail As New DataTable
 
         Dim dt As DataTable = DirectCast(dgInvoice.DataSource, DataTable)
        Try
 
 
 
        Dim dr As DataRow = dt.NewRow()
 
 
        If Not IsDBNull(ProdIDTexBox.Text) = False Or ProdIDTexBox.Text.Length > 2 Then
 
            dtProductDetail = GetProductData.GetProductDetail(ProdIDTexBox.Text)
 
            dr(2) = dtProductDetail.Rows(0).Item(1).ToString
            dr(4) = dtProductDetail.Rows(0).Item(5).ToString
 
[B][COLOR=sandybrown]            dt.Rows.Add(dr)[/COLOR][/B]
 
        End If
        Catch ex As Exception
 
        End Try
 
    End Sub

umm.. of course it will add a new row, look at the code! you say rows.add!

I recommend that you look up the data and then populate the SAME ROW that the cell was on, dont add a row..
 
How to get same row(currentRow?) and populate that row

Thank you very much cjard,

if I remove 'dr.rows.add' then it fails to populate.

So how can we populate the same row? Should we get currentRow? Is there any method to get it and populate same row.

I would very much appreciate, If there is a sample code to do this- or someone modify my code to do this...

Thanks a lot in advance
 
Find the row the current cell relates to (in the cellLeave event, the EventArgs e should be able to give you the cell)

note, avoid getting the Current row from the grid.. if the user leaves the cell in a downwards or upwards motion, they changed rows... unless youre sure the event fires before the grid's current row property is updated
 
Sorry to bother you !!!

Thank you very much cjard,

Sorry to bother you.

Please note that cellLeave(ProdIDCellLeave)event is my user defined handler and It is not giving about currentrow or samerow (or I don't know how to get it???).

I Also tried something like these below. But I am stuck with this without any success for so long period.
VB.NET:
Dim dr As DataRow = DirectCast(dgInvoice.Item(dgInvoice.CurrentCell), DataRow)

VB.NET:
Dim dr As DataRow = dt.Rows(dgInvoice.CurrentRowIndex)

Thanks advance
 
I think you might have to ask the binding currency manager for the current row.. I dont use .NET 1.1, and this sort of thing is significantly simpler in .NET 2.0
 
Specified cast is not valid for Currency Manager

Thank you very much cjard,

I tried to bind Currency manager as following code but it throughs out exception: System.InvalidCastException: Specified cast is not valid

VB.NET:
Private Sub ProdIDCellLeave(ByVal sender As Object, ByVal e As System.EventArgs)  
Dim GetProductData As TechManagement.DBComponents.ProductDB = New TechManagement.DBComponents.ProductDB
Dim dtProductDetail As New DataTable
Dim dt As DataTable = DirectCast(dgInvoice.DataSource, DataTable)
Dim cm As CurrencyManager = DirectCast(dt.Rows, CurrencyManager)
 
Try
 
Dim dr As DataRow = cm.Current

If Not IsDBNull(ProdIDTexBox.Text) = False Or ProdIDTexBox.Text.Length > 2 Then
dtProductDetail = GetProductData.GetProductDetail(ProdIDTexBox.Text)
dr(2) = dtProductDetail.Rows(0).Item(1).ToString
dr(4) = dtProductDetail.Rows(0).Item(5).ToString

End If
Catch ex As Exception
End Try
End Sub

Also I installed .NET 2.0 as a way to solve problem as you suggested. Can I use .NET 2.0 with my VS2003? so How can I configure VS2003 to use .NET 2.0?

If it is simpler with 2.0 and I could configure to use 2.0 what is the next step to bind current row with .NET 2.0?

Sorry to bother you cjard

Thanks for your valuable time.
 
Thank you very much cjard,

I tried to bind Currency manager as following code but it throughs out exception: System.InvalidCastException: Specified cast is not valid

in OO languages, you cant just think "I need a CurrencyManager, i'll convert my rows into one" any more than in the real world you can say "I need a big bag of money, I'll convert an old bucket of horse crap into one"

I'm not really into .NET 1.1 so i cant tell you where the CurrencyManager lives..

Also I installed .NET 2.0 as a way to solve problem
It wont.
.NET apps written using VS2003 target the .NET 1.1 platform only

Can I use .NET 2.0 with my VS2003?
No

so How can I configure VS2003 to use .NET 2.0?
You install VS2005. It is available in several free versions


If it is simpler with 2.0 and I could configure to use 2.0 what is the next step to bind current row with .NET 2.0?

Dim ro as XXXDataRow = DirectCast(DirectCast(xXXBindingSource.Current, DataRowView).Row, XXXDataRow)

Sorry to bother you cjard
Its no bother, I just dont really know the territory, so I'm not much use.. Sorry!

Thanks for your valuable time.
Youre welcome!
 
Can anyone help Please! I'm going nowhere with DG,

Thank you very much cjard,

The only problem to upgrade to VS2005 is, I have Crystal Reports and other tools that I can't obtain with VS2005 free versions.

I tried following :-


Now I set up a data set through inserting a xsd file and added temp table to it. then I bound this to DG As below



VB.NET:
Private Sub customGridStyle()
Dim ds As New Dataset1
Dim ts As New DataGridTableStyle
ts.MappingName = "Temp"
ts.RowHeadersVisible = False
ts.HeaderBackColor = Color.DarkBlue
ts.HeaderForeColor = Color.Wheat
ts.GridLineStyle = DataGridLineStyle.Solid
ts.GridLineColor = Color.DarkBlue
 
 
Me.dgInvoice.TableStyles.Add(ts)
'Me.dgInvoice.DataSource = tblTempinvoice
dgInvoice.DataSource = Dataset11 ', "TempDataTable") ' "TempDataTable")
dgtbc = dgInvoice.TableStyles(0).GridColumnStyles(0)
If Not (dgtbc Is Nothing) Then
dgtbc.Width = 45
dgtbc.HeaderText = "S No"
dgtbc.Alignment = HorizontalAlignment.Center
End If
dgtbc = dgInvoice.TableStyles(0).GridColumnStyles(1) '
If Not (dgtbc Is Nothing) Then
dgtbc.Width = 110
dgtbc.HeaderText = " Product ID"
dgtbc.TextBox.CharacterCasing = CharacterCasing.Upper
ProdIDTexBox = dgtbc.TextBox
AddHandler dgtbc.TextBox.Leave, AddressOf ProdIDCellLeave ' Change ProdIDTexBox in place of dgtbc
 
End If
dgtbc = dgInvoice.TableStyles(0).GridColumnStyles(2) '
If Not (dgtbc Is Nothing) Then
dgtbc.Width = 281
dgtbc.HeaderText = " Description"
dgtbc.TextBox.CharacterCasing = CharacterCasing.Upper
'AddHandler dgtbc.TextBox.Enter, AddressOf DescEnter
ProdDesc = dgtbc.TextBox
End If
dgtbc = dgInvoice.TableStyles(0).GridColumnStyles(3)
If Not (dgtbc Is Nothing) Then
dgtbc.Width = 50
dgtbc.HeaderText = " Qty"
AddHandler dgtbc.TextBox.Leave, AddressOf QtyCellLeave
 
End If
dgtbc = dgInvoice.TableStyles(0).GridColumnStyles(4) '
If Not (dgtbc Is Nothing) Then
dgtbc.Width = 90
dgtbc.HeaderText = " U Price"
End If
dgtbc = dgInvoice.TableStyles(0).GridColumnStyles(5) '
If Not (dgtbc Is Nothing) Then
dgtbc.Width = 100
dgtbc.HeaderText = " Amount"
End If
 
End Sub

Then my ProdiCellLeave event as below:-



VB.NET:
Private Sub ProdIDCellLeave(ByVal sender As Object, ByVal e As System.EventArgs)  Dim GetProductData As TechManagement.DBComponents.ProductDB = New TechManagement.DBComponents.ProductDB
Dim dtProductDetail As New DataTable
 
Try
Dim cm As CurrencyManager = CType(Me.BindingContext(Me.Dataset11, "Temp"), CurrencyManager)
Dim dr As DataRow = cm.Current
 
 
If Not IsDBNull(ProdIDTexBox.Text) = False Or ProdIDTexBox.Text.Length > 2 Then
dtProductDetail = GetProductData.GetProductDetail(ProdIDTexBox.Text)
dr(2) = dtProductDetail.Rows(0).Item(1).ToString
dr(4) = dtProductDetail.Rows(0).Item(5).ToString
End If
Catch ex As Exception
End Try
End Sub
But no any improvement!!! I have done this in VB6. But in .Net Why it is too complex?? I don know why it is too hard in VS.net DG! can anyone help me please?!!

If any one know's other methods to acheive this I would appreciate.

It is similar to grocery's or other application where user enters ProdID and data is fetched to other columns(ie. uprice, description) for billing(or invoicing application)
 
Last edited:
Hmm.. true, CR doesnt come with the free versions, but I think you may find you can successfully use your VS2003 to design the reports, the free merge modules from CR themselves so your app can display the reports..

Crystal are a nice bunch.. They give you a trial key for their flagship XI every time you request one (i.e. every time your 30 day trial runs out) and free DLLs to show your reports. If youre that way inclined, you can use Crystal entirely without paying for it.. You dont get any support, but their support is rubbish anyway. (We never bothered buying after the trial because there were too many problems with it, and now use a different reports package)

However, onto more important things..

I never knew an app like what you speak, most the apps ive worked on, the barcode or product id is entered into a separate text box and the lookup result appears in the grid. however, you have chosen to use the grid..

I took the bull by the horns and did it for you.

Delete both your XXXLeave handlers, and paste this in instead:

VB.NET:
  Private Sub dgInvoice_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgInvoice.CurrentCellChanged

    Select Case prevCell.ColumnNumber
      Case 1
        Dim dtProductDetail As DataTable = getProductData.GetProductDetail( _
          dgInvoice.Item(prevCell).ToString())
        dgInvoice.Item(prevCell.RowNumber, 2) = dtProductDetail.Rows(0)(1)
        dgInvoice.Item(prevCell.RowNumber, 4) = dtProductDetail.Rows(0)(5)

      Case 3
        dgInvoice.Item(prevCell.RowNumber, 5) = _
          Convert.ToInt32(dgInvoice.Item(prevCell)) * _
          Convert.ToInt32(dgInvoice.Item(prevCell.RowNumber, 4))

    End Select

    prevCell = dgInvoice.CurrentCell


  End Sub
 
Back
Top