DataRow.Item[] Vlastnost

Definice

Získá nebo nastaví data uložená v zadaném sloupci.

Přetížení

Name Description
Item[DataColumn]

Získá nebo nastaví data uložená v zadaném DataColumn.

Item[Int32]

Získá nebo nastaví data uložená ve sloupci určeném indexem.

Item[String]

Získá nebo nastaví data uložená ve sloupci určeném názvem.

Item[DataColumn, DataRowVersion]

Získá zadanou verzi dat uloženou v zadaném DataColumn.

Item[Int32, DataRowVersion]

Získá data uložená ve sloupci určeném indexem a verzí dat, která se mají načíst.

Item[String, DataRowVersion]

Získá zadanou verzi dat uloženou v pojmenovaném sloupci.

Item[DataColumn]

Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs

Získá nebo nastaví data uložená v zadaném DataColumn.

public:
 property System::Object ^ default[System::Data::DataColumn ^] { System::Object ^ get(System::Data::DataColumn ^ column); void set(System::Data::DataColumn ^ column, System::Object ^ value); };
public object this[System.Data.DataColumn column] { get; set; }
member this.Item(System.Data.DataColumn) : obj with get, set
Default Public Property Item(column As DataColumn) As Object

Parametry

column
DataColumn

A DataColumn obsahující data.

Hodnota vlastnosti

Obsahuje Object data.

Výjimky

Sloupec nepatří do této tabulky.

Hodnota column null.

Došlo k pokusu o nastavení hodnoty na odstraněný řádek.

Datové typy hodnoty a sloupce se neshodují.

Příklady

Následující příklady ukazují použití Item[] vlastnosti k získání a nastavení hodnoty konkrétního indexu sloupce. První příklad získá hodnotu prvního sloupce v libovolném řádku, na který uživatel klikne v ovládacím DataGrid prvku. Druhá nastaví hodnotu předanou jako argument metodě.

Private Sub DataGrid1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs)
    
    Dim dataGridTable As DataTable = _
        CType(DataGrid1.DataSource, DataTable)
    ' Set the current row using the RowNumber 
    ' property of the CurrentCell.
    Dim currentRow As DataRow = _
        dataGridTable.Rows(DataGrid1.CurrentCell.RowNumber)
    Dim column As DataColumn = dataGridTable.Columns(1)

    ' Get the value of the column 1 in the DataTable.
    label1.Text = currentRow(column).ToString()
End Sub
 
Private Sub SetDataRowValue( _
    ByVal grid As DataGrid, ByVal newVal As Object)

    ' Set the value of a column in the last row of a DataGrid.
    Dim table As DataTable = CType(grid.DataSource, DataTable)
    Dim row As DataRow = table.Rows(table.Rows.Count - 1)
    Dim column As DataColumn = table.Columns("FirstName")
    row(column)= newVal
End Sub

Poznámky

Při nastavení vlastnosti se vygeneruje výjimka, pokud dojde k výjimce ColumnChanging v události.

Pokud se jedná o okamžitou úpravu, podívejte EndEdit se na výjimky, které je možné vygenerovat.

Platí pro

Item[Int32]

Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs

Získá nebo nastaví data uložená ve sloupci určeném indexem.

public:
 property System::Object ^ default[int] { System::Object ^ get(int columnIndex); void set(int columnIndex, System::Object ^ value); };
public object this[int columnIndex] { get; set; }
member this.Item(int) : obj with get, set
Default Public Property Item(columnIndex As Integer) As Object

Parametry

columnIndex
Int32

Index sloupce založený na nule.

Hodnota vlastnosti

Obsahuje Object data.

Výjimky

Nastane, když se pokusíte nastavit hodnotu na odstraněný řádek.

Argument columnIndex je mimo rozsah.

Nastane, když nastavíte hodnotu a nová hodnota Type neodpovídá DataType.

Příklady

Následující příklady ukazují použití Item[] vlastnosti k získání a nastavení hodnoty konkrétního indexu sloupce. První příklad získá hodnotu prvního sloupce v libovolném řádku, na který uživatel klikne v ovládacím DataGrid prvku.

private void DataGrid1_Click(object sender,
    System.EventArgs e)
{
    // Get the DataTable the grid is bound to.
    DataGrid thisGrid = (DataGrid) sender;
    DataTable table = (DataTable) thisGrid.DataSource;
    DataRow currentRow =
        table.Rows[thisGrid.CurrentCell.RowNumber];

    // Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow[1]);
    // You can also use the name of the column:
    // Console.WriteLine(currentRow["FirstName"])
}

