使用数据表列表填充objectlistview

时间:2016-10-26 09:33:49

标签: c# objectlistview

我目前正在使用objectlistview,并且无法使用list填充数据库中的数据,这里是我的代码

    static internal List<Birthday> GetBirthdays()
    {
        if (Birthday.Alldata.Count == 0)
            Birthday.Alldata = Birthday.InitializeData();
        return Birthday.Alldata;
    }
    static private List<Birthday> Alldata = new List<Birthday>();

    static private List<Birthday> InitializeData()
    {
        koneksi con = new koneksi();
        var qcmd = "Select nm, sex, replace(hp, ' ', '') as Hp, datediff(YEAR, left(tgllhr, 4), '2016') as Umur, alamat  " +
                    "from mcif " +
                    "where right(tgllhr, 4) = 20160615";


        con.SqlQuery(qcmd);

        List<Birthday> Birthdaylist = new List<Birthday>();
        using (DataTable dt = con.QueryEx())
        {
            Birthdaylist = (from DataRow dr in dt.Rows

                       select new Birthday()
                       {
                           nama = Convert.ToString(dr["nm"]),
                           sex = Convert.ToString(dr["sex"]),
                           hp = Convert.ToString(dr["hp"]),
                           umur = Convert.ToInt32(dr["umur"]),
                           alamat = Convert.ToString(dr["alamat"])
                       }).ToList();

        }

这是我的联系

public class koneksi
{


    private SqlConnection _con;
    private SqlCommand cmd;
    private SqlDataAdapter _da;
    private DataTable _dt;


    public koneksi(string connectionstring=null)
    {
        var MyIni = new IniFile("setting.ini");
        string database = MyIni.Read("SET1", "Database");
        string userdb = MyIni.Read("SET2", "Database");
        string passdb = MyIni.Read("SET3", "Database");
        string locdb = MyIni.Read("SET4", "Database");

        _con = new SqlConnection(connectionstring??@"Data Source=" + locdb + ";Initial Catalog=" + database + "; User ID=" + userdb + ";password=" + passdb + "");
        _con.Open();

    }

    public void SqlQuery(string queryText)
    {
        cmd = new SqlCommand(queryText, _con);
    }

    public DataTable QueryEx()
    {
        _da = new SqlDataAdapter(cmd);
        _dt = new DataTable();
        _da.Fill(_dt);
        return _dt;
    }


}

此代码的形式为:

private void InitializeDataOlv()
    {


        this.flv.SetObjects(Birthday.GetBirthdays());
    }

我从this尝试了几个例子,但我的olv仍然是空的,实际上我还是初学者使用模型列表

0 个答案:

没有答案