如何在datagridview中使用list和其他数据表填充一列?

时间:2015-07-15 05:49:30

标签: c# datagridview datatable

我想显示两列' Flowers_Date'和'率'在dategicker中日期更改时的datagridview中。我正在使用List获取Flowers_Date的数据,并希望获取' Rate'来自数据表。我怎样才能做到这一点?可能吗?谢谢 ! 这是我的代码的片段。

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {

        String month = dateTimePicker1.Value.ToString("MM-yyyy");
        List<String> dates = new List<string>();

        String date1;
        cmd.Connection = conn;
        conn.Open();
        cmd.CommandText = ("SELECT Lilie_Date,Rate FROM lilie_master WHERE Lilie_Date LIKE '%" + month + "%'");
        OleDbDataReader rd = cmd.ExecuteReader();
        while (rd.Read())
        {

            date1 = rd["Lilie_Date"].ToString();
            if (dates.Contains(date1))
            {
                continue;
            }
            else
            {
                dates.Add(date1);

            }
        }

        conn.Close();

        DataTable dt = ListToDataTable(dates);
        dataGridView1.DataSource = dt;
        dataGridView1.Refresh();

    }

    private static DataTable ListToDataTable(List<String> list)
    {

        DataTable table = new DataTable();
        // DateTime dtime;

        table.Columns.Add("Lilie_Date");
        table.Columns.Add("Rate");

        table.Columns["Lilie_Date"].ReadOnly = true;

        int columns = 0;
        foreach (var array in list)
        {
            if (array.Length > columns)
            {
                columns = array.Length;
            }
        }
        foreach (var array in list)
        {
            table.Rows.Add(array);
        }
        return table;
    }

1 个答案:

答案 0 :(得分:2)

首先,您应该创建一个强类型类来存储Lilie_Date和Rate属性。它使代码更具可读性和清洁性。

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{

    String month = dateTimePicker1.Value.ToString("MM-yyyy");
    // List<String> dates = new List<string>();
    List<DateRatePair> lstPairs = new List<DateRatePair>();
    String date1;
    cmd.Connection = conn;
    conn.Open();
    cmd.CommandText = ("SELECT Lilie_Date,Rate FROM lilie_master WHERE Lilie_Date LIKE '%" + month + "%'");
    OleDbDataReader rd = cmd.ExecuteReader();
    while (rd.Read())
    {

        date1 = rd["Lilie_Date"].ToString();
        if (dates.Contains(date1))
        {
            continue;
        }
        else
        {
            // dates.Add(date1);
            DateRatePair aPair = new DateRatePair();
            aPair.Date = date1;
            aPair.Rate = rd["Rate"].ToString();
            lstPairs.Add(aPair);

        }
    }

    conn.Close();

    DataTable dt = ListToDataTable(lstPairs);
    dataGridView1.DataSource = dt;
    dataGridView1.Refresh();

}

private static DataTable ListToDataTable(List<DateRatePair> list)
{

    DataTable table = new DataTable();
    // DateTime dtime;

    table.Columns.Add("Lilie_Date");
    table.Columns.Add("Rate");

    table.Columns["Lilie_Date"].ReadOnly = true;

    int columns = 0;
    foreach (var array in list)
    {
        if (array.Length > columns)
        {
            columns = array.Length;
        }
    }
    foreach (var array in list)
    {
        DataRow dr = table.NewRow();
        dr["Lilie_Date"] = array.Date;
        dr["Rate"] = array.Rate;
        table.Rows.Add(dr);
    }
    return table;
}

然后你的代码应该修改如下(能够获取两个属性):

:

如果出现任何问题,请回复。