 |
|
|
 |
 |
|
ผมว่าน่าจาเช็คตอน add ข้อมูลนะ ถ้าซ้ำก็ไม่ต้องให้มันบันทึกน่าจาง่ายกว่านะครับ
|
 |
 |
 |
 |
Date :
2009-12-14 16:20:09 |
By :
tordor012 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สร้าง function มาให้ไปวนลูปทุกแถวใน DataGridView2 เพื่อหาว่าซ้ำกับที่เลือกใน DGV1 หรือเปล่า
** อันนี้สงสัยนะ ทำไมของ DGV1 ไม่ทำเป็นแบบ checkbox ล่ะแล้วกด เลือกสุนัขทีเดียวไปที่ DGV2 เฉพาะที่เลือก
ตอนที่ย้ายไปก็ลบของฝั่ง DGV1 ออกด้วยมัน ทีนี้เค้าอยากจะเลือกซ้ำก็เลือกไม่ได้และ แล้วก็เพิ่มปุ่ม clear/reset selection ให้ เวลาอยากจะเลือกใหม่ ก็ลบใน DGV2 ออก แล้ว DGV1 ก็เพิ่มข้อมูลทั้งหมดเข้าไปใหม่
ลองดู มันไปได้หลายทางอยู่ คิดๆๆ 
|
 |
 |
 |
 |
Date :
2009-12-14 16:25:10 |
By :
salapao_codeman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังต้องการอยู่นะครับ ไม่ได้ทำปัญหานี้เลย เเต่ตอนนี้ต้องเอากลับมาทำครับเเต่ทำไม่ได้ซักทีครับ เครียดๆ
|
 |
 |
 |
 |
Date :
2010-02-02 00:00:38 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ผมต้องการให้ลบเเถวที่เลือกไปเเล้วครับ เวลาเราเลือกเเเถวเดียวมันลบให้ครับ เเต่ถ้าเราเลือก 2 เเถวมันจบลบเเถวที่ 2 ครับ
เเต่เเถวบนยังอยู่ครับ
เขียน Code ตรวจสอบเเบบนี้ครับ
Code (VB.NET)
Dim i As Integer = 0
'================== เลือกสุนัขเเล้วส่งไปหน้า sale ====================
For i = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Rows(i).Selected Then
Dim j As Integer = Sale.DataGridView2.Rows.Add()
Sale.DataGridView2.Rows(j).Cells(0).Value = DataGridView1.Rows(i).Cells(0).Value
Sale.DataGridView2.Rows(j).Cells(1).Value = DataGridView1.Rows(i).Cells(1).Value
Sale.DataGridView2.Rows(j).Cells(2).Value = DataGridView1.Rows(i).Cells(2).Value
Sale.DataGridView2.Rows(j).Cells(3).Value = DataGridView1.Rows(i).Cells(3).Value
Sale.DataGridView2.Rows(j).Cells(4).Value = DataGridView1.Rows(i).Cells(4).Value
End If
Next
Dim selectRow As Integer
selectRow = Me.DataGridView1.CurrentRow.Index
For i = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows().RemoveAt(selectRow)
Next
|
 |
 |
 |
 |
