Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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;
}
Posted
Comments
gggustafson 20-Sep-14 11:37am    
Do not even consider the organization of the database (it is usually a hash). If you want sorted data use the ORDER BY clause in your SQL query. That way, the data presented to the DataGridView is already sorted.
[no name] 21-Sep-14 8:02am    
your code is not clear , try to differ between dataset and datatable and datagridVeiw

1 solution

Try directly binding newView

C#
newView.sort = "Appointment Time asc";
dgvAppointments.DataSource = newView.ToTable();


This should work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900