 |
|
|
 |
 |
|
Code (ASP)
<%
<asp:TemplateField HeaderText="Edit">
<HeaderStyle HorizontalAlign="Center" Width="30px"/>
<ItemTemplate>
<asp:ImageButton ID="btnEdit" Width="20px" Height="20px" runat="server" ImageUrl='img/001_45.gif' ToolTip="Edit" OnClick="btnEdit_Click" CommandArgument='<%# DataBinder.Eval(Container, "DataItem.ProductSeq").ToString() %>' CausesValidation="false"/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Bottom" />
</asp:TemplateField>
%>
//DataItem.ProductSeq คือข้อมูลที่อ้างอิงได้ว่ามาจากข้อมูล row ไหนในเทเบิล หรืออาจจะใช้ข้อมูลอื่นที่เป็น primary key ก็ได้ครับ ผมยกตัวอย่างกรณีของโปรแกรมผมนะครับขี้เกียจพิมพ์ใหม่
เมื่อกดที่ปุ่ม btnedit ก็จะได้ข้อมูลมา 1 row แล้วนะครับ
Code (C#)
protected void btnEdit_Click(object sender, EventArgs e)
{
ImageButton btnEdit = sender as ImageButton;
GridViewRow row = (GridViewRow)btnEdit.NamingContainer;
DataRow[] rowDetail = dtDetail.Select("ProductSeq = '" + btnEdit.CommandArgument.ToString() +"'"); //มาถึงตรงนี้จะได้ข้อมูลมา 1 row แล้วครับ ก็เอาไปทำการ add ลงอีก gridview นึง
}
|
 |
 |
 |
 |
Date :
2009-07-24 10:06:46 |
By :
besttooo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ประมาณนี้ได้ไหมครับ
Code (VB.NET)
'' ประกาศ C1 , C2 เป็น Global
Private Sub DataGridView1_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
If e.RowIndex = -1 Then Exit Sub
If DataGridView1.CurrentRow.Index < pro.Rows.Count Then '' เทียบข้อมูลใน datagridview กับ datatable
With DataGridView1
C1 = .Rows.Item(e.RowIndex).Cells(0).Value '' กรณี textbox, datetimepicker อยู่ในฟอร์มเดียวกัน
C2 = .Rows.Item(e.RowIndex).Cells(1).Value
End With
End If
Dim i As Integer = 0
If C1 = "" Then Exit Sub ''check ข้อมูลใน C1
For i = 0 To DataGridView2.Rows.Count - 1
With DataGridView2
.Rows.Item(i).Cells(0).Value = C1
.Rows.Item(i).Cells(1).Value = C2
End With
Next
End Sub

ผิดพลาดประการใดขอ อภัย ด้วยครับ
tee
|
 |
 |
 |
 |
Date :
2009-07-24 18:02:27 |
By :
lee_latee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่ได้เลยค่ะ ทำตามที่คุณ lee_latee บอกแล้วก็ยังไม่ได้เลยค่ะ มันไม่แสดงข้อมูล ใน datagridview ตัวที่2 ดังรูปที่คุณ lee_latee บอกค่ะ ยังไงช่วยหน่อยน่ะค่ะ ขอบคุณค่ะ
|
 |
 |
 |
 |
Date :
2009-07-25 23:48:00 |
By :
a |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนขอดูโค้ดหน่อยครับ
|
 |
 |
 |
 |
Date :
2009-07-28 11:50:08 |
By :
lee_latee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Imports System.Data.SqlClient
Imports System.Data
Public Class FormCheckReceived
Dim conn As SqlConnection
Dim ds As DataSet
Dim c1, c2, c3, c4, c5, c6, c7 As Global.System.String
Dim IsFind As Boolean = False
Dim dt As DataTable
Private Sub FormCheckReceived_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlClient.SqlConnection()
Dim cmd As New SqlClient.SqlCommand()
Dim sql2 As String = "SELECT id,date,order_id,pharmacist_id,supplier_id,quantity,total FROM received"
Dim cmd1 As New SqlClient.SqlCommand(sql2, conn)
Dim Result As New SqlCommand
Dim adapter As New SqlDataAdapter(cmd1)
Dim data As New DataSet()
conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\User\My Documents\Visual Studio 2008\Projects\PROJECT MORYA.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"
conn.Open()
cmd.Connection = conn
adapter.Fill(data, "received")
DataGridView1.DataSource = data.Tables("received")
'DataGridView1.Columns.Add("number", "number")
DataGridView1.Columns("id").HeaderText = "เลขที่ใบรับสินค้า"
DataGridView1.Columns("date").HeaderText = "วันที่รับสินค้า"
DataGridView1.Columns("order_id").HeaderText = "เลขที่ใบสั่งซื้อสินค้า"
DataGridView1.Columns("pharmacist_id").HeaderText = "รหัสเภสัชกร"
DataGridView1.Columns("supplier_id").HeaderText = "รหัสตัวแทนจำหน่าย"
DataGridView1.Columns("quantity").HeaderText = "จำนวน"
DataGridView1.Columns("total").HeaderText = "รวมเป็นเงิน"
'...............................................................................
conn.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim com As New SqlCommand
Dim conn As New SqlClient.SqlConnection()
Dim cmd As New SqlClient.SqlCommand()
conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\User\My Documents\Visual Studio 2008\Projects\PROJECT MORYA.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"
conn.Open()
com.CommandText = "Select * from received"
com.CommandType = CommandType.StoredProcedure
com.Connection = conn
cmd.Connection = conn
cmd.CommandText = ""
'cmd.ExecuteNonQuery()
Dim da As New SqlDataAdapter(com)
Dim ds As New DataSet
da.SelectCommand.Parameters.Add("BeginDate", SqlDbType.Date).Value = dtpstart.Value.ToString("s")
da.SelectCommand.Parameters.Add("EndingDate", SqlDbType.Date).Value = dtpend.Value.ToString("s")
If (IsFind = True) Then
ds.Tables("recevied").Clear()
End If
da.Fill(ds, "received")
If ds.Tables("recevied").Rows.Count <> 0 Then
IsFind = True
DataGridView1.ReadOnly = True
DataGridView1.DataSource = ds.Tables("received")
Else
IsFind = False
MessageBox.Show("ช่วงเวลาที่คุณระบุ ไม่มีรายการขาย", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Dim MyString As String = "12345"
Dim MyInt As Integer = Integer.Parse(MyString)
MyInt += 1
Console.WriteLine(MyInt)
' The result is "12346".
conn.Close()
End Sub
Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
process.Visible = True
End Sub
Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
process.Visible = False
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
If MessageBox.Show("ต้องการออกจากการตรวจสอบการรับสินค้าใช่หรือไม่", "ออกจากระบบ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then
Me.Close()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
If Me.DataGridView1.Rows.Count - 1 > 0 Then
If Not DataGridView1.CurrentRow.IsNewRow Then
MessageBox.Show("คุณต้องการลบข้อมูลใช่หรือไม่", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Question)
DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
Else
MsgBox("Is Noting DATA", MsgBoxStyle.Information)
End If
Else
MsgBox("Is Not DATA", MsgBoxStyle.Information)
End If
Me.Cursor = Cursors.Default
End Sub
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
Dim j As Integer = 0 '' วิธีแรกครับ
Dim SumPrice As Double = 0
For j = 0 To DataGridView1.Rows.Count - 1
SumPrice += CDbl(DataGridView1.Rows(j).Cells(6).Value)
Next
sales_total.Text = SumPrice.ToString("#,##0.00")
'...........แสดงจำนวนแถวใน datagridview..............................
TextBox2.Text = "" + DataGridView1.Rows.Count.ToString()
'................................................................
End Sub
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
If e.RowIndex = -1 Then Exit Sub
If DataGridView1.CurrentRow.Index < DataGridView1.Rows.Count Then '' เทียบข้อมูลใน datagridview กับ datatable
With (DataGridView1)
c1 = .Rows.Item(e.RowIndex).Cells(0).Value '' กรณี textbox, datetimepicker อยู่ในฟอร์มเดียวกัน
c2 = .Rows.Item(e.RowIndex).Cells(1).Value
c3 = .Rows.Item(e.RowIndex).Cells(2).Value
c4 = .Rows.Item(e.RowIndex).Cells(3).Value
c5 = .Rows.Item(e.RowIndex).Cells(4).Value
c6 = .Rows.Item(e.RowIndex).Cells(5).Value
c7 = .Rows.Item(e.RowIndex).Cells(6).Value
End With
End If
Dim i As Integer = 0
If c1 = "" Then Exit Sub ''check ข้อมูลใน C1
For i = 0 To DataGridView2.Rows.Count - 1
With (DataGridView2)
.Rows.Item(i).Cells(0).Value = c1
.Rows.Item(i).Cells(1).Value = c2
.Rows.Item(i).Cells(0).Value = c3
.Rows.Item(i).Cells(1).Value = c4
.Rows.Item(i).Cells(0).Value = c5
.Rows.Item(i).Cells(1).Value = c6
.Rows.Item(i).Cells(1).Value = c7
End With
Next
End Sub
Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
If e.RowIndex = -1 Then Exit Sub
If DataGridView1.CurrentRow.Index < DataGridView2.Rows.Count Then '' เทียบข้อมูลใน datagridview กับ datatable
With (DataGridView1)
c1 = .Rows.Item(e.RowIndex).Cells(0).Value '' กรณี textbox, datetimepicker อยู่ในฟอร์มเดียวกัน
c2 = .Rows.Item(e.RowIndex).Cells(1).Value
c3 = .Rows.Item(e.RowIndex).Cells(2).Value
c4 = .Rows.Item(e.RowIndex).Cells(3).Value
c5 = .Rows.Item(e.RowIndex).Cells(4).Value
End With
End If
Dim i As Integer = 0
If c1 = "" Then Exit Sub ''check ข้อมูลใน C1
For i = 0 To DataGridView2.Rows.Count - 1
With (DataGridView2)
.Rows.Item(i).Cells(0).Value = c1
.Rows.Item(i).Cells(1).Value = c2
.Rows.Item(i).Cells(2).Value = c3
.Rows.Item(i).Cells(3).Value = c4
.Rows.Item(i).Cells(4).Value = c5
End With
Next
End Sub
Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
'................แสดงหมายเลขลำดับที่...............
Using b As SolidBrush = New SolidBrush(Me.DataGridView1.RowHeadersDefaultCellStyle.ForeColor)
e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), _
Me.DataGridView1.DefaultCellStyle.Font, _
b, e.RowBounds.Location.X + 5, _
e.RowBounds.Location.Y + 5)
End Using
End Sub
Private Sub DataGridView2_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView2.RowPostPaint
'................แสดงหมายเลขลำดับที่...............
Using b As SolidBrush = New SolidBrush(Me.DataGridView2.RowHeadersDefaultCellStyle.ForeColor)
e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), _
Me.DataGridView2.DefaultCellStyle.Font, _
b, e.RowBounds.Location.X + 5, _
e.RowBounds.Location.Y + 5)
End Using
End Sub
End Class
|
 |
 |
 |
 |
Date :
2009-07-28 14:44:54 |
By :
a |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยหน่อยน่ะค่ะ ไม่ค่อยถนัดเลย
|
 |
 |
 |
 |
Date :
2009-07-28 14:45:32 |
By :
a |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณ lee_latee ครับ มันไม่รู้จักตัวนี้ครับ pro.Rows.Count แต่ Code ที่นำไปใช้ ใช้ได้ครับ ถ้าตัดตัวนี้ออกไป แต่ก็มีบัคตรงที่ว่าถ้าไปคลิกบริเวณส่วนหัวหรือท้ายสุดมันจะ Error ครับ
|
 |
 |
 |
 |
Date :
2009-07-30 11:48:50 |
By :
zerocool |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|