Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

I have two ListView controls in different projects.

1)lstclnt in client project
2)lstsv in server project

I want to update copy values in lstclnt from lstsv on the click event of Update Button

I have used these 4 codes, they aren't giving any error but doesn't works

C#
foreach (ListViewItem item in MainForm.lstsv.Items)
   {
      lstclnt.Items.Add(item.Clone() as ListViewItem);
   }


C#
lstclnt.Items.AddRange((from ListViewItem item in MainForm.lstsv.Items
            select (ListViewItem)item.Clone()).ToArray());


ListViewItem[] LVIARRY_CopiedListViewItems = new
                              ListViewItem[MainForm.lstsv.Items.Count];
   for (int INT_Count=0; INT_Count < LVIARRY_CopiedListViewItems.Length; INT_Count++)
   {
            LVIARRY_CopiedListViewItems[INT_Count] =                        (ListViewItem)MainForm.lstsv.Items[INT_Count].Clone();
   }
lstclnt.Items.AddRange(LVIARRY_CopiedListViewItems);


ListViewItem[] dest = new ListViewItem[MainForm.lstsv.Items.Count];
   MainForm.lstsv.Items.CopyTo(dest, 0);
   lstclnt.Items.AddRange(dest);


Please Somebody help me out
*MainForm is the name of Form in which lstsv is located
Posted
Updated 15-Jun-22 4:21am
v3
Comments
Albin Abel 12-Feb-11 13:27pm    
In my example when you update second time have a check for existing items in the client or remove all items then update all.
Ali Al Omairi(Abu AlHassan) 12-Feb-11 13:28pm    
Sir, you said
1)lstclnt in client project
2)lstsv in server project
How did you transfer the data? (via TCP?)

100 :rose: ;)
cYpH3r x3r0 12-Feb-11 23:00pm    
yes via TCP
server is a TCPlistener, and client is a TCP client, actually i am building LAN and WAN messenger
Ali Al Omairi(Abu AlHassan) 13-Feb-11 16:52pm    
ok, sir. this was stupid from me. but really i wanted to help.

100 :rose: ;)
Albin Abel 13-Feb-11 6:30am    
I have added a remoting example. In that I am passing a array list. Instead you can use an serializable object as well. That object you may construct as per your list view complexity.

Hi
Assuming your server project has reference to the client project and these two projects are in the same solution.

Let us say both these forms having a form and listview named listview1.

Then in the server form on button click it opens the client form and then cliek another button, it copy the listview items from server to client list view as shown below.

Server form code

C#
public partial class Sever : Form
 {
     private Client.Client clientFrm;
     public Sever()
     {
         InitializeComponent();
     }
     //opens client form
     private void button1_Click(object sender, EventArgs e)
     {
         clientFrm = new Client.Client();
         clientFrm.Show();
     }
     //calls the client method and pass server's listview
     private void button2_Click(object sender, EventArgs e)
     {
         clientFrm.UpdateListView(this.listView1);
     }


On the client form have a public method

MIDL
public void UpdateListView(ListView listView)
{
    foreach (ListViewItem item in listView.Items)
    {
        this.listView1.Items.Add((ListViewItem)item.Clone());
    }
}


This is called from the server and pass the listview.

Is this way work for you?
 
Share this answer
 
Comments
cYpH3r x3r0 12-Feb-11 14:05pm    
@AlbinAbel1
Thanks for your reply, but I am using Multiple start-up(both server and client projects start simultaneously), and update button is on the client side, so all i want is that when i click on update button it brings all items from server listview and insert them in client list view. for this purpose i added the reference of server in client and made the listview of server as "Public Static"(so that it can be accessed on client)
now all i need is that i want to copy this stuff....
any help will be highly appreciated :-)
cYpH3r x3r0 12-Feb-11 23:58pm    
@AlbinAlbel1
This is the loop which i am running at server side
foreach (var client in _clients.Values)
{
ClientForm.listCopier(lstsv);
}

And on the client side i am using the below function

public static void listCopier(ListView listView)
{
foreach (ListViewItem item in listView.Items)
{
lstclnt.Items.Add((ListViewItem)item.Clone());
}
}

but its giving me 2 errors

1) Error 1 An object reference is required for the non-static field, method, or property 'Client.ClientForm.listCopier(System.Windows.Forms.ListView)'

2)Error 2 An object reference is required for the non-static field, method, or property 'Client.ClientForm.lstclnt'

i know this is simple but i am a beginner in c#, so its pretty difficult for me :)
MCY 13-Feb-11 5:09am    
1. on the server side, you utilize the method ListCopier, but the method is defined in client side. Is this "listcopier" also available in server? or do you somehow supply the connection between the call to the method in server and the body of the method in client?
cYpH3r x3r0 13-Feb-11 6:34am    
server is using the function of client, as i have added a reference of client in the server and made the function(listCopier) as public static
Albin Abel 13-Feb-11 6:20am    
The above example as I said assumes both project in a single machine. You need to have a refernce of the client project to the server project (Add reference the client exe). Also you need create an object for the client form.

