PDA

View Full Version : حرفه ای: شماره گذاري خودكار رديفها در ديتاگريد



kafinetetaha
شنبه 17 مهر 1389, 12:11 عصر
باسلام من ميخام بعد از وارد شد رديف جديد بطور اتوماتيك خودش شماره رديف رو بزنه

hamedrokhian
شنبه 17 مهر 1389, 13:13 عصر
با سلام - این کد را در رویداد cellformatting وارد کن.
Dim i, j As Integer
For i = 0 To DataGridView1.Rows.Count - 1
j = i + 1
DataGridView1.Rows.Item(i).Cells.Item(0).Value = j
DataGridView1.Columns.Item(0).HeaderText = "ردیف"
Next
با تشکر:لبخندساده::خجالت:

ali_najari
شنبه 17 مهر 1389, 13:42 عصر
دوست عزيز علاوه بر راهي كه دوست عزيزمون پيشنهاد كردن ميتوني بخاطر ساده و سريعتر شدن برنامت اين كد رو در قسمت CellPainting اضافه كني اينطوري برنامت سريعتر ميشه زماني كه تعداد ركوردهات زياد باشه



If e.RowIndex >= 0 And e.ColumnIndex = 0 Then
Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
End If

ali_najari
شنبه 17 مهر 1389, 14:07 عصر
دوست عزيز اگر هم نميخواي كه توي ستونهات شماره رديف رو نشون بدي و ميخواي توي RowHeader شماره ستون رو بزاري ميتوني از كد زير استفاده كني

اين كد هم بايد توي CellPainting قرار بدي



If e.RowIndex >= 0 AndAlso e.ColumnIndex = -1 Then
Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor)
Dim gridBrush As New SolidBrush(Me.DataGridView1.GridColor)
Dim gridLinePen As New Pen(gridBrush)
Try
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
e.CellBounds.Bottom - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
e.CellBounds.Top, e.CellBounds.Right - 1, _
e.CellBounds.Bottom)
e.Graphics.DrawString(e.RowIndex, e.CellStyle.Font, _
Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, _
StringFormat.GenericDefault)
e.Handled = True
Finally
gridLinePen.Dispose()
gridBrush.Dispose()
backColorBrush.Dispose()
End Try


End If

DoDoklak
یک شنبه 18 مهر 1389, 15:51 عصر
البته راه دیگه ایی هم هست که به نظر من از روشهای فوق بهتره

http://s6.aks98.com/images/93983198818122291995.jpg


جهت پیاده سازی آن کافیه از رویداد RowPostPaint مربوط به DataGridView استفاده کنید


Private Sub DGV_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEvent Args) Handles DGV.RowPostPaint
Using b As SolidBrush = New SolidBrush(DGV.RowHeadersDefaultCellStyle.ForeColo r)
e.Graphics.DrawString("", DGV.DefaultCellStyle.Font, b, DGV.Size.Width - 40, 6)
e.Graphics.DrawString(e.RowIndex + 1, DGV.DefaultCellStyle.Font, b, e.RowBounds.Location.X + DGV.Size.Width - 40, e.RowBounds.Location.Y)
End Using
End Sub

alirezash
یک شنبه 18 مهر 1389, 16:33 عصر
دوستان عزیز ار روش
i+=1
چطور ؟ میشه استفاده کرد ؟