Reputation: 581
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.
Upvotes: 5
Views: 1775
Reputation: 581
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!
Upvotes: 3
Reputation: 930
The other thing could be that if the datagrid is bound by a stored procedure within that you could add an ORDER BY clause that would sort the data however you want in the first place...
Upvotes: 0
Reputation:
You can use RowsAdded event of datagridview. Inside that method you can write code to sort the datagridview.
private void dataGridView1_RowsAdded(object sender,
DataGridViewRowsAddedEventArgs e)
{
datagridview1.Sort();
}
Upvotes: 3
Reputation: 9322
If your DataGridView is bound, you could use:
dataGridView1.Columns[0].ValueType = typeof(System.Int32);
dataGridView1.Columns[0].CellTemplate.ValueType = typeof(System.Int32);
If your DataGridView is not bound, you could use SortCompare handler in sorting values, like:
public Form1()
{
InitializeComponent();
dataGridView1.SortCompare += new DataGridViewSortCompareEventHandler(dataGridView1_SortCompare);
}
Then do this:
void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
if (e.Column.Index == 0)
{
if ( int.Parse(e.CellValue1.ToString()) > int.Parse(e.CellValue2.ToString()))
{
e.SortResult = 1;
}
else if (int.Parse(e.CellValue1.ToString()) < int.Parse(e.CellValue2.ToString()))
{
e.SortResult = -1;
}
else
{
e.SortResult = 0;
}
e.Handled = true;
}
}
Upvotes: 0