Skip to content

Commit 1b24df5

Browse files
Copilotcsharpfritz
andcommitted
Update GridView row selection sample to use SelectMethod pattern
Co-authored-by: csharpfritz <78577+csharpfritz@users.noreply.github.com>
1 parent 29df25b commit 1b24df5

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

samples/AfterBlazorServerSide/Components/Pages/ControlSamples/GridView/RowSelection.razor

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
ItemType="Customer"
1313
AutoGenerateColumns="false"
1414
DataKeyNames="CustomerID"
15+
SelectMethod="GetCustomers"
1516
OnRowCommand="HandleRowCommand"
1617
EmptyDataText="No data available">
1718
<Columns>
@@ -99,17 +100,6 @@
99100
private Customer SelectedCustomer;
100101
private List<Customer> _customers;
101102

102-
protected override void OnAfterRender(bool firstRender)
103-
{
104-
if (firstRender)
105-
{
106-
_customers = GetCustomers();
107-
CustomersGrid.DataSource = _customers;
108-
CustomersGrid.DataBind();
109-
}
110-
base.OnAfterRender(firstRender);
111-
}
112-
113103
private void HandleRowCommand(GridViewCommandEventArgs args)
114104
{
115105
if (args.CommandName == "Select")
@@ -118,7 +108,7 @@
118108
int rowIndex = Convert.ToInt32(args.CommandArgument);
119109

120110
// Get the data item for the selected row using the cached data source
121-
if (rowIndex < _customers.Count)
111+
if (_customers != null && rowIndex < _customers.Count)
122112
{
123113
SelectedCustomer = _customers[rowIndex];
124114

@@ -130,9 +120,9 @@
130120
}
131121
}
132122

133-
private List<Customer> GetCustomers()
123+
public IQueryable<Customer> GetCustomers(int maxRows, int startRowIndex, string sortByExpression, out int totalRowCount)
134124
{
135-
return new List<Customer>
125+
var customers = new List<Customer>
136126
{
137127
new Customer
138128
{
@@ -163,5 +153,11 @@
163153
CompanyName = "Innovation Labs"
164154
}
165155
};
156+
157+
// Cache for use in HandleRowCommand
158+
_customers = customers;
159+
160+
totalRowCount = customers.Count;
161+
return customers.AsQueryable();
166162
}
167163
}

0 commit comments

Comments
 (0)