10 Ocak 2017 Salı

DataGridViewRowCollection Sınıfı

Giriş
Bu sınıf ile DataTable sınıfına ait DataRowCollection çok benziyorlar.

Add metodu - Object []
Elimizde bir object[] olsun. Şöyle yaparız.
dgv.Rows.Add(objArray); //objArray is of type object[]
string[] ile şöyle yaparız.
var input = "11,22,33,44";
string[] array = input.Split(',');
dgv.Rows.Add(array);
Elimizde IEnumerable<object[]> varsa şöyle bir extension yazılabilir.
public static class DataGridViewExtensions
{
  public static void AddRange(this DataGridViewRowCollection collection, 
                              IEnumerable<object[]> rows)
  {
    foreach (object[] item in rows)
    {
      collection.Add(item);
    }
  }
}
AddRange metodu
Elimizde bir DataGridViewRow[] olsun. Şöyle yaparız.
dgv.Rows.AddRange(dgvRows); //allowed, dgvRows is DataGridViewRow[]
Cast metodu
Şöyle yaparız.
int count = dataGridView1.Rows
    .Cast<DataGridViewRow>()
    .Select(row => (string)row.Cells["TimeOut"].Value)
    .Where(v => !string.IsNullOrEmpty(v)) 
    .Count();
Clear metodu
Tüm satırları şöyle sileriz.
dgv.Rows.Clear();
Count Alanı
Tüm satırları şöyle dolaşabiliriz.
for (int i = 0; ji < dgv.Rows.Count; i++){...}
En son satıra şöyle erişiriz.
dgv.Rows[gdv.Rows.Count - 1].ToString()
RemoveAt metodu
Belli bir konumdaki satırı şöyle sileriz.
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

Hiç yorum yok:

Yorum Gönder