Click here to Skip to main content
15,887,254 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to make a general class library with a method to populate any ListView with two columns. It does this by taking the name of a ListView and two string arrays(one for each column). The problem is when I want to use the method in another class, this method is not shown over their as a part of this class so that I'm not even able to access it. This is the code:

C#
public static class FillListView
{
    public static void Fill2colListView(ListView listView, string[] col1, string[] col2)
    {
        for (int i = 0; i < col1.Length; i++)
            {
                ListViewItem item = new ListViewItem(col1[i]);
                item.SubItems.Add(col2[i]);
                listView.Items.Add(item);
            }
    }
}


Can anyone help me why this method is not accessible in another class? It is urgent and I badly need help as I can't find this thing on the net.
Posted
Updated 3-Jun-14 4:06am
v2
Comments
Richard MacCutchan 3-Jun-14 9:59am    
Have you added it to your project? Also, it is probably quicker just to add the three lines of code to any other class you have, rather than importing this.
Anas Tasadduq 3-Jun-14 10:04am    
But I want a general class (like an API) so I can use it anywhere I want. In some of my classes, I need to use it many times so a method like this would be better. I also want to have other methods in the same class to populate ListViews with more than five columns!
Richard MacCutchan 3-Jun-14 11:14am    
You could create a much more elegant class than this, by inheriting from the ListView class and adding some extra methods for what you want. Doing it this way is largely pointless if you have to create a different class or method for each number of columns, and it's not really adhering to the principles of OOP.
[no name] 3-Jun-14 10:10am    
Did you rebuild your project after adding this? How are you trying to access it? And, no, it's not at all urgent.
Anas Tasadduq 3-Jun-14 10:15am    
In the class I want to use this, I added its reference, added the using statement for its namespace and then tried to access it by writing its class name in the class and putting a dot after it so that it showed me the methods of the class. But not even the Intellisense menu appeared and it said "identifier expected" when I hovered my mouse over it.

You are doing everything correctly. You don't really need to use Solution 1: it would work, but there is no a need to use extension methods; it's just syntactic sugar you may or may not want to have.

You just don't quite understand static methods, that's all. But your code is correct (not a great design, but it is). To call it from another class, simply call FillListView.Fill2colListView(someListView, someString, anotherString);

To generalize the method, use array of arrays of strings or some container supplying the set of strings to be filled in.

Another powerful approach it to use data binding. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.bindingcontext.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.databindings.aspx[^].

For better understanding of static methods, please see my past answers:
Type casting in c#[^],
C# windows base this key word related and its uses in the application[^],
What makes static methods accessible?[^].

—SA
 
Share this answer
 
Comments
Anas Tasadduq 3-Jun-14 13:44pm    
Thanks a lot, friend!
Sergey Alexandrovich Kryukov 3-Jun-14 14:04pm    
You are very welcome. Good luck, call again.
—SA
Write an extension method for it;

C#
public static class MyExtensions
{
    public static void FillListView(this ListView listView, params string[][] col)
    {
        ////You can skip clear phase if you sure you have columns in your listview
        listView.Items.Clear();
        listView.Columns.Clear();
        for (int i = 0; i < col.Length; i++)
        {
            listView.Columns.Add("column" + i);
        }
        ////

        for (int i = 0; i < col.Length; i++)
        {
            var row = col.Select(p => p[i]).ToArray();
            listView.Items.Add(new ListViewItem(row));
        }
    }
}


Then call it like this;

C#
string[] c1 = new string[]{"a","b","c"};
string[] c2 = new string[] { "d", "e", "f" };
string[] c3 = new string[] { "g", "h", "i" };
listView1.FillListView(c1,c2,c3);


You can add any number of column. Element count check of columns is not implemented, you should do it.
 
Share this answer
 
v2
Comments
Anas Tasadduq 3-Jun-14 10:45am    
I'm a beginner, so can you please explain how are the two for loops working? Thanks in advance. And if there is a simpler way to do it, please tell me that way.
Emre Ataseven 3-Jun-14 11:00am    
First loop is adding columns. Second loop is taking first elements of each column array and creates a new array with them. Then we are adding this array as listview item. Add this class to your project, then you can call it by listView1.FillListView. Assuming you have .Net 4.0 or higher.

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