ClientForm.listCopier(lstsv); this may be wrong ClientForm clientForm=new ClientForm(); next call
clientForm.listCopier(lstsv);

Also....make sure the lstclnt is present, this will not come if you add refernce of the client to the server... Let me know if you have further problems..

I am posting an example for remoting you may use if the project running in two different solutions or in different machines..
If the two listviews are in different solutions, then you cannot simply copy from one and insert to other. I suggest getting all listviewitems to a List<ListviewItem> in one solution project, serialize it, and then:
a. If client and server runs on the same machine, save the serialized List to physical drive in one app, and open in other (monitor the folder for changes and if new file is saved or an existing one is updated, deserialize the file and insert them to target listview)
b. If client and server runs on different machines, send the serialized byte array to target by socketing
 
Share this answer
 
v3
Comments
Albin Abel 12-Feb-11 14:12pm    
In that case can use remoting or WCF functionalities also. That would be helpful to transfer data at runtime. But you may not transfer the listview as such....but in a form which is compatible
cYpH3r x3r0 12-Feb-11 23:59pm    
its giving me 2 errors
given above in the discussion
MCY 13-Feb-11 5:13am    
In your solution, you call one of the client methods in server. I kindly recommend to part b of my answer; serialize the listviewitems on the server, send them to clients via TCP, then when the serialized packets arrive to the client and deserialize them, cast them to listview items. finally add them to target listview one by one. Of course, two listviews in server and client should be compatible. If not, you should do some extra work for compatibility.
cYpH3r x3r0 13-Feb-11 12:54pm    
oh nice, this would be fine
MAGIC BLOCK 2022 15-Jun-22 20:39pm    
This is how did i make it with Albin Able's solution:

// copy this code to form1

private void button12_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(listView1);
frm2.Show();
frm2.UpdateListView(this.listView1);
}



// copy below code to form2



public partial class Form2 : Form
{
public Form2(ListView listView)
{
InitializeComponent();
}



public void UpdateListView(ListView listView)
{
foreach (ListViewItem item in listView.Items)
{
this.listView1.Items.Add((ListViewItem)item.Clone());
}

}
}
For using remoting with c#
In this example create a class library and add the following code. My project name is RemotableObject. Note your project name because you need to refer this in your client and server projects
C#
[Serializable]
public  class Remotable : MarshalByRefObject
{
    public static Delegate setListView;

    public  void Test(ArrayList list)
    {
        object[] args = new object[1];
        args[0] = list;
        setListView.DynamicInvoke(args);

    }

}

Compile this class library in to the dll file.
Now create a server project, add a listview (say listview1) in the form (simple listview for this example, no sublist).
Add refernce to the dll file created.The namespce for my example is using RemotableObject;

You need to add also the System.Runtime.Remoting (Add reference-> in the .Net tab select this and click ok)

Add a button and in the button's click event...
C#
private void button1_Click(object sender, EventArgs e)
{
    try
    {
        ChannelServices.RegisterChannel(new TcpChannel(), false);
    }
    catch { }

    Type requiredType = typeof(Remotable);

    //Relapce the local host to network ip incase the client runs at a network machine, or other remote server name
    // where the configured port is open and ACESSIBLE, not restricted by firewalls and other stuff
    Remotable remoteObject = (Remotable)Activator.GetObject(requiredType, "tcp://localhost:9998/ListService");

    ArrayList list = new ArrayList();
    foreach (ListViewItem item in listView1.Items)
    {
        list.Add(item.Text);
    }
    remoteObject.Test(list);
    remoteObject = null;
}

Note: use these additional libraries for server and as well as for client
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotableObject;

In the client form add a list view anf have the following code

You need to add also the System.Runtime.Remoting (Add reference-> in the .Net tab select this and click ok)

Add the reference to the dll to this project as well and have the above references..
C#
public partial class Form1 : Form
{
    private delegate void setList(ArrayList list);
    private ArrayList list;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        TcpChannel tcpChannel = new TcpChannel(9998);
        ChannelServices.RegisterChannel(tcpChannel, false);
        Type commonInterfaceType = typeof(Remotable);

        RemotingConfiguration.RegisterWellKnownServiceType(commonInterfaceType,
        "ListService", WellKnownObjectMode.SingleCall);
        Remotable.setListView = new setList(setListView);

    }

    public void setListView(ArrayList list)
    {
        this.list = list;
        if (listView1.InvokeRequired)
        {
            listView1.Invoke(new MethodInvoker(invoker));
        }
    }

    public void invoker()
    {
        foreach (String listItemText in this.list)
        {
            ListViewItem item = new ListViewItem(listItemText);
            listView1.Items.Add(item);
        }
        listView1.Refresh();

    }
}
;
START the client first, because client listens and server access it execute its method to update the listview.
Then run the server....
click the button (we added earlier) which has the remoting code at its event. Now you can see the listview in the client get updated
 
Share this answer
 
v2
Comments
MCY 13-Feb-11 11:17am    
good answer. 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900