Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

// I am beginner on C#, i wish to get the data from DB, the put it into the datagrid, i am using wpf (C#), the program basically is able to run but i cannot return the result dynamically, instead hard coded i use here, so can give me some of the idea how to return the result dynamically, pls refer the comment inside the data file

//Main file

using System;
using System.Data;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

using Microsoft.Windows.Controls;

 
namespace testDG
{
    public partial class Window1
    {
        //local variables
       

        public Window1()
        {
            this.InitializeComponent();

            dg1.ItemsSource = data.GetCountryData();

        }

    }


}

//data file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MySql.Data;
using MySql.Data.MySqlClient;

namespace testDG
{
    public class data
    {
        //define property
        public string Code {get; set;}
        public string Country { get; set; }
       

        public data(string cd, string ct)
        {
            this.Code = cd;
            this.Country = ct;
        }

        public static List<data> GetCountryData()
        {

// when i code "new data[intVar]", it prompt me to use const var, instead of local var, but i will count the DB only can know got how many records, so const is impossible right? then "new data[4]{ using loop here  }", it's prompt me invalid to use loop at here, so any other ways i can return the value dynamically? Pls help..thx
           

            return new List<data>(new data[4]
            {
                new data("1", "Malaysia"),
                new data("2", "HK"),
                new data("3", "China"),
                new data("4", "Singapore")               
               
            });

           
        }

    }
}

Posted

You instantiate List with 4 storage slots, then you attempt to fill the 2nd through 5th slots?
 
Share this answer
 
Actually the 4 storage slot that u seen is just a dummy data, to test whether the program is able to run or not. What i plans to do is make the size dynamic and data display also dynamic...So, does it possible?
 
Share this answer
 
Seems you are having problems with lists and loops. First of all, you are using a collection initializer, which is only meant to initialize collections with a constant number of elements. To create a list of variable size, use this syntax:
C#
// Create an empty list.
List<data> myData = new List<data>();
Then just fill the list with data and it will dynamically resize according to your space requirements:
C#
// Fill the list with data.
for(int i = 0; i < GetMax(); i++)
{
	myData.Add(GetData(i));
}
The GetMax and GetData methods can be defined however you like. And they don't need to be methods... I just made them methods to make it clear that anything could go there. Here is an example of how they could be declared:
C#
// Get the size of the list.
public int GetMax() { return 4; }

// Get the data for each index in the list.
public data GetData(int index)
{
	switch(i)
	{
		case 0: return new data("1", "Malaysia");
		case 1: return new data("2", "HK");
		case 2: return new data("3", "China");
		case 3: return new data("4", "Singapore");
		default: throw new Exception("Invalid");
	}
}
 
Share this answer
 
v4
Oh, really thanks for your help, i am successfully solve it..thx thx
 
Share this answer
 

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