Edit

Share via


LinqDataSourceStatusEventArgs.TotalRowCount Property

Definition

Gets the total number of rows in a data set from a data-retrieval operation.

public:
 property int TotalRowCount { int get(); };
public int TotalRowCount { get; }
member this.TotalRowCount : int
Public ReadOnly Property TotalRowCount As Integer

Property Value

The total number of rows in a data set from the data retrieval operation; -1 if the LinqDataSourceStatusEventArgs object was created during a data modification operation; -1 if you enabled customized paging by setting AutoPage to true and by setting RetrieveTotalRowCount to false.

Examples

The following example shows a Web page with a LinqDataSource control, a GridView control, and a Literal control. The LinqDataSource control defines an event handler for the Selected event.

<asp:Literal ID="Literal1" runat="server"></asp:Literal> Total Records
<br />
<asp:LinqDataSource 
  AutoPage="true"
  ID="LinqDataSource1" 
  runat="server" 
  ContextTypeName="ExampleDataContext" 
  TableName="Customers" 
  onselected="LinqDataSource1_Selected">
</asp:LinqDataSource>
<asp:GridView 
  ID="GridView1" 
  runat="server" 
  AllowPaging="true" 
  AutoGenerateColumns="True" 
  DataKeyNames="CustomerID" 
  DataSourceID="LinqDataSource1">
</asp:GridView>
<asp:Literal ID="Literal1" runat="server"></asp:Literal> Total Records
<br />
<asp:LinqDataSource 
  AutoPage="true"
  ID="LinqDataSource1" 
  runat="server" 
  ContextTypeName="ExampleDataContext" 
  TableName="Customers">
</asp:LinqDataSource>
<asp:GridView 
  ID="GridView1" 
  runat="server" 
  AllowPaging="true" 
  AutoGenerateColumns="True" 
  DataKeyNames="CustomerID" 
  DataSourceID="LinqDataSource1">
</asp:GridView>

The following example shows the code for the event handler for the Selected event. The value of the TotalRowCount property is assigned to the Literal control.

protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
    Literal1.Text = e.TotalRowCount.ToString();
}
Protected Sub LinqDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceStatusEventArgs) Handles LinqDataSource1.Selected
    Literal1.Text = e.TotalRowCount.ToString()
End Sub

Remarks

You use the TotalRowCount property to get the number of records in the data set during a data retrieval operation. Typically, you retrieve this property when you are displaying pages of data and want to show the total number of records.

When data is being paged, the value in the TotalRowCount property might be different from the actual number of rows that are returned by the query. This is because the query returns only the number of rows that are needed for that page of data.

The TotalRowCount property contains the number of records from a query only when it is accessed in an event handler for the Selected event. When the property is accessed from an event handler for the ContextCreated, Deleted, Inserted, or Updated event, the TotalRowCount property contains -1.

The value of the TotalRowCount property depends on the AutoPage property of the LinqDataSource control, and on the AllowPaging property of the data-bound control. The following table summarizes the possible values for the TotalRowCount property.

AutoPage Property of LinqDataSource control AllowPaging Property of data-bound control TotalRowCount property Outcome
true true Number of records for a query. Data is automatically paged.
false false Number of records for a query. Data is not paged.
true false -1 Data is not paged.
false true The value that you assigned to the TotalRowCount property in the event handler for the Selecting event. Data is paged according to the values that you specified when you customized paging.

Applies to