private void SetDataRowValue(DataGrid grid, object newValue)
{
    // Set the value of the last column in the last row of a DataGrid.
    DataTable table;
    table = (DataTable) grid.DataSource;
    DataRow row;

    // Get last row
    row = (DataRow)table.Rows[table.Rows.Count-1];

    // Set value of last column
    row[table.Columns.Count-1] = newValue;
}
Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs)

    ' Get the DataTable the grid is bound to.
    Dim thisGrid As DataGrid = CType(sender, DataGrid)
    Dim table As DataTable = CType(thisGrid.DataSource, DataTable)
    Dim currentRow As DataRow = _
        table.Rows(thisGrid.CurrentCell.RowNumber)

    ' Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow(1))
    ' You can also use the name of the column:
    ' Console.WriteLine(currentRow("FirstName"))
    End Sub

    Private Sub SetDataRowValue( _
        ByVal grid As DataGrid, ByVal newValue As Object)

    ' Set the value of the last column in the last row of a DataGrid.
    Dim table As DataTable
    table = CType(grid.DataSource, DataTable)
    Dim row As DataRow 
    row = table.Rows(table.Rows.Count-1)
    row(table.Columns.Count-1) = newValue
End Sub

Poznámky

Při nastavení vlastnosti se vygeneruje výjimka, pokud dojde k výjimce ColumnChanging v události.

Pokud se jedná o úpravu, podívejte EndEdit se na výjimky, které je možné vygenerovat.

Platí pro

Item[String]

Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs

Získá nebo nastaví data uložená ve sloupci určeném názvem.

public:
 property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ columnName); void set(System::String ^ columnName, System::Object ^ value); };
public object this[string columnName] { get; set; }
member this.Item(string) : obj with get, set
Default Public Property Item(columnName As String) As Object

Parametry

columnName
String

Název sloupce.

Hodnota vlastnosti

Obsahuje Object data.

Výjimky

Sloupec zadaný pomocí columnName nelze najít.

Nastane, když se pokusíte nastavit hodnotu na odstraněný řádek.

Nastane, když nastavíte hodnotu a její Type neodpovídá DataType.

Nastane při pokusu vložit hodnotu null do sloupce, kde AllowDBNull je nastavena na false.

Příklady

Následující příklady ukazují použití Item[] vlastnosti k získání a nastavení hodnoty konkrétního indexu sloupce. První příklad získá hodnotu prvního sloupce v libovolném řádku, na který uživatel klikne v ovládacím DataGrid prvku. Druhá nastaví hodnotu předanou jako argument metodě.

private void DataGrid1_Click(
    object sender, System.EventArgs e)
{
    // Get the DataTable the grid is bound to.
    DataGrid thisGrid = (DataGrid) sender;
    DataTable table = (DataTable) thisGrid.DataSource;
    DataRow currentRow =
        table.Rows[thisGrid.CurrentCell.RowNumber];

    // Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow["FirstName"]);
    // You can also use the index:
    // Console.WriteLine(currentRow[1]);
}

private void SetDataRowValue(
    DataGrid grid, object newValue)
{
    // Set the value of the first column in
    // the last row of a DataGrid.
    DataTable table = (DataTable) grid.DataSource;
    DataRow row = table.Rows[table.Rows.Count-1];
    row["FirstName"] = newValue;
}
Private Sub DataGrid1_Click( _
    sender As Object, e As System.EventArgs)
     
    ' Get the DataTable the grid is bound to.
    Dim thisGrid As DataGrid = CType(sender, DataGrid)
    Dim table As DataTable = _
        CType(thisGrid.DataSource, DataTable)
    Dim currentRow As DataRow = _
        table.Rows(thisGrid.CurrentCell.RowNumber)

    ' Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow("FirstName"))
    ' You can also use the index:
    ' Console.WriteLine(currentRow(1).ToString())
End Sub
    
Private Sub SetDataRowValue( _
    grid As DataGrid, newValue As Object)
    ' Set the value of the first column in 
    ' the last row of a DataGrid.
    Dim table As DataTable = _
        CType(grid.DataSource, DataTable)
    Dim row As DataRow
    row = table.Rows((table.Rows.Count - 1))
    row("FirstName") = newValue
End Sub

Poznámky

