Back to Devexpress

CustomColumnDataEventArgs.ListSourceRowIndex Property

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-customcolumndataeventargs-f6cb815b.md

latest9.8 KB
Original Source

CustomColumnDataEventArgs.ListSourceRowIndex Property

Gets the current row’s index in the data source.

Namespace : DevExpress.XtraGrid.Views.Base

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
public int ListSourceRowIndex { get; }
vb
Public ReadOnly Property ListSourceRowIndex As Integer

Property Value

TypeDescription
Int32

An integer value that identifies the row by its index in the data source.

|

Remarks

Use the ListSourceRowIndex property to refer to the current row using the methods provided by your data source.

To get values in the row identified by the ListSourceRowIndex property, use the ColumnView.GetListSourceRowCellValue method.

Example 1

In the following example, it is assumed that the Grid Control is bound to a table that contains the “Quantity”, “UnitPrice”, and “Discount” columns. The code below adds an unbound column that calculates the total order amount according to the following expression: Quantity * UnitPrice * (1-Discount).

csharp
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;

private void Form1_Load(object sender, EventArgs e) {
    // ...
    gridControl1.ForceInitialize();

    // Create an unbound column.
    GridColumn unboundColumn = gridView1.Columns.AddField("Total");
    unboundColumn.VisibleIndex = gridView1.Columns.Count;
    unboundColumn.UnboundDataType = typeof(decimal);
    // Disable column edit operations.
    unboundColumn.OptionsColumn.AllowEdit = false;
    // Specify format settings.
    unboundColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
    unboundColumn.DisplayFormat.FormatString = "c";
    // Customize appearance settings.
    unboundColumn.AppearanceCell.BackColor = Color.FromArgb(179, 226, 221);
}

// Return the total amount for a specific row.
decimal getTotalValue(GridView view, int listSourceRowIndex) {
    decimal unitPrice = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "UnitPrice"));
    decimal quantity = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Quantity"));
    decimal discount = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Discount"));
    return unitPrice * quantity * (1 - discount);
}

// Specify data for the Total column.
private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) {
   GridView view = sender as GridView;
   if (e.Column.FieldName == "Total" && e.IsGetData) e.Value = 
     getTotalValue(view, e.ListSourceRowIndex);
}
vb
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Columns

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' ...
    GridControl1.ForceInitialize()

    ' Create an unbound column.
    Dim unboundColumn As GridColumn = GridView1.Columns.AddField("Total")
    unboundColumn.VisibleIndex = GridView1.Columns.Count
    unboundColumn.UnboundDataType = GetType(Decimal)
    ' Disable column edit operations.
    unboundColumn.OptionsColumn.AllowEdit = False
    ' Specify format settings.
    unboundColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
    unboundColumn.DisplayFormat.FormatString = "c"
    ' Customize appearance settings.
    unboundColumn.AppearanceCell.BackColor = Color.FromArgb(179, 226, 221)

End Sub

' Return the total amount for a specific row.
Private Function getTotalValue(view As GridView, listSourceRowIndex As Integer) As Decimal
    Dim unitPrice As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "UnitPrice"))
    Dim quantity As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Quantity"))
    Dim discount As Decimal = Convert.ToDecimal(view.GetListSourceRowCellValue(listSourceRowIndex, "Discount"))
    Return unitPrice * quantity * (1 - discount)
End Function

' Specify data for the Total column.
Private Sub GridView1_CustomUnboundColumnData(ByVal sender As Object,
ByVal e As CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
    Dim view As GridView = TryCast(sender, GridView)
    If e.Column.FieldName = "Total" AndAlso e.IsGetData Then
        e.Value = getTotalValue(view, e.ListSourceRowIndex)
    End If
End Sub

The following code snippets (auto-collected from DevExpress Examples) contain references to the ListSourceRowIndex property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-grid-async-load-data-in-unbound-column/CS/WindowsApplication3/Main.cs#L40

csharp
{
    if (unboundValues.ContainsKey(e.ListSourceRowIndex)) return unboundValues[e.ListSourceRowIndex];
    unboundValues.Add(e.ListSourceRowIndex, "Not Loaded");

winforms-grid-multiple-row-selection-web-style-checkboxes/CS/E1271/CheckMarkSelection.cs#L201

csharp
if(e.IsGetData)
    e.Value = IsRowSelected(View.GetRowHandle(e.ListSourceRowIndex));
else

winforms-grid-add-radio-group-column/CS/Helper/GridRadioGroupColumnHelper.cs#L77

csharp
if (e.IsGetData)
    e.Value = e.ListSourceRowIndex == SelectedDataSourceRowIndex;
if (e.IsSetData)

winforms-grid-popupcontaineredit-with-dynamic-content/CS/Q148684/Form1.cs#L66

csharp
if (e.IsGetData)
    e.Value = unboundData[e.ListSourceRowIndex];
else if (e.IsSetData)

winforms-grid-calculate-custom-summary-based-on-other-summary-values/CS/Q273845/Form1.cs#L63

csharp
if(e.IsGetData)
    e.Value = Convert.ToDecimal(view.GetListSourceRowCellValue(e.ListSourceRowIndex, colUnitPrice)) *
        Convert.ToInt32(view.GetListSourceRowCellValue(e.ListSourceRowIndex, colUnitsInStock));

winforms-grid-async-load-data-in-unbound-column/VB/WindowsApplication3/Main.vb#L40

vb
Private Function GetSummaryValue(ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) As Object
    If unboundValues.ContainsKey(e.ListSourceRowIndex) Then
        Return unboundValues(e.ListSourceRowIndex)

winforms-grid-multiple-row-selection-web-style-checkboxes/VB/E1271/CheckMarkSelection.vb#L234

vb
If e.IsGetData Then
    e.Value = IsRowSelected(View.GetRowHandle(e.ListSourceRowIndex))
Else

winforms-grid-add-radio-group-column/VB/Helper/GridRadioGroupColumnHelper.vb#L76

vb
If e.IsGetData Then
    e.Value = e.ListSourceRowIndex = SelectedDataSourceRowIndex
End If

winforms-grid-popupcontaineredit-with-dynamic-content/VB/Q148684/Form1.vb#L69

vb
If e.IsGetData Then
    e.Value = unboundData(e.ListSourceRowIndex)
ElseIf e.IsSetData Then

winforms-grid-show-error-icons/VB/Form1.vb#L75

vb
If e.IsGetData Then
    e.Value = e.ListSourceRowIndex
End If

See Also

Rows

CustomColumnDataEventArgs Class

CustomColumnDataEventArgs Members

DevExpress.XtraGrid.Views.Base Namespace