Click here to Skip to main content
15,894,740 members

how to get data into dictionary collection in c#.net?

Member 8090436 asked:

Open original thread
Hi,
I have a dictionary collection where I stored name and value.
For instance i have 500 records stored in that collection just like below-
C#
Dictionary<string,int> dict=new Dictionary<string,int>();
dict.add("A",1);
dict.add("B",2);
--
dict.add("AAB",500);

I have a method name GetData(int PageNumber,Dictionary<string,int>() myDictionary)
it takes 2 aurguments.
First i will pass page number,and second i have collection.

I want to achieve, if i pass page number 1 then dictionary collection maintain only 1-100 key value pair.
If i passed 2 then dictionary collection maintain only 101-200 key value pair.
And keep going such fashion. means i want to create it dynamic approach
Any approach is greatly appreciated!


Below are my code which i have implemented, but it is static i want it dynamic-
C#
public Dictionary<string, int> CreateCollection()
        {
            dictionary.Add("A", 1);
            dictionary.Add("B", 2);
            dictionary.Add("C", 3);
            dictionary.Add("D", 4);
            dictionary.Add("E", 5);
            dictionary.Add("F", 6);
            dictionary.Add("G", 7);
            dictionary.Add("H", 8);
            dictionary.Add("I", 9);
            dictionary.Add("J", 10);

            return dictionary;
        }

        public void getdata(int pageNo,object myDictionary)
        {
            Dictionary<string, int> KeyValuePair = ((Dictionary<string, int>)myDictionary);
            if (pageNo == 1)// pageNo is the Datagrid Pager index in this case....
            {
                foreach (KeyValuePair<string, int> kvp in KeyValuePair.Take(5))
                {
                    Response.Write("Key " + kvp.Key);
                    Response.Write("Value "+kvp.Value);
                }
            }
        }

        protected void GetData_Click(object sender, EventArgs e)
        {
            getdata(1, this.CreateCollection());
        }
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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