Při nastavení vlastnosti se vygeneruje výjimka, pokud dojde k výjimce ColumnChanging v události.

Pokud se jedná o okamžitou úpravu, podívejte EndEdit se na výjimky, které je možné vygenerovat.

Platí pro

Item[DataColumn, DataRowVersion]

Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs

Získá zadanou verzi dat uloženou v zadaném DataColumn.

public:
 property System::Object ^ default[System::Data::DataColumn ^, System::Data::DataRowVersion] { System::Object ^ get(System::Data::DataColumn ^ column, System::Data::DataRowVersion version); };
public object this[System.Data.DataColumn column, System.Data.DataRowVersion version] { get; }
member this.Item(System.Data.DataColumn * System.Data.DataRowVersion) : obj
Default Public ReadOnly Property Item(column As DataColumn, version As DataRowVersion) As Object

Parametry

column
DataColumn

A DataColumn , který obsahuje informace o sloupci.

version
DataRowVersion

Jedna z DataRowVersion hodnot, které určují požadovanou verzi řádku. Možné hodnoty jsou Default, Original, Currenta Proposed.

Hodnota vlastnosti

Obsahuje Object data.

Výjimky

Sloupec nepatří do tabulky.

Argument column obsahuje hodnotu null.

Řádek nemá tuto verzi dat.

Příklady

Následující příklad získá aktuální hodnotu klikané buňky v ovládacím DataGrid prvku.

private void DataGrid1_Click(object sender,
    System.EventArgs e)
{
    DataTable dataGridTable =
        (DataTable)DataGrid1.DataSource;

    // Set the current row using the RowNumber
    // property of the CurrentCell.
    DataRow currentRow = dataGridTable.Rows[DataGrid1.CurrentCell.RowNumber];
    DataColumn column = dataGridTable.Columns[1];

    // Get the value of the column 1 in the DataTable.
    Console.WriteLine(currentRow[column, DataRowVersion.Current]);
}
Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim dataGridTable As DataTable = _
        CType(DataGrid1.DataSource, DataTable)

    ' Set the current row using the RowNumber 
    ' property of the CurrentCell.
    Dim currentRow As DataRow = dataGridTable.Rows( _
        DataGrid1.CurrentRowIndex)
    Dim column As DataColumn = dataGridTable.Columns(1)

    ' Get the value of the column 1 in the DataTable.
    label1.Text = currentRow(column, _
        DataRowVersion.Current).ToString()
End Sub

Poznámky

Vlastnost version by neměla být zaměňována RowState . Argument version popisuje stav dat obsažených ve sloupci vzhledem k původní hodnotě sloupce.

Při nastavení vlastnosti se vygeneruje výjimka, pokud dojde k výjimce ColumnChanging v události.

Pokud se jedná o okamžitou úpravu, podívejte EndEdit se na výjimky, které je možné vygenerovat.

Viz také

Platí pro

Item[Int32, DataRowVersion]

Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs

Získá data uložená ve sloupci určeném indexem a verzí dat, která se mají načíst.

public:
 property System::Object ^ default[int, System::Data::DataRowVersion] { System::Object ^ get(int columnIndex, System::Data::DataRowVersion version); };
public object this[int columnIndex, System.Data.DataRowVersion version] { get; }
member this.Item(int * System.Data.DataRowVersion) : obj
Default Public ReadOnly Property Item(columnIndex As Integer, version As DataRowVersion) As Object

Parametry

columnIndex
Int32

Index sloupce založený na nule.

version
DataRowVersion

Jedna z DataRowVersion hodnot, které určují požadovanou verzi řádku. Možné hodnoty jsou Default, Original, Currenta Proposed.

Hodnota vlastnosti

Obsahuje Object data.

Výjimky

Argument columnIndex je mimo rozsah.

Datové typy hodnoty a sloupce se neshodují.

Řádek nemá tuto verzi dat.

Došlo k pokusu o nastavení hodnoty na odstraněný řádek.

Příklady

Následující příklad získá aktuální hodnotu sloupce prostřednictvím Item[] vlastnosti objektu DataRow .

Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    ' Set the current row using the RowNumber property of the CurrentCell.
    Dim currentRow As DataRow = CType(DataGrid1.DataSource, DataTable). _
       Rows(DataGrid1.CurrentCell.RowNumber)

    ' Get the value of the column 1 in the DataTable.
    label1.Text = currentRow(1, DataRowVersion.Current).ToString()
