Bagikan melalui


GridViewRowCollection Kelas

Definisi

Mewakili kumpulan GridViewRow objek dalam GridView kontrol.

public ref class GridViewRowCollection : System::Collections::ICollection
public class GridViewRowCollection : System.Collections.ICollection
type GridViewRowCollection = class
    interface ICollection
    interface IEnumerable
Public Class GridViewRowCollection
Implements ICollection
Warisan
GridViewRowCollection
Penerapan

Contoh

Contoh berikut menunjukkan cara melakukan iterasi melalui Rows kumpulan GridView kontrol dan menampilkan nilai kolom di halaman.


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void AuthorsGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.Footer)
    {      
      
      // Get the number of items in the Rows collection.
      int count = AuthorsGridView.Rows.Count;

      // If the GridView control contains any records, display 
      // the last name of each author in the GridView control.
      if (count > 0)
      {      
        Message.Text = "The authors are:<br />";
        
        foreach (GridViewRow row in AuthorsGridView.Rows)
        {
          Message.Text += row.Cells[0].Text + "<br />";
        }
      }
      
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewRowCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewRowCollection Example</h3>

      <table>
        <tr>
          <td>
            <asp:gridview id="AuthorsGridView" 
              datasourceid="AuthorsSqlDataSource" 
              autogeneratecolumns="false"
              onrowcreated="AuthorsGridView_RowCreated"  
              runat="server"> 
                     
              <columns>
                <asp:boundfield datafield="au_lname"
                  headertext="Last Name"/>
                <asp:boundfield datafield="au_fname"
                  headertext="First Name"/>
              </columns>
                                    
            </asp:gridview>
          </td>
          <td>
            <asp:label id="Message" 
              forecolor="Red"
              runat="server"/>
          </td>
        </tr>
      </table>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname] FROM [authors] WHERE [state]='CA'"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
          
    </form>
  </body>
</html>

<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub AuthorsGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.Footer Then
      
      ' Get the number of items in the Rows collection.
      Dim count As Integer = AuthorsGridView.Rows.Count

      ' If the GridView control contains any records, display 
      ' the last name of each author in the GridView control.
      If count > 0 Then
           
        Message.Text = "The authors are:<br />"
        
        Dim row As GridViewRow
        For Each row In AuthorsGridView.Rows
        
          Message.Text &= row.Cells(0).Text & "<br />"
        
        Next
        
      End If
        
    End If
        
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewRowCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewRowCollection Example</h3>

      <table>
        <tr>
          <td>
            <asp:gridview id="AuthorsGridView" 
              datasourceid="AuthorsSqlDataSource" 
              autogeneratecolumns="false"
              onrowcreated="AuthorsGridView_RowCreated"  
              runat="server"> 
                     
              <columns>
                <asp:boundfield datafield="au_lname"
                  headertext="Last Name"/>
                <asp:boundfield datafield="au_fname"
                  headertext="First Name"/>
              </columns>
                                    
            </asp:gridview>
          </td>
          <td>
            <asp:label id="Message" 
              forecolor="Red"
              runat="server"/>
          </td>
        </tr>
      </table>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname] FROM [authors] WHERE [state]='CA'"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
          
    </form>
  </body>
</html>

Keterangan

Kelas GridViewRowCollection ini digunakan untuk menyimpan dan mengelola kumpulan GridViewRow objek dalam GridView kontrol. Setiap baris dalam kontrol diwakili GridView oleh GridViewRow objek. GridView Kontrol menyimpan semua baris datanya dalam Rows koleksi.

Kelas GridViewRowCollection ini mendukung beberapa cara untuk mengakses item dalam koleksi:

  • Gunakan pengindeks Item[] untuk langsung mengambil GridViewRow objek pada indeks berbasis nol tertentu.

  • GetEnumerator Gunakan metode untuk mengambil enumerator yang dapat digunakan untuk melakukan iterasi melalui koleksi.

  • CopyTo Gunakan metode untuk menyalin item dalam koleksi ke dalam System.Array objek, yang kemudian dapat digunakan untuk mengakses item dalam koleksi.

Untuk menentukan jumlah total item dalam koleksi, gunakan Count properti

Konstruktor

Nama Deskripsi
GridViewRowCollection(ArrayList)

Menginisialisasi instans GridViewRowCollection baru kelas menggunakan objek yang ditentukan ArrayList .

Properti

Nama Deskripsi
Count

Mendapatkan jumlah item dalam GridViewRowCollection objek.

IsReadOnly

Mendapatkan nilai yang menunjukkan apakah baris dalam GridViewRowCollection objek dapat dimodifikasi.

IsSynchronized

Mendapatkan nilai yang menunjukkan apakah objek disinkronkan GridViewRowCollection (aman utas).

Item[Int32]

Mendapatkan objek GridViewRow pada indeks yang ditentukan.

SyncRoot

Mendapatkan objek yang digunakan untuk menyinkronkan akses ke koleksi.

Metode

Nama Deskripsi
CopyTo(GridViewRow[], Int32)

Menyalin semua item dari ini GridViewRowCollection ke objek yang ditentukan Array , dimulai dari indeks yang ditentukan dalam Array objek.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetEnumerator()

Mengembalikan enumerator yang berisi semua GridViewRow objek dalam GridViewRowCollection.

GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Implementasi Antarmuka Eksplisit

Nama Deskripsi
ICollection.CopyTo(Array, Int32)

Untuk deskripsi anggota ini, lihat CopyTo(Array, Int32).

Metode Ekstensi

Nama Deskripsi
AsParallel(IEnumerable)

Mengaktifkan paralelisasi kueri.

AsQueryable(IEnumerable)

IEnumerable Mengonversi ke IQueryable.

Cast<TResult>(IEnumerable)

Melemparkan elemen ke jenis yang IEnumerable ditentukan.

OfType<TResult>(IEnumerable)

Memfilter elemen berdasarkan IEnumerable jenis tertentu.

Berlaku untuk

Lihat juga