 |
|
การ หาผลรวมของ datagrid ทั้งหมดตามรูปที่แนบมาเลย ครับ |
|
 |
|
|
 |
 |
|
Code (VB.NET)
GridView1. FooterRow. Cells[2]. FindControl("Label2")) .Text = myDataSet. Tables[0]. Compute("sum(amount)", ""). ToString()
ส่วนที่ใส่อะลองใส่ใน With ดูนะ ผมก็ไม่เคยใช้ เเต่ลองไปหาดูเขาบอกว่าใส่อันนี้เขาไปเเต่เขาไม่บอกว่าใส่ตรงไหนของ Code นะครับ
|
 |
 |
 |
 |
Date :
2010-12-27 13:18:31 |
By :
EucifeR |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับที่ตอบ
แต่ไม่รู้ใช้งานยังงัย งง เลย
|
 |
 |
 |
 |
Date :
2010-12-27 15:42:16 |
By :
zagi |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
[Code (C#)
public partial class _Default : System.Web.UI.Page
{
int i=0;
double TotalPrice =0.0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
i++;
TotalPrice+=Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,"UnitPrice"));
}
else if(e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Font.Bold=true;
e.Row.Cells[0].Text="ราคารวม:";
e.Row.Cells[2].Text=TotalPrice.ToString("#,###0.00");
}
}
เราเอามาจาหนังสืออะค่ะ โค้ดC#อะค่ะ ในช่องที่โชว์ราคารวม ในนี้ให้โชว์คอมลัมที่2นะค่ะ ของคุณเป็นยังไงแก้ได้เลยย
ลองเอามาให้ใช้ดูอะค่ะ ส่วน UnitPrice เป็นชื่อฟิลด์อะค่ะ
|
 |
 |
 |
 |
Date :
2010-12-27 21:32:38 |
By :
somjang99 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้า code ผมเป็นแบบนี้ละครับ ต้องแก้ไขยังงัยบ้าง
Code (VB.NET)
Me.DataGridView1.DataSource = dt
Me.DataGridView1.Columns.Clear()
Dim column As DataGridViewTextBoxColumn
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "voucherno"
column.HeaderText = "เลขที่ใบเบิก"
column.Width = 85
Me.DataGridView1.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "itemcode"
column.HeaderText = "รหัสสินค้า"
column.Width = 85
Me.DataGridView1.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DefaultCellStyle.Format = "0.00"
column.DataPropertyName = "mainquantity"
column.HeaderText = "จำนวนที่เบิก"
column.Width = 75
Me.DataGridView1.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DefaultCellStyle.Format = "0.00"
column.DataPropertyName = "price"
column.HeaderText = "ราคารวม"
column.Width = 80
Me.DataGridView1.Columns.Add(column)
|
 |
 |
 |
 |
Date :
2010-12-28 09:37:00 |
By :
zagi |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
การใช้งานในลักษณะของฟอร์มใบกำกับ ใบส่งของ มักไม่นำผลรวมไปใส่ไว้ในตาราง แต่จะสร้างบรรทัดผลรวมไว้ด้านล่างของตาราง
เนื่องจากจะมีความยุ่งยากในการเพิ่มหรือลดรายการ
|
 |
 |
 |
 |
Date :
2010-12-28 09:50:05 |
By :
base3000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีใครพอรู้บ้างรึเปล่าครับว่าต้องเขียนยังงัย
รบกวนชี้แนะแนวทางด้วยครับ
|
 |
 |
 |
 |
Date :
2010-12-29 08:33:40 |
By :
zagi |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คำแนะนำ คือ ทำตามคุณ tong
|
 |
 |
 |
 |
Date :
2010-12-29 09:23:01 |
By :
ตังค์แมน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Copy ไปใส่
Code (VB.NET)
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellValueChanged
Dim Sum As Decimal
If e.ColumnIndex = 2 Then
For Each dgr As DataGridViewRow In Me.DataGridView1.Rows
If dgr.Cells(2).Value IsNot DBNull.Value Then
Sum += dgr.Cells(2).Value
End If
Next
MessageBox.Show("จำนวนที่รวม = " & Sum)
ElseIf e.ColumnIndex = 3 Then
For Each dgr As DataGridViewRow In Me.DataGridView1.Rows
If dgr.Cells(3).Value IsNot DBNull.Value Then
Sum += dgr.Cells(3).Value
End If
Next
MessageBox.Show("จำนวนเงินรวม = " & Sum)
End If
End Sub
|
 |
 |
 |
 |
Date :
2010-12-29 11:05:14 |
By :
base3000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษนพครับพี่ copy แล้ว ไม่มีอะไรเกิดขึ้นเลยครับ
ต้องทำยังงัยต่อครับ
|
 |
 |
 |
 |
Date :
2010-12-29 13:23:30 |
By :
zagi |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาเต็ม ๆ มาแปะให้ดูหน่อยซิ
|
 |
 |
 |
 |
