Prev: WPF-style code
Next: Uploading file to UNIX host
From: Anthony on 17 Apr 2010 03:24 > var bs = new BindingSource {DataSource = q}; > > dataGridView1.AutoGenerateColumns = true; > dataGridView1.AutoSize = true; > > dataGridView1.DataSource = bs; > } That's another trick (with a few more lines of code) :-)))) Do you know why i now can't sort the DataGrid by clicking the column header? By default, users can sort the data in a DataGridView control by clicking the header of a column. Thank you very much ! Anthony
From: Mr. Arnold on 17 Apr 2010 09:26 Anthony wrote: >> var bs = new BindingSource {DataSource = q}; >> >> dataGridView1.AutoGenerateColumns = true; >> dataGridView1.AutoSize = true; >> >> dataGridView1.DataSource = bs; >> } > > That's another trick (with a few more lines of code) :-)))) > > Do you know why i now can't sort the DataGrid by clicking the column header? > By default, users can sort the data in a DataGridView control by clicking > the header of a column. > > Thank you very much ! I am more of a Web.UI.Control programmer rather that Windows.UI.Control programmer. So, I don't know why the sort by column header doesn't work for you. You may have to do it manually if someone can't help you to make it work automatically. http://msdn.microsoft.com/en-us/library/0868ft3z.aspx You can also consider this event. ColumnHeaderMouseClick This event is used to capture when a header is clicked. Sometimes you may need to modify the direction that the sort glyph is pointing in the column squares. You can check e.ColumnIndex on the event parameter to find out which of the column headers was clicked. http://dotnetperls.com/datagridview-tips You might use this event, and based on e.ColumnIndex, query and sort 'q' on corresponding property in the list of objects in 'q' and rebind. If(e.ColumnIndex == 0) { var q1 = (from a in q.OrderBy(b => b.Name) select a).tolist(); // bind q1 } If you want descending then q.OrderByDescending(b => b.Name). HTH
From: Anthony on 19 Apr 2010 01:49
> I am more of a Web.UI.Control programmer rather that Windows.UI.Control > programmer. So, I don't know why the sort by column header doesn't work > for you. > > You may have to do it manually if someone can't help you to make it work > automatically. > > http://msdn.microsoft.com/en-us/library/0868ft3z.aspx > > You can also consider this event. > > ColumnHeaderMouseClick > This event is used to capture when a header is clicked. > Sometimes you may need to modify the direction that the > sort glyph is pointing in the column squares. > You can check e.ColumnIndex on the event parameter to > find out which of the column headers was clicked. > > http://dotnetperls.com/datagridview-tips > > You might use this event, and based on e.ColumnIndex, query and sort 'q' > on corresponding property in the list of objects in 'q' and rebind. > > If(e.ColumnIndex == 0) > { > var q1 = (from a in q.OrderBy(b => b.Name) select a).tolist(); > // bind q1 > } > > If you want descending then q.OrderByDescending(b => b.Name). > > HTH > Thank you for the explanation. But it is weird that the default sort by clicking the header of a column disappeared. I've checked all settings again, but no result. Anthony |