Date :
2010-02-03 12:43:14 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Dim selectRow As Integer
selectRow = Me.DataGridView1.CurrentRow.Index
For i = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows().RemoveAt(selectRow)
Next
คุณลองคิดดูว่า selectRow โดยลบไปแล้ว แถวต่อไปจะลบอะไรละ ในเมื่อคุณไม่ได้ focus แถวต่อไปให้มัน
คุณเขียนแบบนี้ดีกว่าครับ คุณลองแปลงเป็น vb เอานะ
Code (C#)
foreach (System.Windows.Forms.DataGridViewRow dr in dataGridView1.SelectedRows)
{
dataGridView1.Rows.Remove(dr);
}
|
 |
 |
 |
 |
Date :
2010-02-04 10:54:14 |
By :
numenoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณพี่ numenoy มากครับ
เดี๋ยวลอง มั่วๆแปลงๆ ดูครับ
ถ้าไม่ได้คงต้องรบกวนอีกครับผม
|
 |
 |
 |
 |
Date :
2010-02-04 12:39:12 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังทำไม่ได้เลยครับพี่ numenoy ผมไม่เข้าใจครับ ลองไปอ่านดูเรื่อง For Each เเล้วก็ยังงงอยู่ ไม่เคยใช้เลยด้วยครับ
เลยลองเขียน code เเบบนี้อะครับ เเต่ก็ยัง Error ครับ
รอบนี้ไม่สามารถเพิ่มข้อมูลลงไปได้เลยอะครับ
รบกวนพี่ช่วยดูทีครับผม
รูป Error ครับ

code
Code (VB.NET)
Dim selectRow As Integer
selectRow = Me.DataGridView1.CurrentRow.Index
For Each selectRow In DataGridView1.Rows()
DataGridView1.Rows().RemoveAt(selectRow)
Next
|
 |
 |
 |
 |
Date :
2010-02-06 17:05:12 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่ได้เลยคร๊าบบบบ 
|
 |
 |
 |
 |
Date :
2010-02-08 22:20:53 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองอันนี้หน่อยคุณเพื่อนว่าได้หรือเปล่า กวางลอง test กับโปรแกรมกวางแล้วมันลบค่าที่ต้องการได้น่ะ (event CellMouseDoubleClick) แต่อันนี้ลบได้ทีละแถวนะ ไม่รู้จะตรงกับที่ต้องการหรือเปล่า
Code (VB.NET)
Dim selectRow As Integer
selectRow = Me.DataGridview1.CurrentRow.Index
For i As Integer = 0 To DataGridview1.CurrentRow().Index
DataGridview1.Rows().RemoveAt(selectRow)
Next
ลองดูเน้อ
|
 |
 |
 |
 |
Date :
2010-02-09 00:40:02 |
By :
Kwang196 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
= ='
เอาไปลองดูครับ
Code (VB.NET)
For Each dgvr As System.Windows.Forms.DataGridViewRow In Me.DataGridView1.SelectedRows
Me.DataGridView1.Rows.Remove(dgvr)
Next
อันนี้มันเลือกทีละ rows จ๊ะ เจ้าของกระทู้อยากได้แบบเลือกหลายๆ rows แล้ว delete จ๊ะ
Code (VB.NET)
Dim selectRow As Integer
selectRow = Me.DataGridview1.CurrentRow.Index
For i As Integer = 0 To DataGridview1.CurrentRow().Index
DataGridview1.Rows().RemoveAt(selectRow)
Next
|
 |
 |
 |
 |
Date :
2010-02-09 08:25:31 |
By :
numenoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบใจจ้ากวาง เดี๋ยวชั้นขอลองไป test ก่อนเน้อ
ขอบคุณพี่ numenoy มากๆเช่นกันครับผม
|
 |
 |
 |
 |
Date :
2010-02-09 10:04:37 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำได้เเล้วคร๊าบบบบ ต้องใช้ตามแบบพี่ numenoy 
เพราะถ้าทำตามเเบบกวางอันนั้นเคยลองไปเเล้วครับ ลบได้เเค่เเถวเดียวเองงะ ยังไงก็ขอบใจกวางมากๆเน้อ ช่วยๆกัน อิอิ
|
 |
 |
 |
 |
Date :
2010-02-09 13:08:48 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เจอปัญหาอีกรอบครับ พอลบเเถวที่ต้องการลบมันลบข้อมูลในเเถวได้ครับ เเต่ลบไปเเต่ข้อมูลครับเเถวกลับไม่ลบครับ
คือ ผมให้ ดับเบิ้ลคลิ้ก เเล้วลบข้อมูลครับโดยการส่งค่ากลับไปตารางเดิมที่เราเลือกมาครับ ผลคือส่งข้อมูลกลับไปได้ครับ
เเต่อย่างที่บอกครับ เเถวกลับยังอยู่ครับ
รูปครับ

code
Code (VB.NET)
Private Sub DataGridView2_DoubleClick1(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView2.DoubleClick
Dim selectRow As Integer
Dim i As Integer
Dim temp1 As Integer = 0
Dim temp2 As Integer = 0
Dim sum As Integer = 0
selectRow = Me.DataGridView2.CurrentRow.Index
If Me.DataGridView2.Rows.Count <> 0 Then
If MessageBox.Show("ต้องการลบข้อมูลแถวนี้หรือไม่?", "Warning !!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
For i = 0 To DataGridView2.Rows.Count - 1 'ให้วนตั้งเเต่เเถวเเรกจนถึงเเถวสุดท้าย
If DataGridView2.Rows(i).Selected Then 'เเถวไหนก็ตามที่มีการเลือกให้ทำเงื่อนไขด้านล่าง
temp1 = DataGridView2.Rows(i).Cells(4).Value
temp2 = lb_totalPrice.Text - temp1
sum = temp2
sum = FormatNumber(sum, 2)
Dim j As Integer = DataGridView2.Rows.Add()
Sale_DogList.DataGridView1.Rows(j).Cells(0).Value = DataGridView2.Rows(i).Cells(0).Value
Sale_DogList.DataGridView1.Rows(j).Cells(1).Value = DataGridView2.Rows(i).Cells(1).Value
Sale_DogList.DataGridView1.Rows(j).Cells(2).Value = DataGridView2.Rows(i).Cells(2).Value
Sale_DogList.DataGridView1.Rows(j).Cells(3).Value = DataGridView2.Rows(i).Cells(3).Value
Sale_DogList.DataGridView1.Rows(j).Cells(4).Value = DataGridView2.Rows(i).Cells(4).Value
End If
Next
Me.DataGridView2.Rows.RemoveAt(selectRow)
lb_totalPrice.Text = sum.ToString("###,##0.00")
End If
End If
End Sub
|
 |
 |
 |
 |
Date :
2010-02-09 15:04:34 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มาช่วย up ค้าาา พอดีว่าทำ Project ร่วมกัน และติดปัญหานี้.....ยังแก้ไม่ได้เลย
ตั้งใจว่าจะทำให้เสร็จอาทิตย์ เหลืออยู่นิดหน่อยแล้วคะ ช่วยด้วยนะค้าาา
ขอบคุณคะ
|
 |
 |
 |
 |
Date :
2010-02-09 16:47:15 |
By :
numsom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
For Each dgvr As System.Windows.Forms.DataGridViewRow In Me.DataGridView2.SelectedRows
Dim dgr As System.Windows.Forms.DataGridViewRow = dgvr.Clone()
For i As Integer = 0 To dgvr.Cells.Count - 1
dgr.Cells(i).Value = dgvr.Cells(i).Value
Next
DataGridView1.Rows.Add(dgr)
Me.DataGridView2.Rows.Remove(dgvr)
Next
ตรง loop for น่ะ
อ่านของน้องแล้วดูยุ่งเยิงจัง เอานี้ไปลองใช้ดู ไวกว่านะ ^ ^"
|
 |
 |
 |
 |
Date :
2010-02-09 16:48:16 |
By :
numenoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองทำตามเเล้วครับ เเต่ไม่เข้าใจครับ
บรรทัดนี้คืออะไรอะครับพี่
Code (VB.NET)
Dim dgr As System.Windows.Forms.DataGridViewRow = dgvr.Clone()
เเล้วมันจะเอาข้อมูลที่ลบไปคืน DataGridView1 อีกหน้าได้ยังไงหรอครับ
|
 |
 |
 |
 |
Date :
2010-02-09 21:33:02 |
By :
numsom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สุดท้ายผมก็เอาเเบบง่ายดีกว่าครับ คือเช็คค่าของ 2 DataGridView เลยว่า Column 0 มีค่าตรงกันรึเปล่า
ถ้าตรงกันก็เเสดงว่าเลือกค่าซ้ำ เเต่ก็ยังติดปัญหาว่ารอบเเรกเลือกสุนัขได้เเต่เลือกรอบที่ 2 สุนัขไม่ซ้ำเเต่กลับเลือกไม่ได้ครับ
code ครับ
Code (VB.NET)
Dim i As Integer = 0
For i = 0 To Sale.DataGridView2.Rows.Count - 1
Dim temp_DogID As String = ""
temp_DogID = Sale.DataGridView2.Rows(i).Cells(0).Value
If temp_DogID = Me.DataGridView1.Rows(i).Cells(0).Value Then
MessageBox.Show("ซ้ำกรุณาเลือกใหม่", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
Next
|
 |
 |
 |
 |
Date :
2010-02-10 01:15:04 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ ๆ กวางทำได้แล้ว เป็นแบบนี้ อิอิ ลองดูนะว่า โน้ตได้ผลเป็นยังไง
Code (VB.NET)
Dim i As Integer = 0
For i = 0 To Sale.DataGridView2.Rows.Count - 1
Dim temp_DogID As String = ""
temp_DogID = Sale.DataGridView2.Rows(i).Cells(0).Value
If temp_DogID = Me.DataGridView1.CurrentRow.Cells(0).Value Then ' ใช้ CurrentRow เพื่อเอาค่าที่เราเลือก ณ ตอนนั้นไปเทียบกับ Grid ที่เราจะใส่ข้อมูล
MessageBox.Show("ซ้ำกรุณาเลือกใหม่", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
Next
|
 |
 |
 |
 |
Date :
2010-02-10 02:22:12 |
By :
Kwang196 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาใจช่วยจริงๆ โปรเจ็คน้องเนี้ย 555+
Code (VB.NET)
For Each dgvr As System.Windows.Forms.DataGridViewRow In Me.DataGridView2.SelectedRows 'บอกว่า loop ทุกๆ ค่า rows ที่เลือกไว้
Dim dgr As System.Windows.Forms.DataGridViewRow = dgvr.Clone() 'ให้ทำการ clone structure ของ row นี้ไว้ นั่นคือ ได้ columes ของ datagridview นี้ไว้ แต่ไม่ได้ values นะ
For i As Integer = 0 To dgvr.Cells.Count - 1
dgr.Cells(i).Value = dgvr.Cells(i).Value
Next 'loop นี้บอกว่า ให้ copy ค่าของทุกๆ cells ใน dgvr เข้าทุกๆ cells ของ dgr อันนี้จะได้ values แล้ว
DataGridView1.Rows.Add(dgr)' เพิ่ม rows ใหม่ใน datagridview1 นั่นก็คือ grid ของน้องที่อยากให้เอาค่าไปลงแหล่ะครับ
Me.DataGridView2.Rows.Remove(dgvr)'ลบ rows เก่าที่น้อง double click แหล่ะครับ
Next
พอติดตามโปรเจ็คน้องมาได้สักพัก จึงรู้ว่า น้องใช้ listview control จะดีกว่านะ แต่คงไม่ทันแล้ว...ก็ด้น code กันต่อไป สู้ๆ
|
 |
 |
 |
 |
Date :
2010-02-10 08:19:28 |
By :
numenoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โปรเจคผม มันยังไงหรอครับพี่ numenoy ช่วยชี้เเนะทีครับ เผื่อจะได้เอาไปปรับใช้กับการทำงานจริงครับ
ส่วนของกวางเดี๋ยวลองทำดู วันนี้ไปทำงานที่ ม. ป่าว ??
|
 |
 |
 |
 |
Date :
2010-02-10 09:45:32 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบใจเเกมากๆนะกวาง ทำได้เเล้ว อิอิ 
|
 |
 |
 |
 |
Date :
2010-02-10 09:53:34 |
By :
artcode |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โอเค...ได้แล้วโน้ะ
วันนี้ยังไม่รู้เลย...กะทำรายงานอีกวิชาให้เสร็จ ๆ ไปเลยง่ะ สู้ ๆ
|
 |
 |
 |
 |
Date :
2010-02-10 12:46:21 |
By :
Kwang196 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
int i = 0;
string tmpProduct = "";
for (i = 0; i < lsvProduct.Items.Count - 0; ++i)
{
tmpProduct = lsvProduct.Items[i].SubItems[0].Text;
if (lbl_pro_id.Text == tmpProduct)
{
MessageBox.Show("รหัสซ้ำ", "ผลตรวจ", MessageBoxButtons.OK, MessageBoxIcon.Information);
clearAllDataproduct();
this.txt_pro_id.Focus();
return;
}
|
 |
 |
 |
 |
Date :
2017-01-18 12:46:15 |
By :
เจษฎา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบกันข้ามปีเลยทีเดียว
|
 |
 |
 |
 |
Date :
2017-01-19 12:02:36 |
By :
parinya-t |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าเป็นโปรเจคนักศีกษา พวกคุณทำได้เท่านี้ก็ถือว่า "ดีแล้ว"
อันนี้พูดถึงบางคนเขียนโปรแกรมมาทั้งชีวิตแต่ไร้ซึ่งจินตนาการ (ไร้การพัฒนา)
เผื่อมีประโยชน์สำหรับผู้ที่ผ่านไปและผ่านมา
(เทคนิคการเล่นกับอีเวนต์ RemoveHandler/AddHandler) เพื่อเพิ่มประสิทธิภาพของโปรแกรม
--- DataGridView/ComboBox/etc... (น้อยคนนักที่จะคำนึงถึงจุดนี้)
Caller frmDataGridView1
Code (VB.NET)
Private Sub btnSelectDog_Click(sender As Object, e As EventArgs) Handles btnSelectDog.Click
Dim f As New frmDataGridView2
f.strDogCode = If(DataGridView1.SelectedRows.Count > 0 AndAlso DataGridView1.Columns.Count > 0, DataGridView1.SelectedRows(0).Cells("IDCardNo").Value, String.Empty)
f.srcDataGridView = DataGridView1
f.ShowDialog()
End Sub
Lookup Form (frmDataGridView2)
Code (VB.NET)
Public Class frmDataGridView2
Public strDogCode As String = String.Empty 'รหัสสุนัข
Public srcDataGridView As DataGridView = Nothing ' DataGridView1 control in frmDataGridView1
Public strDogCodeReturnValue As String = String.Empty
Private Sub frmDataGridView2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'เลือกระเบียนถ้าจำเป็น แต่ยังไม่ขออึ๊บ
RemoveHandler DataGridView2.SelectionChanged, AddressOf DataGridView2_SelectionChanged
DataGridView2.ClearSelection()
'Fill Data when you down.
DataGridView2.Rows.Add("1", "111")
DataGridView2.Rows.Add("2", "222")
DataGridView2.Rows.Add("3", "333")
DataGridView2.Rows.Add("4", "444")
If Not String.IsNullOrEmpty(strDogCode) Then
Dim tmpSelectedRow = (From r As DataGridViewRow In DataGridView2.Rows
Where r.Cells("IDCardNo").Value = strDogCode).FirstOrDefault()
If tmpSelectedRow IsNot Nothing AndAlso DataGridView2.Columns.Count > 0 Then
DataGridView2.CurrentCell = DataGridView2.Rows(tmpSelectedRow.Index).Cells(0)
End If
End If
AddHandler DataGridView2.SelectionChanged, AddressOf DataGridView2_SelectionChanged
End Sub
Private Sub DataGridView2_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView2.SelectionChanged
MessageBox.Show("ขออึ๊บได้ไหม?")
strDogCodeReturnValue = String.Empty
If srcDataGridView IsNot Nothing AndAlso DataGridView2.SelectedRows.Count > 0 Then
If Not DataGridView2.SelectedRows(0).Cells("IDCardNo").Value <> strDogCode Then
Dim lstDup = (From r As DataGridViewRow In srcDataGridView.Rows Where (Not r.IsNewRow AndAlso r.Cells("IDCardNo").Value = DataGridView2.SelectedRows(0).Cells("IDCardNo").Value)
Group r By key = r.Cells("IDCardNo").Value Into tmpGrp = Group
Where tmpGrp.Count() > 0
Select IDCardNO = key, cntIDCardNo = tmpGrp.Count()).ToList()
If lstDup IsNot Nothing AndAlso lstDup.Count > 0 Then
lstDup.ForEach(Sub(x)
MessageBox.Show(x.IDCardNO & " นับได้ " & x.cntIDCardNo)
End Sub)
Else
strDogCodeReturnValue = DataGridView2.SelectedRows(0).Cells("IDCardNo").Value
End If
End If
End If
End Sub
End Class
ปล. ได้ยินแต่เสียง สุนัขกับสุนัขคุยกัน แต่ไม่เคยได้ยินสิงห์กับสิงห์คุยกันมานานมากแล้ว
|
 |
 |
 |
 |
Date :
2017-01-20 10:15:08 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
DataGridView ว่ามีค่าซ้ำกันเขียนยังไงอ่าครับ
|
 |
 |
 |
 |
Date :
2017-08-20 14:31:28 |
By :
ben |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รยกวนด้วยครับ
|
 |
 |
 |
 |
Date :
2017-08-21 10:27:21 |
By :
man |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|