【问题标题】:Creating Datagridview at runtime在运行时创建 Datagridview
【发布时间】:2012-01-29 07:02:19
【问题描述】:

我已经走到了尽头……

我正在从 dateTimePicker 中获取一个值,并且我正在根据 dateTimePicker 中的值执行 SQL 查询。执行查询后,查询结果应显示在 Datagridview 上。

这是我目前制作的代码:

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

       }    

    }
}

我对 c# 非常缺乏经验,所以任何答案都将不胜感激。

请帮忙。

谢谢,

子目录

【问题讨论】:

    标签: c# sql datagridview


    【解决方案1】:

    使用此代码:

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

    【讨论】:

      【解决方案2】:

      查看以下链接:

      How to: Bind Data to the Windows Forms DataGridView Control

      DataGridView 对象从“绑定源”读取数据,您必须将其链接到包含 SQL SELECT 语句的 TableAdapter。

      你大约是那里的 1/2。让连接字符串正常工作是成功的一半。另一半是找出您的数据与 DatagridView 相关的位置。

      祝你好运!

      【讨论】:

      • 如果你喜欢这个答案;请投票,以便其他人也可以看到链接!
      【解决方案3】:

      您必须将参数添加到SqlCommand 才能使用它:

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

      【讨论】:

        【解决方案4】:

        正如我看到您的代码,您缺少参数值的分配,

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

        在此行之后,您必须为命令对象提供参数值。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-13
          • 2011-05-22
          • 1970-01-01
          相关资源
          最近更新 更多