sijijames
asked on
Datagrid Events
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
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
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("Syste m.Decimal" )
.ColumnName = "price"
.DefaultValue = 50
End With
' Create the second, calculated, column.
cTax = New DataColumn
With cTax
.DataType = System.Type.GetType("Syste m.Decimal" )
.ColumnName = "tax"
.Expression = "price * 0.0862"
End With
' Create third column
cTotal = New DataColumn
With cTotal
.DataType = System.Type.GetType("Syste m.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
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("Syste
.ColumnName = "price"
.DefaultValue = 50
End With
' Create the second, calculated, column.
cTax = New DataColumn
With cTax
.DataType = System.Type.GetType("Syste
.ColumnName = "tax"
.Expression = "price * 0.0862"
End With
' Create third column
cTotal = New DataColumn
With cTotal
.DataType = System.Type.GetType("Syste
.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
ASKER
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.
In the previous example all values are assigned. But I want to give value at run time and do the calculation using these values.
Where do you assign the values ? In the datagrid or somewhere else ?
ASKER
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.
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.
So your datagrid isn't bound to a datatable or dataview ?
ASKER
sir,
I connect my datagrid to a Database. How I can display
the amt automatically. Please help me with an give example .
I connect my datagrid to a Database. How I can display
the amt automatically. Please help me with an give example .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 .
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 .
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
ASKER
sir,
I want to know how to display the contents of datatable to datagrid.please help me with an suitable code.
I want to know how to display the contents of datatable to datagrid.please help me with an suitable code.
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.ad d("AMT",ge ttype(sing le),"(qty * rate) + Tax")
datagrid1.datasource = ds.tables("DT")
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.ad
datagrid1.datasource = ds.tables("DT")
ASKER
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=PROJ ECTJ22;uid =sa;pwd=he llo;databa se=Purchas e")
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
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=PROJ
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
Hi,
in n = Dg.VisibleRowCount() 'dg is datagrid name
VisibleRowCount() will only return the number of rows visible and not the full row count.
in n = Dg.VisibleRowCount() 'dg is datagrid name
VisibleRowCount() will only return the number of rows visible and not the full row count.
ASKER
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 ?
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 ?
venki