سؤال

وكيف تحصل على الصف عدد الخلايا داتاغريدفيف؟ على وجه التحديد، إذا كان المستخدم قد اختار خلية واحدة، وكيف يمكن أن تحصل على رقم الصف؟ فإنه يحتاج إلى الوصول إلى خلية معينة بناء على ما اختارت المستخدم.

وأنا أعلم أن طريقة RemoveAt يمكن استخدامها لإزالة في التركيز، ولكن لا يمكنك الحصول على رقم الصف في التركيز على ما يبدو؟

وشكرا للمساعدة!

هل كانت مفيدة؟

المحلول

ويمكنك ببساطة استخدام RowIndex الخلية الحالية :

var row = dataGridView1.CurrentCell.RowIndex;

نصائح أخرى

وهذا واحد يعمل بشكل جيد.

Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint

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

ومن تقريبا نفسها ولكن يمكنك أيضا استخدام هذا الحل:

var row = dataGridView1.CurrentRow.Index

وهناك طريقة أخرى إذا كنت بحاجة إلى تتبع تفاعل المستخدم مع داتاغريدفيف: في حالتي هناك معالجة إضافية في بعض الوظائف العامة التي تستخدم إحداثيات الصفوف والأعمدة Me.I_SelCol وMe.I_SelRow، لكنني لم تظهر أن لأنها ليست ذات صلة OP.

مع أطيب التحيات، روب

Private Sub I_DataGridView_CurrentCellChanged(sender As Object, e As EventArgs) Handles I_DataGridView.CurrentCellChanged

        If Me.I_DataGridView.CurrentCellAddress.X < 0 Or Me.I_DataGridView.CurrentCellAddress.Y < 0 Then Exit Sub

        ' The Windows Me.I_DataGridView object will have already deselected the current cell and selected the 
        ' new cell as per user navigation using mouse or cursor keys.  We just need to store the current
        ' co-ordinates for the currently selected cell.

        Me.I_SelCol = Me.I_DataGridView.CurrentCellAddress.X
        Me.I_SelRow = Me.I_DataGridView.CurrentCellAddress.Y

Exit Sub
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top