I have this method that consumes an api
private void OrdenesTrabajoAPI()
{
try
{
string Desde = DateTime.Now.Date.ToString("o", CultureInfo.InvariantCulture);
string Hasta = DtHasta.Value.ToString("o", CultureInfo.InvariantCulture);
var client = new RestClient("https://fleet.cloudfleet.com/api/v1/work-orders/?createdAtFrom=" + Desde + "&createdAtTo=" + Hasta + "");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", ConfigurationManager.AppSettings["KEY"]);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddParameter("application/json; charset=utf-8", "", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.Content.Contains("No Work Orders found with the specified filters"))
{
lblError.Text = "No se encontraron órdenes de trabajo con las fechas especificados";
}
else
{
List<Vehiculos> ob = JsonConvert.DeserializeObject<List<Vehiculos>>(response.Content);
dataGridView2.DataSource = ob;
}
MessageBox.Show(response.Content);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
As you can see the data that it brings from the api puts them in the
dataGridView2.DataSource = ob;
and load them like this
I need to sort the number column from highest to lowest
with this code it supposedly orders them
dataGridView2.Sort(dataGridView2.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
But I get the following
'To be sortable, a DataGridView control must be bound to an IBindingList object.'
JSON
[
{
"number": 2657,
"vehicleCode": "C9M955",
"workshopDate": "2020-09-30T14:35:00.0000000Z",
"startDate": "2020-09-30T14:35:00.0000000Z",
"estimatedFinishDate": "2020-09-30T15:35:00.0000000Z",
"status": "opened",
"odometer": 210500,
"hourmeter": null,
"vendor": {
"id": 72791,
"name": "Almacen y taller Autocar inyeccion"
},
"reason": null,
"detectedIssue": null,
"paymentCondition": null,
"warranty": null,
"comments": null,
"driver": null,
"maintenanceLabels": null,
"type": null,
"city": null,
"costCenter": null,
"primaryGroup": null,
"secundaryGroup": null,
"createdAt": "2021-01-18T14:37:03.2170000Z",
"createdBy": {
"id": 19718,
"name": "Juan Perez"
},
"affectsMaintenanceSchedule": true,
"affectsVehicleAvailability": true,
"updatedAt": "2021-01-18T14:42:30.1500000Z",
"updatedBy": {
"id": 19718,
"name": "Juan Perez"
},
"totalCostLabors": 24500,
"totalCostParts": 0,
"totalCost": 24500,
"technicalCompletionDate": null,
"finalCompletionDate": null,
"lastSystemTechnicalCompletionDate": null,
"lastSystemFinalCompletionDate": null
}
]
Solution
private void OrdenesTrabajoAPI()
{
try
{
string Desde = DateTime.Now.Date.ToString("o", CultureInfo.InvariantCulture);
string Hasta = DtHasta.Value.ToString("o", CultureInfo.InvariantCulture);
var client = new RestClient("https://fleet.cloudfleet.com/api/v1/work-orders/?createdAtFrom=" + Desde + "&createdAtTo=" + Hasta + "");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", ConfigurationManager.AppSettings["KEY"]);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddParameter("application/json; charset=utf-8", "", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.Content.Contains("No Work Orders found with the specified filters"))
{
lblError.Text = "No se encontraron órdenes de trabajo con las fechas especificados";
}
else
{
List<Vehiculos> ob = JsonConvert.DeserializeObject<List<Vehiculos>>(response.Content);
dataGridView2.DataSource = ob;
dataGridView2.DataSource = (from o in ob
orderby o.number descending
select o).ToList();
}
MessageBox.Show(response.Content);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Variant 1:
Since you have the ob object which already exists you could do:
You save the list of vehicles in the datagridview , sorted by number descending. List ob , must be a global variable to work anywhere.
Variant2:
Specifically your error gives you because you don't have your datagridview bound to a bindingsource , to do so you must create a datasource object :
Then this other window appears, where you would choose the origin of the data, if it is from a database, or an object:
When you create the datasource , you can drag the field or fields you want to the form ( see image 1 ) or all the fields. In the example I give you, you would drag the Author field and this already creates a grid with all the fields you want. , it also generates the bindingsource object , which is the one that serves as a link between our data and the visual components, and each field that you drag would have its DataSource property linked to the created bindingsource .
So you could put:
and since the datagridview is bound to this bindingSource through its DataSource property , the changes will be reflected in it.
Notice that in variant1 you use the datagridview object and in this one the bindingsource object.