Hi All,
I'm having serious trouble with this so I'm hoping somebody can assist me.
I have a dataset which is filled with appointment data from an SQL database, I have a datagridview which is databound to this dataset which displays the appointment data.
My aim is to create a dataview which shows the appointments sorted by time, and then iterate through the dataset inserting rows where appointment times are not used, these inserted rows will show only a time in the Appointment Time column.
I have succeeded in inserting a new row in the dataset with an appointment time at a particular index using a button to test (index will eventually be determined in a loop where an appointment time is not located), the datagridview updates with the new row; However, when I sort the data after adding the new rows, they all appear at the bottom of the datagridview.
I understand that the sorted dataview is not representative of the actual dataset order, so I'm trying to create a new datatable based on the dataview but I just can't get it working.
Also if I try to insert a new row after sorting it doesn't work either.
Can anybody help?
Here's the code I have
private void UpdateAppointmentsDataGrid()
{
sdaAppointments = new SqlDataAdapter();
sdaAppointments.SelectCommand = cmd;
dbAppointmentsDataSet = new DataTable();
sdaAppointments.Fill(dbAppointmentsDataSet);
AppointmentsbSource = new BindingSource();
AppointmentsbSource.DataSource = dbAppointmentsDataSet;
dgvAppointments.DataSource = AppointmentsbSource;
sdaAppointments.Update(dbAppointmentsDataSet);
}
private void btnAddNewRow_Click(object sender, EventArgs e)
{
DataRow row = dbAppointmentsDataSet.NewRow();
dbAppointmentsDataSet.Rows.InsertAt(row, 3);
row[0] = DateTime.Now;
}
private void btnSort_Click(object sender, EventArgs e)
{
DataView newView = new DataView(dbAppointmentsDataSet);
newView.Sort = "Appointment Time";
DataTable newTable = newView.ToTable();
dgvAppointments.DataSource = newTable.DefaultView;
}