从Winform搜索按钮提交时,Datagridview没有填充

时间:2020-07-13 21:29:54

标签: c# winforms search stored-procedures textbox

我目前有一个正在编译的C#winform,其中包含零错误和零警告:

这是我第一次参加winform项目:

注意:我在ASPX中有一个类似的项目,该项目按预期工作

但是这个必须是winform。

我还需要从一个空的数据网格开始,因为在生产环境中,我们将调用的表包含数百万行。

(这不能过滤将杀死生产环境中的服务器的数据网格类型解决方案。)

winform超级简单,包含:

  • 一种形式:GetParentID
  • 文本框:textBoxValueToSearch
  • 按钮:BTN_SEARCH
  • 数据网格视图:ParentIDOutput
  • app.config中的连接字符串:RCPDEV(已测试并且可以正常工作)

问题:当我在文本框中输入一个值并单击搜索按钮时,我希望在数据网格视图中返回一个Parent_Container_Id。但是,单击按钮没有任何作用。

我已经测试了存储过程代码,并且可以正常工作:


IN PROGRAM.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace GetParentID
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new GetParentID());
        }
    }
}

在GETPARENTID.DESIGNER.CS中

namespace GetParentID
{
    partial class GetParentID
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBoxValueToSearch = new System.Windows.Forms.TextBox();
            this.BTN_SEARCH = new System.Windows.Forms.Button();
            this.ParentIDOutput = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.ParentIDOutput)).BeginInit();
            this.SuspendLayout();
            // 
            // textBoxValueToSearch
            // 
            this.textBoxValueToSearch.Location = new System.Drawing.Point(31, 22);
            this.textBoxValueToSearch.Name = "textBoxValueToSearch";
            this.textBoxValueToSearch.Size = new System.Drawing.Size(268, 20);
            this.textBoxValueToSearch.TabIndex = 0;
            // 
            // BTN_SEARCH
            // 
            this.BTN_SEARCH.Location = new System.Drawing.Point(305, 20);
            this.BTN_SEARCH.Name = "BTN_SEARCH";
            this.BTN_SEARCH.Size = new System.Drawing.Size(75, 23);
            this.BTN_SEARCH.TabIndex = 1;
            this.BTN_SEARCH.Text = "Search";
            this.BTN_SEARCH.UseVisualStyleBackColor = true;
            // 
            // ParentIDOutput
            // 
            this.ParentIDOutput.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.ParentIDOutput.Location = new System.Drawing.Point(31, 69);
            this.ParentIDOutput.Name = "ParentIDOutput";
            this.ParentIDOutput.Size = new System.Drawing.Size(349, 67);
            this.ParentIDOutput.TabIndex = 2;
            // 
            // GetParentID
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(408, 299);
            this.Controls.Add(this.ParentIDOutput);
            this.Controls.Add(this.BTN_SEARCH);
            this.Controls.Add(this.textBoxValueToSearch);
            this.Name = "GetParentID";
            this.Text = "GetParentID";
            ((System.ComponentModel.ISupportInitialize)(this.ParentIDOutput)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBoxValueToSearch;
        private System.Windows.Forms.Button BTN_SEARCH;
        private System.Windows.Forms.DataGridView ParentIDOutput;
    }
}

IN GETPARENTID.CS

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GetParentID
{
    public partial class GetParentID : Form
    {
        public GetParentID()
        {
            InitializeComponent();
        }
        BindingSource binder = new BindingSource();

        private void BTN_SEARCH_Click(object sender, EventArgs e)
        {

            // SqlConnection sqlCon = new SqlConnection(RCPDEV)
            string constr;

            string containerIdValue = textBoxValueToSearch.Text;

            constr = Properties.Settings.Default.RCPDEV;

            SqlConnection con = new SqlConnection(constr);

            con.Open();

            SqlCommand cmd = new SqlCommand("RFID_GET_CONTAINER_PARENT_ID", con);

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@CONTAINER_ID", SqlDbType.NVarChar, 25).Value = containerIdValue;

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            DataTable dataTable = new DataTable();

            adapter.Fill(dataTable);

            ParentIDOutput.DataSource = binder;

            binder.DataSource = dataTable;

            con.Close();

            //FillDataGridView();
        }
    }
}

关于我在这里做错了什么的任何想法

/在过去16个小时左右的时间里,我一直在梳理头发,尝试许多不同的事情,阅读了大量的文章/教程/帮助票,但徒劳无功。

请允许我解决此问题。

1 个答案:

答案 0 :(得分:0)

您应该订阅Control.Click之类的事件,以响应用户对winform控件的任何操作。

// BTN_SEARCH
// 
this.BTN_SEARCH.Click += BTN_SEARCH_Click;

或者您可以订阅MouseClick

您应该查看Control.Event列表,以找出适合您情况的事件。

您可以在“属性”窗口上订阅事件,而无需编写代码。

Event List