End Sub

Poznámky

Po volání BeginEdit metody můžete vytvořit nebo aktualizovat řádek. Podobně EndEdit musí být volána metoda pro potvrzení úpravy. Po volání EndEdit metody a před voláním AcceptChanges metody se uloží interní reprezentace původních a nových navrhovaných hodnot. Proto, dokud nezavoláte AcceptChanges, můžete použít version argument určit, kterou verzi sloupce potřebujete, buď nebo DataRowVersion.OriginalDataRowVersion.Proposed. Jakmile však zavoláte metodu AcceptChanges , verze sloupce se vrátí na DataRowVersion.Original. Pokud je řádek nový, můžete parametr předat DataRowVersion.Default také k načtení výchozí hodnoty sloupce. Při předávání DataRowVersion.Currentvrátí vlastnost aktuální hodnotu, bez ohledu na její verzi.

Note

Metoda BeginEdit se volá implicitně při změně hodnoty ovládacího prvku vázaného na data nebo při DataRow přidání objektu do DataRowCollection; EndEdit metoda je volána implicitně při volání následujících metod: AcceptChanges metoda DataRow objektu, AcceptChanges metoda objektu DataTable nebo CancelEdit metoda.

Naproti tomu DataRowVersion výčet Current vrátí verzi dat po EndEdit zavolání metody.

Argument version by neměl být zaměňován s RowState vlastností. Argument version popisuje stav dat obsažených ve sloupci vzhledem k původní hodnotě sloupce. Vlastnost RowState popisuje stav celého řádku vzhledem k nadřazené DataTable.

Při nastavení vlastnosti se vygeneruje výjimka, pokud dojde k výjimce ColumnChanging v události.

Pokud se jedná o okamžitou úpravu, podívejte EndEdit se na výjimky, které je možné vygenerovat.

Platí pro

Item[String, DataRowVersion]

Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs
Zdroj:
DataRow.cs

Získá zadanou verzi dat uloženou v pojmenovaném sloupci.

public:
 property System::Object ^ default[System::String ^, System::Data::DataRowVersion] { System::Object ^ get(System::String ^ columnName, System::Data::DataRowVersion version); };
public object this[string columnName, System.Data.DataRowVersion version] { get; }
member this.Item(string * System.Data.DataRowVersion) : obj
Default Public ReadOnly Property Item(columnName As String, version As DataRowVersion) As Object

Parametry

columnName
String

Název sloupce.

version
DataRowVersion

Jedna z DataRowVersion hodnot, které určují požadovanou verzi řádku. Možné hodnoty jsou Default, Original, Currenta Proposed.

Hodnota vlastnosti

Obsahuje Object data.

Výjimky

Sloupec zadaný pomocí columnName nelze najít.

Datové typy hodnoty a sloupce se neshodují.

Řádek nemá tuto verzi dat.

Řádek byl odstraněn.

Příklady

Následující příklad načte aktuální verzi dat kliknutím na buňku DataGrid ovládacího prvku.

private void DataGrid1_Click(object sender, System.EventArgs e)
{
    // Set the current row using the RowNumber
    // property of the CurrentCell.
    DataRow currentRow =
        ((DataTable)(DataGrid1.DataSource)).
        Rows[DataGrid1.CurrentCell.RowNumber];

    // Print the current value of the column named "FirstName."
    Console.WriteLine(currentRow["FirstName",
        DataRowVersion.Current]);
}
Private Sub DataGrid1_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    ' Set the current row using the RowNumber property 
    ' of the CurrentCell.
    Dim currentRow As DataRow = _
        CType(DataGrid1.DataSource, DataTable). _
        Rows(DataGrid1.CurrentCell.RowNumber)

    ' Print the current value of the column named "FirstName."
    Console.WriteLine(currentRow("FirstName", _
        DataRowVersion.Current).ToString())
End Sub

Poznámky

Verze by neměla být zaměňována s RowState vlastností. Argument version popisuje stav dat obsažených ve sloupci vzhledem k původní hodnotě sloupce. Vlastnost RowState popisuje stav celého řádku vzhledem k nadřazené DataTable.

Při nastavení vlastnosti se vygeneruje výjimka, pokud dojde k výjimce ColumnChanging v události.

Pokud se jedná o okamžitou úpravu, podívejte EndEdit se na výjimky, které je možné vygenerovat.

Viz také

Platí pro