sfgrid programmatically set/read cell value
"UpdateCell" does not work instead "SetCellValue" what am I wrong?
Among the methods I have implemented which is the most correct to use?
<button class="btn btn-primary" @onclick="UpdateCell">ByUpdateCell</button>
<button class="btn btn-primary" @onclick="SetCellValue">BySetCellValue</button>
<button class="btn btn-primary" @onclick="DataSourceSetValue">ByDataSource</button>
<button class="btn btn-primary" @onclick="CurrentViewDataSetValue">ByCurrentViewData</button>
<button class="btn btn-primary" @onclick="GetValueCell">GetValueCell</button>
<p>Current count: @cellValue</p>
<SfGrid @ref="@myGrid" DataSource="@Orders">
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120" IsPrimaryKey="true"></GridColumn>
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn>
<GridColumn Field=@nameof(Order.OrderDate) HeaderText="Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code{
public List<Order> Orders { get; set; }
public SfGrid<Order> myGrid { get; set; }
public string cellValue { get; set; }
private void UpdateCell()
{
myGrid.UpdateCell(1, "CustomerID", "UpdateCell!");
}
private void SetCellValue()
{
myGrid.SetCellValue(1001, "CustomerID", "SetCellValue!");
}
private void DataSourceSetValue()
{
Orders.Where(x => x.OrderID == 1001).First().CustomerID = "DataSourceSetValue!";
myGrid.Refresh();
}
private void CurrentViewDataSetValue()
{
((Order)myGrid.CurrentViewData.Where(x => ((Order)x).OrderID == 1001).First()).CustomerID = "DataSourceSetValue!"; ;
myGrid.Refresh();
}
private void GetValueCell()
{
cellValue = ((Order)myGrid.CurrentViewData.Where(x => ((Order)x).OrderID == 1001).First()).CustomerID ;
}
protected override void OnInitialized()
{
Orders = Enumerable.Range(1, 5).Select(x => new Order()
{
OrderID = 1000 + x,
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
Freight = 2.1 * x,
OrderDate = DateTime.Now.AddDays(-x),
}).ToList();
}
public class Order
{
public int? OrderID { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
public double? Freight { get; set; }
}
}
Attachment: Index_2a5706ea.7z
|
<button class="btn btn-primary" @onclick="BatchSaveMethod">Batch Save</button>
|
Thanks for the example, now everything is clear to me.Best Regards.
I am not using editmode = batch. We don't provide EditMode property.
How can I set value of that cell. I have already foun RowIndex.
Hi Amish,
Based on your requirements, we suggest using the Grid public method SetCellValueAsync
to achieve your goal. Please refer to the code snippet below for your
reference.
|
@using Syncfusion.Blazor.Grids
<button id="SetCellValue" @onclick="DataHandler">SetCellValue</button> <SfGrid DataSource="@Orders" @ref="grid" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })"> <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings>
<GridColumns> <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn> <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn> <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> </GridColumns> </SfGrid>
@code {
public List<Order> Orders { get; set; }
protected override void OnInitialized() { Orders = GetAllRecords(); } SfGrid<Order> grid; private async Task DataHandler() { await grid.SetCellValueAsync(1005, "CustomerID", "UpdatedCell"); }
|
Regards,
Prathap S