Sort option c# . net by column
Hi , Im new to datagridview basically , I have been successfull so far. Now im trying to add the option to sort by column ascending and descending , can someone assist me
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
kiran kumarRank #3,110 · 461 reputationPosted Feb 20, 2010, 5:27 PM EST
if you want sort the coloumn in descending order you can use this
this.dataGridView1.Sort(dataGridView1.Columns[int index],System.ComponentModel.ListSortDirection.Descending);
//index refers to your column no
if you want sort the coloumn in ascending order you can use this
this.dataGridView1.Sort(dataGridView1.Columns[int index],System.ComponentModel.ListSortDirection.Ascending);
//index refers to your coloumn no
mark the answer if it helps you
David SmithRank #999 · 2K reputationPosted Feb 20, 2010, 5:46 PM EST
David SmithRank #999 · 2K reputationPosted Feb 20, 2010, 6:00 PM EST
How do I fix that option
this.dataGridView1.Sort(dataGridView1.Columns[2],System.ComponentModel.ListSortDirection.Descending);
this.dataGridView1.Sort(dataGridView1.Columns[2],System.ComponentModel.ListSortDirection.Ascending);
this piece of code actually added the arrow to sort, but as soon as i hit the sort button it populated the error.
Karan Patel120 reputationPosted Feb 20, 2010, 6:23 PM EST
David SmithRank #999 · 2K reputationPosted Feb 20, 2010, 6:42 PM EST
Sam HobbsRank #107 · 20.5K reputationPosted Feb 20, 2010, 7:35 PM EST
dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.Automatic;
That's all you need to do, probably; if that does not work, then you probably need to remove other code that you have added to try to get it to work. Sometimes we expect something to be difficult and end up doing it a way that is more difficult than it needs to be.
Kirtan PatelRank #292 · 8.6K reputationPosted Feb 20, 2010, 7:41 PM EST
kiran kumarRank #3,110 · 461 reputationPosted Feb 20, 2010, 7:44 PM EST
David SmithRank #999 · 2K reputationPosted Feb 20, 2010, 11:00 PM EST
David SmithRank #999 · 2K reputationPosted Feb 21, 2010, 4:44 AM EST
Question someone, now I want to be able to have the option to hide and unhide columns and rows and also have the ability to add highlight and color multiple rows and columns
can someone point me in the right direction?
David SmithRank #999 · 2K reputationPosted Feb 21, 2010, 4:55 AM EST