Click here to Skip to main content
15,885,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm working on an MDI database application that requires consistency throughout each of the forms. As such, in my program I have a public DataTable which several controls over several forms are bound to. The idea is that the controls can be updated automatically whenever a change occurs within the DataTable, which keeps everything up-to-date.

However, for example, if I have a DataGridView bound to a DataTable and a ComboBox that is bound to a column in the same DataTable, when I sort the columns (i.e. by clicking on a column) the ordering of the items in the ComboBox also changes. I'm assuming that this is because reordering the DataGridView also reorders the DataTable.

Is there any way of allowing the user to sort the DataGridView WITHOUT the bound DataTable also being sorted as well?

using (SqlCommand sqlCommand = new SqlCommand(query, connection)
{
   sqlCommand.CommandType = CommandType.Text;
   SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
   dt = new DataTable();
   da.Fill(dt);
}

comboBox.BindingContext = new BindingContext();
comboBox.DataSource = dt;
comboBox.DisplayMember = "Column";

dataGridView.BindingContext = new BindingContext();
dataGridView.DataSource = dt;


Thanks in advance.
Posted

1 solution

Hi,

If you have used same DataTable for two control still you can manage two to have act independently. Check the following code here where I have used same DataTable object "dTSource" for two control,one DataGridView and another Combobox control. I sorted one column of Grid while keeping the combox value intact. I used a DataView object for the purpose.

DataTable dTSource = CreateDataTable();
//CreateDataTable returns a DataTable of 2 column "ID"  and "Desc"
DataView dView = new DataView(dTSource);
dataGridView1.DataSource = dTSource.DefaultView;
comboBox1.DataSource = dView;
comboBox1.DisplayMember = "ID";
comboBox1.ValueMember = "ID";



What I did was created a new DataView object while is having the initial un-sorted view of data and is bound to ComboBox control.
The default view of the datatable is bound to GridView object.

Hope that is helpfull to you,
 
Share this answer
 
v2
Comments
Kyrielia 16-Nov-10 6:19am    
Cheers for this! I've been stuck trying to find a solution for two days.
Arindam Tewary 16-Nov-10 6:27am    
ohh .. Glad that I could help you :)

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