Skip to content

Commit f1bba34

Browse files
vignesh.natarajan@syncfusion.comvignesh.natarajan@syncfusion.com
authored andcommitted
updated comments
1 parent a96487c commit f1bba34

File tree

4 files changed

+193
-18
lines changed
  • Binding MS SQL database using CustomAdaptor/Blazor Web app/Grid_MSSQL/Grid_MSSQL/Grid_MSSQL
  • Binding MS SQL database using UrlAdaptor

4 files changed

+193
-18
lines changed

Binding MS SQL database using CustomAdaptor/Blazor Web app/Grid_MSSQL/Grid_MSSQL/Grid_MSSQL/Components/Pages/Home.razor

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,42 @@
5151
</SfGrid>
5252

5353
@code {
54+
/// <summary>
55+
/// Implementing custom adaptor by extending the <see cref=“DataAdaptor”/> class.
56+
/// The DataGrid component support for custom data binding, which enables the binding and manipulation of data in a personalized way, using user-defined methods.
57+
/// </summary>
5458
public class CustomAdaptor : DataAdaptor
5559
{
5660
public OrderData OrderService = new OrderData();
57-
// Performs data Read operation
61+
/// <summary>
62+
/// Returns the data collection after performing data operations based on request from <see cref=”DataManagerRequest”/>
63+
/// </summary>
64+
/// <param name="dataManagerRequest">DataManagerRequest containes the information regarding paging, grouping, filtering, searching which is handled on the DataGrid component side</param>
65+
/// <param name="additionalParam">An optional parameter that can be used to perform additional data operations.</param>
66+
/// <returns>The data collection's type is determined by how this method has been implemented.</returns>
5867
public override async Task<object> ReadAsync(DataManagerRequest DataManagerRequest, string key = null)
5968
{
6069
IEnumerable<Order> DataSource = await OrderService.GetOrdersAsync();
70+
// Handling Searching in Custom Adaptor.
6171
if (DataManagerRequest.Search != null && DataManagerRequest.Search.Count > 0)
6272
{
6373
// Searching
6474
DataSource = DataOperations.PerformSearching(DataSource, DataManagerRequest.Search);
6575
}
66-
if (DataManagerRequest.Sorted != null && DataManagerRequest.Sorted.Count > 0)
67-
{
68-
// Sorting
69-
DataSource = DataOperations.PerformSorting(DataSource, DataManagerRequest.Sorted);
70-
}
76+
// Handling Filtering in Custom Adaptor.
7177
if (DataManagerRequest.Where != null && DataManagerRequest.Where.Count > 0)
7278
{
7379
// Filtering
7480
DataSource = DataOperations.PerformFiltering(DataSource, DataManagerRequest.Where, DataManagerRequest.Where[0].Operator);
7581
}
82+
// Handling Sorting in Custom Adaptor.
83+
if (DataManagerRequest.Sorted != null && DataManagerRequest.Sorted.Count > 0)
84+
{
85+
// Sorting
86+
DataSource = DataOperations.PerformSorting(DataSource, DataManagerRequest.Sorted);
87+
}
7688
int count = DataSource.Cast<Order>().Count();
89+
// Handling Paging in Custom Adaptor. For example, Skip is 0 and Take is equal to page size for first page.
7790
if (DataManagerRequest.Skip != 0)
7891
{
7992
//Paging
@@ -83,13 +96,15 @@
8396
{
8497
DataSource = DataOperations.PerformTake(DataSource, DataManagerRequest.Take);
8598
}
99+
// Handling Aggregates and Group in Custom Adaptor.
86100
DataResult DataObject = new DataResult();
87-
if (DataManagerRequest.Aggregates != null || DataManagerRequest.Group != null) // Aggregation
101+
if (DataManagerRequest.Aggregates != null) // Aggregation
88102
{
89103
if (DataManagerRequest.Group != null)
90104
{
105+
// For Aggregate alone
91106
IEnumerable ResultData = DataSource.ToList();
92-
// Grouping
107+
//For Grouping
93108
foreach (var group in DataManagerRequest.Group)
94109
{
95110
ResultData = DataUtil.Group<Order>(ResultData, group, DataManagerRequest.Aggregates, 0, DataManagerRequest.GroupByFormatter);
@@ -102,9 +117,11 @@
102117
}
103118
DataObject.Count = count;
104119
DataObject.Aggregates = DataUtil.PerformAggregation(DataSource, DataManagerRequest.Aggregates);
105-
120+
//Here RequiresCount is passed from the control side itself, where ever the ondemand data fetching is needed then the RequiresCount is set as true in component side itself.
121+
// In the above case we are using Paging so datas are loaded in ondemand bases whenever the next page is clicked in DataGrid side.
106122
return DataManagerRequest.RequiresCounts ? DataObject : (object)DataSource;
107123
}
124+
// Handling Grouping in Custom Adaptor.
108125
if (DataManagerRequest.Group != null)
109126
{
110127
IEnumerable ResultData = DataSource.ToList();
@@ -115,22 +132,54 @@
115132
}
116133
DataObject.Result = ResultData;
117134
DataObject.Count = count;
135+
//Here RequiresCount is passed from the control side itself, where ever the ondemand data fetching is needed then the RequiresCount is set as true in component side itself.
136+
// In the above case we are using Paging so datas are loaded in ondemand bases whenever the next page is clicked in DataGrid side.
118137
return DataManagerRequest.RequiresCounts ? DataObject : (object)ResultData;
119138
}
139+
//Here RequiresCount is passed from the control side itself, where ever the ondemand data fetching is needed then the RequiresCount is set as true in component side itself.
140+
// In the above case we are using Paging so datas are loaded in ondemand bases whenever the next page is clicked in DataGrid side.
120141
return DataManagerRequest.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource;
121142
}
143+
144+
/// <summary>
145+
/// Inserts a new data item into the data collection.
146+
/// </summary>
147+
/// <param name="dataManager">The DataManager is a data management component used for performing data operations in applications.</param>
148+
/// <param name="record">The new record which is need to be inserted.</param>
149+
/// <param name="additionalParam">An optional parameter that can be used to perform additional data operations.</param>
150+
/// <returns>Returns the newly inserted record details.</returns>
122151
public override async Task<object> InsertAsync(DataManager DataManager, object Value, string Key)
123152
{
153+
// Given that the Value property consists of newly inserted record details. It can be used to insert the records into database by calling the predefined logic.
124154
await OrderService.AddOrderAsync(Value as Order);
125155
return Value;
126156
}
157+
/// <summary>
158+
/// Updates an existing data item in the data collection.
159+
/// </summary>
160+
/// <param name="dataManager">The DataManager is a data management component used for performing data operations in applications.</param>
161+
/// <param name="record">The modified record which is need to be updated.</param>
162+
/// <param name="primaryColumnName">The primaryColumnName specifies the field name of the primary column.</param>
163+
/// <param name="additionalParam">An optional parameter that can be used to perform additional data operations.</param>
164+
/// <returns>Returns the updated data item.</returns>
127165
public override async Task<object> UpdateAsync(DataManager DataManager, object Value, string keyField, string key)
128166
{
167+
// Given that the Value property consists of modified record details. It can be used to update the changes into database by calling the predefined logic.
129168
await OrderService.UpdateOrderAsync(Value as Order);
130169
return Value;
131170
}
171+
172+
/// <summary>
173+
/// Removes a data item from the data collection.
174+
/// </summary>
175+
/// <param name="dataManager">The DataManager is a data management component used for performing data operations in applications.</param>
176+
/// <param name="primaryColumnValue">The primaryColumnValue specifies the primary column value which is needs to be removed from the grid record.</param>
177+
/// <param name="primaryColumnName">The primaryColumnName specifies the field name of the primary column.</param>
178+
/// <param name="additionalParam">An optional parameter that can be used to perform additional data operations.</param>
179+
/// <returns>Returns the removed data item.</returns>
132180
public override async Task<object> RemoveAsync(DataManager DataManager, object Value, string keyField, string key)
133181
{
182+
// Given that the key column is identified nullable interger type in the DataGrid, the primaryColumnValue can be utilized from Value property directly.
134183
await OrderService.RemoveOrderAsync(Value as int?);
135184
return Value;
136185
}

Binding MS SQL database using CustomAdaptor/Blazor Web app/Grid_MSSQL/Grid_MSSQL/Grid_MSSQL/Data/OrderData.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ namespace Grid_MSSQL.Data
55
{
66
public class OrderData
77
{
8+
//Enter the connectionstring of database
89
public string ConnectionString = <Enter your connectionstring here>;
910
public async Task<List<Order>> GetOrdersAsync()
1011
{
12+
//Create query to fetch data from database
1113
string Query = "SELECT * FROM dbo.Orders ORDER BY OrderID;";
1214
List<Order> Orders = null;
15+
//Create SQL Connection
1316
using (SqlConnection Connection = new SqlConnection(ConnectionString))
1417
{
15-
SqlDataAdapter Ddapter = new SqlDataAdapter(Query, Connection);
18+
//Using SqlDataAdapter and Query create connection with database
19+
SqlDataAdapter Adapter = new SqlDataAdapter(Query, Connection);
1620
DataSet Data = new DataSet();
1721
Connection.Open();
18-
// Using SqlDataAdapter, we process the query string and fill the data into the dataset
19-
Ddapter.Fill(Data);
22+
// Using SqlDataAdapter, process the query string and fill the data into the dataset
23+
Adapter.Fill(Data);
24+
//Cast the data fetched from Adaptor to List<T>
2025
Orders = Data.Tables[0].AsEnumerable().Select(r => new Order
2126
{
2227
OrderID = r.Field<int>("OrderID"),
@@ -31,30 +36,39 @@ public async Task<List<Order>> GetOrdersAsync()
3136
}
3237
public async Task AddOrderAsync(Order Value)
3338
{
39+
//Create query to insert the specific into the database by accessing its properties
3440
string Query = $"Insert into Orders(CustomerID,Freight,ShipCity,EmployeeID) values('{(Value as Order).CustomerID}','{(Value as Order).Freight}','{(Value as Order).ShipCity}','{(Value as Order).EmployeeID}')";
3541
SqlConnection Connection = new SqlConnection(ConnectionString);
3642
Connection.Open();
43+
//Execute the SQL Command
3744
SqlCommand SqlCommand = new SqlCommand(Query, Connection);
45+
//Exceute this code to reflect the changes into the database
3846
SqlCommand.ExecuteNonQuery();
3947
Connection.Close();
4048
}
4149

4250
public async Task UpdateOrderAsync(Order Value)
4351
{
52+
//Create query to update the changes into the database by accessing its properties
4453
string Query = $"Update Orders set CustomerID='{(Value as Order).CustomerID}', Freight='{(Value as Order).Freight}',EmployeeID='{(Value as Order).EmployeeID}',ShipCity='{(Value as Order).ShipCity}' where OrderID='{(Value as Order).OrderID}'";
4554
SqlConnection Connection = new SqlConnection(ConnectionString);
4655
Connection.Open();
56+
//Execute the SQL Command
4757
SqlCommand SqlCommand = new SqlCommand(Query, Connection);
58+
//Exceute this code to reflect the changes into the database
4859
SqlCommand.ExecuteNonQuery();
4960
Connection.Close();
5061
}
5162

5263
public async Task RemoveOrderAsync(int? Key)
5364
{
65+
//Create query to remove the specific from database by passing the primary key column value.
5466
string Query = $"Delete from Orders where OrderID={Key}";
5567
SqlConnection Connection = new SqlConnection(ConnectionString);
5668
Connection.Open();
69+
//Execute the SQL Command
5770
SqlCommand SqlCommand = new SqlCommand(Query, Connection);
71+
//Exceute this code to reflect the changes into the database
5872
SqlCommand.ExecuteNonQuery();
5973
Connection.Close();
6074
}

0 commit comments

Comments
 (0)