DataGridViewRowCollection.AddRange(DataGridViewRow[]) Yöntem

Tanım

Belirtilen DataGridViewRow nesneleri koleksiyona ekler.

public:
 virtual void AddRange(... cli::array <System::Windows::Forms::DataGridViewRow ^> ^ dataGridViewRows);
public virtual void AddRange(params System.Windows.Forms.DataGridViewRow[] dataGridViewRows);
abstract member AddRange : System.Windows.Forms.DataGridViewRow[] -> unit
override this.AddRange : System.Windows.Forms.DataGridViewRow[] -> unit
Public Overridable Sub AddRange (ParamArray dataGridViewRows As DataGridViewRow())

Parametreler

dataGridViewRows
DataGridViewRow[]

öğesine eklenecek DataGridViewRownesne dizisiDataGridViewRowCollection.

Özel durumlar

dataGridViewRows, null'e eşittir.

dataGridViewRows yalnızca bir satır içerir ve içerdiği satırda denetimdeki sütunlardan daha fazla hücre vardır.

İlişkili DataGridView denetim, yeni satırların eklenmesini geçici olarak engelleyen aşağıdaki eylemlerden birini gerçekleştirmektedir:

  • Denetimdeki tüm hücreleri seçme.

  • Seçim temizleniyor.

-veya-

Bu yöntem, aşağıdaki DataGridView olaylardan biri için bir işleyiciden çağrılır:

-veya-

DataSource özelliğinin DataGridView özelliği değildirnull.

-veya-

Dizideki dataGridViewRows en az bir girdi olur null.

-veya-

sütun DataGridView içermiyor.

-veya-

Dizideki dataGridViewRows en az bir satır, olmayan DataGridViewbir null özellik değerine sahiptir.

-veya-

Dizideki en az bir satırın dataGridViewRows özellik değeri vardırSelected.true

-veya-

Dizideki dataGridViewRows iki veya daha fazla satır aynıdır.

-veya-

Dizideki dataGridViewRows en az bir satır, denetimdeki ilgili sütunun türüyle uyumlu olmayan türde bir veya daha fazla hücre içerir.

-veya-

Dizideki dataGridViewRows en az bir satır, denetimdeki sütunlardan daha fazla hücre içeriyor.

-veya-

Bu işlem, dondurulmamış satırlar sonrasında dondurulmuş satırlar ekler.

Örnekler

Aşağıdaki kod örneği, Açıklamalar bölümünde belirtilen hataya geçici bir çözüm olarak yeni kayıtların satırı seçildiğinde yönteminin nasıl kullanılacağını AddRange gösterir.

// Workaround for bug that prevents DataGridViewRowCollection.AddRange
// from working when the row for new records is selected. 
private void AddRows(params DataGridViewRow[] rows)
{
    InsertRows(dataGridView1.RowCount - 1, rows);
}

// Workaround for bug that prevents DataGridViewRowCollection.InsertRange
// from working when any rows before the insertion index are selected.
private void InsertRows(int index, params DataGridViewRow[] rows)
{
    System.Collections.Generic.List<int> selectedIndexes =
        new System.Collections.Generic.List<int>();
    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
    {
        if (row.Index >= index)
        {
            selectedIndexes.Add(row.Index);
            row.Selected = false;
        }
    }
    dataGridView1.Rows.InsertRange(index, rows);
    foreach (int selectedIndex in selectedIndexes)
    {
        dataGridView1.Rows[selectedIndex].Selected = true;
    }
}
' Workaround for bug that prevents DataGridViewRowCollection.AddRange
' from working when the row for new records is selected. 
Private Sub AddRows(ByVal ParamArray rows As DataGridViewRow())
    InsertRows(dataGridView1.RowCount - 1, rows)
End Sub

' Workaround for bug that prevents DataGridViewRowCollection.InsertRange
' from working when any rows before the insertion index are selected.
Private Sub InsertRows(ByVal index As Integer, _
    ByVal ParamArray rows As DataGridViewRow())

    Dim selectedIndexes As New System.Collections.Generic.List(Of Integer)

    For Each row As DataGridViewRow In dataGridView1.SelectedRows
        If row.Index >= index Then
            selectedIndexes.Add(row.Index)
            row.Selected = False
        End If
    Next row

    dataGridView1.Rows.InsertRange(index, rows)

    For Each selectedIndex As Integer In selectedIndexes
        dataGridView1.Rows(selectedIndex).Selected = True
    Next selectedIndex

End Sub

Açıklamalar

yöntemi, AddRange mümkünse öğesine paylaşılan satırlar DataGridViewRowCollectionekler. Aksi takdirde, yeni satırlar paylaşılmaz. Daha fazla bilgi için bkz. Windows Forms DataGridView Denetimini Ölçeklendirmeye yönelik En İyi Yöntemler.

Denetimdeki satırlar, yeni satırlar eklendiğinde otomatik olarak sıralanmaz. Yeni satırları doğru konumlarına sıralamak için bir olay işleyicisinde DataGridView.Sort yöntemini çağırınDataGridView.RowsAdded. Kullanıcı bir DataGridView.Sort hücreyi CellValueChanged değiştirdiğinde satırları sıralamak için bir olay işleyicisinde yöntemini çağırmak da isteyebilirsiniz.

Important

Bir hata nedeniyle, AddRange özellik AllowUserToAddRows ve yeni kayıtların satırı seçiliyse yöntemi uygulamanızın true yanıt vermeyi durdurmasını sağlar. Bu hatayı geçici olarak çözmek için, bu yöntemi çağırmadan önce satırın seçimini iptal etmeniz ve sonra satırı yeniden seçmelisiniz. Daha fazla bilgi için bu konudaki kod örneğine bakın.

Şunlara uygulanır

Ayrıca bkz.