?
Solved

Datagrid Events

Posted on 2004-05-02
17
Medium Priority
?
263 Views
Last Modified: 2012-06-27
In my datagrid there are four fields (no,qty,rate and amt).
I want to display automaticaly to the amt field after multiplying qty and rate. Which datagrid event is used for this purpose.Please help me with an example
0
Comment
Question by:sijijames
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
17 Comments
 
LVL 21

Expert Comment

by:tovvenki
ID: 10974805
is it windows forms app or a we app??

venki
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10975186
You don't have to use an event for that, when you create the datagrid you can add extra columns

here is a nice example from MSDN

Private Sub CalcColumns()
     Dim cPrice As DataColumn
     Dim cTax As DataColumn
     Dim cTotal As DataColumn
     Dim rate As Single
     rate = .0862
     dim t as DataTable = New DataTable
 
     ' Create the first column.
     cPrice = New DataColumn
     With cPrice
         .DataType = System.Type.GetType("System.Decimal")
         .ColumnName = "price"
         .DefaultValue = 50
     End With
     
     ' Create the second, calculated, column.
     cTax = New DataColumn
     With cTax
         .DataType = System.Type.GetType("System.Decimal")
         .ColumnName = "tax"
         .Expression = "price * 0.0862"
     End With
     
    ' Create third column
     cTotal = New DataColumn
     With cTotal
         .DataType = System.Type.GetType("System.Decimal")
         .ColumnName = "total"
         .Expression = "price + tax"
     End With
 
     ' Add columns to DataTable
     With t.Columns
         .Add(cPrice)
         .Add(cTax)
         .Add(cTotal)
     End With
   
     Dim r As DataRow
     r = t.NewRow
     t.Rows.Add(r)
     Dim dView As New DataView
     dView.Table = t
     DataGrid1.DataSource = dView
 End Sub
0
 

Author Comment

by:sijijames
ID: 10975499
sir,
     In the previous example all values are assigned. But I want to give value  at run time and  do the calculation using these values.
 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10975528
Where do you assign the values ? In the datagrid or somewhere else ?
0
 

Author Comment

by:sijijames
ID: 10975787
I assign the values in the datagrid.


 Dim i, j, n As Integer
        n = Dg.VisibleRowCount()
        n = n - 2
        For i = 0 To n
        Dg(i, 5) = (Val(Dg(i, 2)) * (Val(Dg(i, 3)))) + (Val(Dg(i,4)))
        Next

I write this code inside the click event of a button.Using this code, I got the answer by clicking the button.But it is not display automatically in the amt field.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10975815
So your datagrid isn't  bound to a datatable or dataview ?
0
 

Author Comment

by:sijijames
ID: 10975955
sir,
     I connect my datagrid to a Database. How I can display
the amt automatically. Please help me with an give example .
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 80 total points
ID: 10976061
You can use the example a gave above this will automatically display amt

yourds.tables("yourtable").columns.add("AMT",gettype(single),"no * (qty + rate)")
yourdatagrid.datasource =  yourds.tables("yourtable")

put this in the load event of your form

0
 

Author Comment

by:sijijames
ID: 10976202
sir,
             My question is  that, how I could display the value automatically (when focus comes to the amt field , this field is already created in the design time) in the amt field .
ie, amt=(qty*rate)+tax .
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10976243
I really don't understand your question, my example shows the value automatically, even if you add new rows during runtime it will automatically calculate amt
0
 

Author Comment

by:sijijames
ID: 10976665
sir,
    I want to know how to display the contents of datatable to datagrid.please help me with an suitable code.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10976723
Ok, I'll try again
I assume your dataset is named DS and your datatable (DT)  looks like this :
No, QTY, Rate, Tax and that you want to calculate AMT automatically

form_LOAD event:

DS.tables("DT").columns.add("AMT",gettype(single),"(qty * rate) + Tax")
datagrid1.datasource =  ds.tables("DT")



0
 

Author Comment

by:sijijames
ID: 10984059
sir,
   My project is windows application.

My next  problem is that when data is inserted through datagrid to database only two rows are inserted other rows are avoided due some unknown reasons. My code is enclosed below.


Private Sub SvBn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SvBn.Click
        Dim con As New SqlConnection("server=PROJECTJ22;uid=sa;pwd=hello;database=Purchase")
        Dim a ,n As Integer
        Dim cmd As New SqlCommand()
        Dim cmd1 As New SqlCommand()
        cmd.Connection = con
        cmd1.Connection = con
        con.Open()
        n = Dg.VisibleRowCount()  'dg is datagrid name
               n = n - 1
        For a = 0 To n
            cmd.CommandText = "Insert into purtran values(" & PurIdTb.Text & " ," & Dg(a, 1) & "," & Dg(a, 2) & "," & Dg(a, 3) & "," & Dg(a, 4) & "," & Dg(a, 5) & "  ) "
            cmd.ExecuteNonQuery()
        Next
        cmd1.CommandText = "Insert into Purse values( " & InNoTb.Text & "," & PurIdTb.Text & "," & SupIdTb.Text & ",'" & DtpTb.Text & "') "
        cmd1.ExecuteNonQuery()
        con.Close()
End sub
0
 
LVL 21

Expert Comment

by:tovvenki
ID: 10984184
Hi,
in n = Dg.VisibleRowCount()  'dg is datagrid name
VisibleRowCount()  will only return the number of rows visible and not the full row count.
0
 

Author Comment

by:sijijames
ID: 10984233
sir,
    Can you please help me with a suitable example ?. I want to insert only the rows having values given at run time.Which method is used to count the rows having values ?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

771 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