I am making a chat messenger. In the messenger I am able to get all the people online in the server. Aclly there is a client side and a server side. And I have to link that chat messenger to a database where all his friend list is stored. So only the people who are both online and are in his friend list will be shown. I managed to get all online in the server in a listview and all present in the friendlist in another list view. Now to achieve my aim , I just have to select the usernames that is common to both the listviews and put them in another list views.
If any one has any alternate, less processor consuming idea of implementing this please inform.
Loading
Sam HobbsPosted Jan 29, 2010, 7:39 PM
First, I must say that I am not sure of a couple of things about your data. I get the impression that the amount of data is small, so performance is not as much of a problem as it would be for many other things. Regarless, it is useful to know how to process a large amount of data efficiently. I also am not sure where the data is coming from; is most of the data being loaded from from a database or from some other file on disk, or is the data being built more dynamically. The difference can influence how the data is loaded; is most of the data read in all at once, or does most of the data get created in pieces.
I will assume that the data it is being built in pieces, since users will be signining in and out and such. I think the easiest way to do it that would be efficient for the sytem too is to use a DataTable. It is entirely possible to create a DataTable in memory that never exists on disk. Microsoft has implemented many tools for databases, such as searching, and you can use them even in DataTables that exist only in memory. For this, you will want to create the schema for each of the tables and the IP address would be a primary key.
So what you can do is that when a row is added to one of the DataTables, you can search the other DataTables to determine if the item exists in it (the other DataTable); if it exists in the other DataTable, then you need to also add it to the combined list. When a row is deleted in either of the first two DataTables, search the combined DataTable and if the item to be dleted is in the combined DataTable, then you need to delete the item from the combined DataTable too.
Use of DataTables in this manner might sound innefficient, but actually Microsoft has developed the classes to be as efficient as possible and would be quite efficient when the amount of data is large.
Kirtan PatelPosted Jan 27, 2010, 1:30 AM
//Code for Intersecting Two List View
private void button1_Click(object sender, EventArgs e)
{
foreach (ListViewItem li in listView1.Items)
{
foreach (ListViewItem l2 in listView2.Items)
{
if (li.SubItems[1].Text == l2.SubItems[1].Text)
{
string[] SubItemsLv = new string[2];
SubItemsLv[0]= li.SubItems[0].Text;
SubItemsLv[1] = li.SubItems[1].Text;
ListViewItem lvi = new ListViewItem(SubItemsLv);
listView3.Items.Add(lvi);
}
}
}
}
if my answer helps you than check "Do you like this answer" check box please :)
Anubhav MishraPosted Jan 26, 2010, 9:29 PM
Sam HobbsPosted Jan 26, 2010, 9:24 PM
Add a DataGridView to the form. In the form constructor after InitializeComponent() put:
Build and test that. That shows that you can put data into a SortedList and then easily show the data in a DataGridView. You can search for items in the SortedList using the key.
The next thing to do is to learn how to use LINQ to get a union of the two lists.
Sam HobbsPosted Jan 26, 2010, 4:36 PM
Note that a DataGridView would be more flexible and make things easier.