How to fill one column using list and other by datatable in datagridview?

lss mesy

I want to display two columns 'Flowers_Date' and 'rate' in datagridview when date changes in datepicker. I am fetching data for Flowers_Date using List and wants to fetch data for 'Rate' from datatable. How can i do that? is it possible? thanks ! here is the snippet of my code.

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;
    }
Hoa Nguyen

Firstly, you should create a strongly-type class to store Lilie_Date and Rate properties. It makes the code more readable and clean.

public class DateRatePair
{
     public String Date { get; set; }
     public String Rate { get; set; }
}

Then your code should be modified as below (able to fetch both of 2 properties):

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;
}

Response if any problems arises.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Fill column of dataframe using a list

From Dev

How to create new DataTable with column structure from other DataTable?

From Dev

How to get list of one column values from DataTable?

From Dev

How can i fill the other form fields automatically if user fill one field in jsp (while using spring mvc and hibernate)

From Dev

How to concatenate Datagridview rows values and store it into one datatable Cell

From Dev

How to fill a column conditionally to values in an other column being in a list?

From Dev

How to fill datagridview grayed area using empty rows

From Dev

How to serialize and deserialize JSON file into a DataTable to fill a DataGridView?

From Dev

Fill data of one row using other table column sum in mysql

From Dev

Multiple Queries to Fill One DataTable

From Dev

MySQL: How do I list one column and sum the other?

From Dev

How to create new DataTable with column structure from other DataTable?

From Dev

How to modify one column and copy one DataTable into another DataTable

From Dev

How to get list of one column values from DataTable?

From Dev

DataGridView column filter using datatable as source

From Dev

How can i fill the other form fields automatically if user fill one field in jsp (while using spring mvc and hibernate)

From Dev

How to bind a list of objects of a class to a datagridview whose one field is again an object of some other class?

From Dev

How to fill a column automatically from special values in other column?

From Dev

List values from one column not found in the other

From Dev

Read .csv to datatable and fill a datagridview

From Dev

How to fill WPF DataGridView from related tables using LINQ query?

From Dev

How to update first column of a datatable for all rows using LINQQ or any other method?

From Dev

How to make Jquery DataTable Column as HyperLink or ActionLink with other column value?

From Dev

Fill a combobox with a column in datagridview

From Dev

How to fill TextBox dynamically in a GridView using a DataTable

From Dev

How to make new datatable using other tables

From Dev

How to select one column in Datatable using Jquery

From Dev

How to fill a datagridview with data depending on the value in a database column

From Dev

How to get row's column value from datatable to textbox after double click on datagridview row without using properties

Related Related

  1. 1

    Fill column of dataframe using a list

  2. 2

    How to create new DataTable with column structure from other DataTable?

  3. 3

    How to get list of one column values from DataTable?

  4. 4

    How can i fill the other form fields automatically if user fill one field in jsp (while using spring mvc and hibernate)

  5. 5

    How to concatenate Datagridview rows values and store it into one datatable Cell

  6. 6

    How to fill a column conditionally to values in an other column being in a list?

  7. 7

    How to fill datagridview grayed area using empty rows

  8. 8

    How to serialize and deserialize JSON file into a DataTable to fill a DataGridView?

  9. 9

    Fill data of one row using other table column sum in mysql

  10. 10

    Multiple Queries to Fill One DataTable

  11. 11

    MySQL: How do I list one column and sum the other?

  12. 12

    How to create new DataTable with column structure from other DataTable?

  13. 13

    How to modify one column and copy one DataTable into another DataTable

  14. 14

    How to get list of one column values from DataTable?

  15. 15

    DataGridView column filter using datatable as source

  16. 16

    How can i fill the other form fields automatically if user fill one field in jsp (while using spring mvc and hibernate)

  17. 17

    How to bind a list of objects of a class to a datagridview whose one field is again an object of some other class?

  18. 18

    How to fill a column automatically from special values in other column?

  19. 19

    List values from one column not found in the other

  20. 20

    Read .csv to datatable and fill a datagridview

  21. 21

    How to fill WPF DataGridView from related tables using LINQ query?

  22. 22

    How to update first column of a datatable for all rows using LINQQ or any other method?

  23. 23

    How to make Jquery DataTable Column as HyperLink or ActionLink with other column value?

  24. 24

    Fill a combobox with a column in datagridview

  25. 25

    How to fill TextBox dynamically in a GridView using a DataTable

  26. 26

    How to make new datatable using other tables

  27. 27

    How to select one column in Datatable using Jquery

  28. 28

    How to fill a datagridview with data depending on the value in a database column

  29. 29

    How to get row's column value from datatable to textbox after double click on datagridview row without using properties

HotTag

Archive