Date :
2010-12-29 13:46:29 |
By :
base3000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Sub scap()
Dim dt As New DataTable
Dim mycommand As New OdbcCommand
Dim Adapter As OdbcDataAdapter
Dim ds As DataSet = New DataSet
Dim cn As New OdbcConnection("dsn=costmo;uid=sa;pwd=")
cn.Open()
Dim cmd = " select voucherno,itemcode,mainquantity,inoutvaluei " & _
" from inventoryflowdtl " & _
" where mono = '" & voucherno & "' and approved = '1' and reasonindex = '4' and inout = '1' "
mycommand.Connection = cn
mycommand.CommandText = cmd
Adapter = New OdbcDataAdapter(mycommand)
'Dim Adapter As SqlDataAdapter
Adapter.Fill(ds, "table2")
dt = ds.Tables("table2")
cn.Close()
Me.DataGridView3.DataSource = dt
Me.DataGridView3.Columns.Clear()
Dim column As DataGridViewTextBoxColumn
column = New DataGridViewTextBoxColumn()
column.DefaultCellStyle.BackColor = Color.BlanchedAlmond
column.DataPropertyName = "voucherno"
column.HeaderText = "เลขที่ใบเบิก"
column.Width = 80
Me.DataGridView3.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "itemcode"
column.HeaderText = "รหัส SCG"
column.ReadOnly = True
column.Width = 70
Me.DataGridView3.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DefaultCellStyle.Format = "0.00"
column.DataPropertyName = "mainquantity"
column.HeaderText = "จำนวนที่คืน"
column.ReadOnly = True
column.Width = 70
Me.DataGridView3.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DefaultCellStyle.Format = "0.00"
column.DataPropertyName = "inoutvaluei"
column.HeaderText = "ราคารวม"
column.ReadOnly = True
column.Width = 70
Me.DataGridView3.Columns.Add(column)
End Sub
ใจเย็นๆๆนะครับพี่
ผมหัดเขียนอยู่ ครับ
|
 |
 |
 |
 |
Date :
2010-12-29 13:56:50 |
By :
zagi |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กำลังหาอยู่ครับ ไม่ใช่ว่าไม่ทำอะไรเลย พอดีพี่เค้าช่วยดูครับ
ถ้าไม่ได้ก็ไม่เป็นไรครับ พี่ๆๆๆ ขอบคุณมากครับ
แล้วผมจะลองหาวิธีอื่นดูครับ ขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2010-12-29 14:11:34 |
By :
zagi |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

Code (VB.NET)
SumValue(2)
SumValue(3)
เอาสองชุดนี้ไปแปะไว้นอก sub
Code (VB.NET)
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellValueChanged
If e.ColumnIndex = 2 Or e.ColumnIndex = 3 Then
SumValue(e.ColumnIndex)
End If
End Sub
Private Sub SumValue(ByVal ColumnIndex As Integer)
Dim Sum As Decimal
For Each dgr As DataGridViewRow In Me.DataGridView1.Rows
If dgr.Cells(ColumnIndex).Value IsNot DBNull.Value Then
Sum += dgr.Cells(ColumnIndex).Value
End If
Next
MessageBox.Show("ผลรวมของ" & Me.DataGridView1.Columns(ColumnIndex).HeaderText & " = " & Sum)
End Sub
เอา Sum ไปประยุกต์ใช้เอง
|
 |
 |
 |
 |
Date :
2010-12-29 14:40:58 |
By :
base3000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เดี่ยวผมลองนะครับพี่
ขอบคุณมากๆๆครับ ที่พี่ยังตอบ
|
 |
 |
 |
 |
Date :
2010-12-29 14:47:52 |
By :
zagi |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ตามนี้เลยครับพี่
|
 |
 |
 |
 |
Date :
2010-12-29 15:00:01 |
By :
zagi |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2010-12-29 15:09:10 |
By :
base3000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โชคดีนะครับ ความชำนาญไม่ได้เป็นในวันสองวันหรอกนะ
จากที่ดู จขกท ต้องฝึกอีกเป็นปีเลย ไม่ยังคนช่วยเหนื่อย
|
 |
 |
 |
 |
Date :
2010-12-29 15:43:07 |
By :
ตังค์แมน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใครก็ได้ ช่วยบอกโค้ดลบออกจาก DataGridView แล้วผลคำนวณยอดรวม ถูกลบออกไปตามที่ถูกเลือกค่ะ

ภาพดังกล่าวเป็นภาพโค้ดเพิ่มรายการลงใน DataGridView ค่ะ ซึ่งไม่มีปัญหาอะไร แต่ทีนี้มีปัญหาที่ไม่รู้ว่า จะเลือกแถวไหนก็ได้แล้วสามารถลบออกและยอดรวมถูกหักออกไปด้วยค่ะ
|
 |
 |
 |
 |
Date :
2021-12-27 15:27:38 |
By :
Nest |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|