DataGridView auto re-sort
c#, datagridview, sorting
Solution
Finally got it work! There is what I did:
DataGridViewColumn column = dataGridView1.SortedColumn;
ListSortDirection order;
if (dataGridView1.SortOrder.Equals(SortOrder.Ascending))
{
order = ListSortDirection.Ascending;
}
else
{
order = ListSortDirection.Descending;
}
dataGridView1.Sort(column, order);
So, I got the sorted column and I check in a if what is the order that the datagridview is sorted, then I sort again the column that used sorted for the same order... Notice that this code is fresh so it isn't "bulletproof" I'm not checking if the values of column and the order are null or not! If you need this code and use it, take that in mind! It's almost like the answer of Jayesh. Thank you all for the help!
Problem
I'm making a program in C# that receive `sms` and display to the user, and I'm having a problem with the sort of the data. For example, if the user sort the table by ids, from the higher id to the lower so when a new `sms` arrives it gets on the top of the table, the new sms will get to the bottom of the table anyway. Here is a screenshot As you can see, the id 125 is under the 0 instead of being on the top of the table... Is there any code or event that I should use? There is where I want to start that event: ``` public void readSms() { try { comm = AppData.getInstance().getComm(); DecodedShortMessage[] messages = comm.ReadMessages(PhoneMessageStatus.All, "SM"); foreach (DecodedShortMessage message in messages) { if (AppData.getInstance().mensagens.Count != 0) { Message msg = new Message( AppData.getInstance().messages.Last.Value.getId() + 1, ((SmsDeliverPdu)(message.Data)).OriginatingAddress, message.Data.UserDataText, ((SmsDeliverPdu)(message.Data)).SCTimestamp.ToDateTime(), false); AppData.getInstance().setMensagem(msg); } else { Message msg = new Message( 0, ((SmsDeliverPdu(message.Data)).OriginatingAddress, message.Data.UserDataText, ((SmsDeliverPdu)(message.Data)).SCTimestamp.ToDateTime(), false); AppData.getInstance().setMensagem(msg); } } // I need to put the event of sorting here in case any message was been added } catch (Exception ex) { MessageBox.Show(ex.Message); } } ``` Thank you once more Edper for your help! There is when I add the message in the program: ``` public void preencherTabela(int lastIndex) { LinkedList<Message> messages = AppData.getInstance().getMessagessList(); { addTable( messages.ElementAt(i).getId(), messages.ElementAt(i).getChecked(), messages.ElementAt(i).getMsg(), messages.ElementAt(i).getNum(), messages.ElementAt(i).getDate()); } } ``` And the `AddTable` method: ``` private void addTable(int p, bool p_2, string p_3, string p_4, DateTime dateTime) { this.dataGridView1.Invoke( new MethodInvoker(() => { this.dataGridView1.Rows.Add(p, p_2, p_3, p_4, dateTime); })); } ``` If you want to now also the point of it, this is a program for show Messages in a screen, for example, in a disco, the users send messages from the mobile to a number connected by a model to a PC and the messages will show on it, actually I'm testing it in a friends disco. Thank you again for the help! Edit: Added a bounty of 50 reputation for who can help me fix the table.