Subash
Subash

Reputation: 165

Creating Datagridview at runtime

I am at the end of the rope...

I am taking a value from a dateTimePicker and I am executing a SQL query based on the value from the dateTimePicker. The result of the query should be displayed on a Datagridview after executing the query.

Here is the code I have made so far:

namespace App1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            DateTime varDate;
            varDate = dateTimePicker1.Value; 
              }


        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=SUBASH-LAPTOP\COBRA;Initial Catalog=Test;Integrated Security=True");

            SqlCommand Command = con.CreateCommand();

            Command.CommandText = "Select * From orders where date_purchased <= @varDate ";

            con.Open();

            SqlDataReader reader = Command.ExecuteReader();

            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add(); 
            DataGridView d1 = new DataGridView();
            this.Controls.Add(d1);
            while (reader.Read())
            {
                d1.DataSource = ds.Tables[0];

       }    

    }
}

I pretty inexperienced with c# so any answers would be most appreciated.

Please help.

Thanks,

Subash

Upvotes: 2

Views: 5647

Answers (4)

MRM
MRM

Reputation: 435

use this code:

private void button1_Click(object sender, EventArgs e)
{

    SqlConnection con = new SqlConnection(@"Data Source=SUBASH-LAPTOP\COBRA;Initial Catalog=Test;Integrated Security=True");

    SqlCommand Command = con.CreateCommand();

    SqlDataAdapter dp = new SqlDataAdapter("Select * From orders where date_purchased <= @varDate", con);
    dp.SelectCommand.Parameters.AddWithValue("@varDate", dateTimePicker1.Value);
    DataSet ds = new DataSet();
    dp.Fill(ds);
    DataGridView d1 = new DataGridView();
    d1.DataSource = ds;
    d1.DataMember = ds.Tables[0].TableName;
    this.Controls.Add(d1);

}    

Upvotes: 2

DevinBM
DevinBM

Reputation: 91

Check out the Link below:

How to: Bind Data to the Windows Forms DataGridView Control

the DataGridView object reads in data from a "binding source" you have to link that to a TableAdapter that contains your SQL SELECT statement.

You're about 1/2 the way there. Getting the connection string working correctly is half the battle. The other half is finding out where your data is in relation to the DatagridView.

good Luck!

Upvotes: 2

Ravi Gadag
Ravi Gadag

Reputation: 15881

as i saw your code, you are missing assignment of parmeter value,

 Command.CommandText = "Select * From orders where date_purchased <= @varDate ";

after this line you have to provide parmeter value to command object.

Command.Parameters.AddWithValue("@varDate", DateTimePicker1.Value);

Upvotes: 1

Andomar
Andomar

Reputation: 238296

You have to add the parameter to the SqlCommand for it to be used:

Command.Parameters.AddWithValue("@varDate", DateTimePicker1.Value);

Upvotes: 1

Related Questions