martedì 19 febbraio 2013

Binding DatagridView with HashTable

If you have your data into an HashTable, and you wanna bind a DataGrdiView, look at the following code as example.

private void BindYourDataGridView()
        {
            // here put data into the HashTable, but you can already have data
            // in you hashtable, in this case delete this lines -----------
            Hashtable YourData= new Hashtable();

            for (int i = 0; i < something; i++)
                YouData.Add(i, something[i]);
            //---------------------------------------------------------------------
            
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("data");

            dt.Columns.Add("A", typeof(string));
            dt.Columns.Add("B"typeof(int));

            IDictionaryEnumerator enumerator = YourData.GetEnumerator();

            DataRow row = null;
            
            //adding row in the datatable
            while (enumerator.MoveNext())
            {
                int index = (int)enumerator.Key;
                string value = (string)enumerator.Value;
                row = dt.NewRow();
                row["A"] = index;
                row["B"] = value;
                dt.Rows.Add(row);
            }

      Your_DataGridView.DataSource = ds.Tables[0];
    }


My Two Cents ...

Nessun commento:

Posta un commento