تفضل صديقي ، حسب راي افضل طريقه للتعامل مع ال gridview
كود :
Sub FillGrid_yearly()
DataGrid1.CancelEdit()
DataGrid1.Columns.Clear()
DataGrid1.DataSource = Nothing
'Delete Button
Dim btnDelete As New DataGridViewButtonColumn()
btnDelete.FlatStyle = FlatStyle.Flat
Dim datee = New CalendarColumn() With {.HeaderText = "التاريخ"}
datee.DefaultCellStyle.Format = "dd-MM-yyyy"
datee.DataPropertyName = "Date"
DataGrid1.Columns.Insert(0, btnDelete)
DataGrid1.Columns.Add("", "أسم المستاجر")
DataGrid1.Columns.Add("", "رقم الهوية")
DataGrid1.Columns.Add("", "رقم العقد")
DataGrid1.Columns.Add("", "رقم المحل")
DataGrid1.Columns.Add("", "قيمة الدفعة")
DataGrid1.Columns.Add("", "طريقة الدفع")
DataGrid1.Columns.Add(datee)
DataGrid1.Columns.Add("", "الملاحظات")
DataGrid1.Columns.Add("", "id")
DataGrid1.Columns.Add("", "")
DataGrid1.Columns(9).Visible = False
Dim sqlstring As String = "Select * FROM payments_view ORDER BY date DESC"
'فحص الاتصال بقاعدة البيانات
If SQL.conn.State = ConnectionState.Open Then
SQL.conn.Close()
End If
SQL.conn.Open()
Dim adt As New SqlDataAdapter(sqlstring, SQL.conn)
Dim dt As New DataTable
adt.Fill(dt)
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
DataGrid1.Rows.Add()
DataGrid1.Rows(i).Cells(1).Value = dt.Rows(i).ItemArray(1)
DataGrid1.Rows(i).Cells(2).Value = dt.Rows(i).ItemArray(2)
DataGrid1.Rows(i).Cells(3).Value = dt.Rows(i).ItemArray(3)
DataGrid1.Rows(i).Cells(4).Value = dt.Rows(i).ItemArray(4)
DataGrid1.Rows(i).Cells(5).Value = dt.Rows(i).ItemArray(5)
DataGrid1.Rows(i).Cells(6).Value = dt.Rows(i).ItemArray(6)
DataGrid1.Rows(i).Cells(7).Value = Format(dt.Rows(i).ItemArray(8), "dd-MM-yyyy")
DataGrid1.Rows(i).Cells(8).Value = dt.Rows(i).ItemArray(9)
DataGrid1.Rows(i).Cells(9).Value = dt.Rows(i).ItemArray(0)
Next
SQL.conn.Close()
Dim rowNumber As Integer = 1
For Each row As DataGridViewRow In DataGrid1.Rows
If row.IsNewRow Then Continue For
row.HeaderCell.Value = "" & rowNumber
rowNumber = rowNumber + 1
Next
DataGrid1.Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
'كود خاص بتخطيط الجريد فيو
DataGrid1.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal
Me.DataGrid1.GridColor = Color.WhiteSmoke
End Sub
من خلال هذا الكود يمكنك اضافع صوره ( صوره X مثلا ) لحذف سطر
كود الصوره :
كود :
Private Sub DataGrid1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGrid1.CellPainting
Try
If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
Dim bmpFind As Bitmap = My.Resources.Delete_25
Dim ico As Icon = Icon.FromHandle(bmpFind.GetHicon)
e.Graphics.DrawIcon(ico, e.CellBounds.Left, e.CellBounds.Top - 1)
e.Handled = True
End If
Catch ex As Exception
End Try
End Sub
ويمكنك حذف السطر باستخام هذا الكود :
كود :
Dim id As Integer
Private Sub DataGrid1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGrid1.CellContentClick
id = DataGrid1.SelectedRows(0).Cells(9).Value
If e.ColumnIndex = 0 Then
If MessageBox.Show("هل تريد حقا حذف هذة الدفعة نهائيا ؟", "حذف", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
Return
End If
Try
SQL.conn.Open()
Dim sqlstring As String = "delete from payments where id=" & id
Dim cmd As New SqlCommand(sqlstring, SQL.conn)
cmd.ExecuteNonQuery()
SQL.conn.Close()
Payments.pay_NotifyIcon.Visible = True
Payments.pay_NotifyIcon.ShowBalloonTip(1000, "مجمعات ابو الأسد التجارية", "تم حذف السجل بنجاح", ToolTipIcon.Info)
Payments.Timer1.Start()
FillGrid_yearly()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
ويمكند تحديث البيانات و العديد من الاشياء