【问题标题】:Datagridview is not populating when submitting from winform search button从 winform 搜索按钮提交时未填充 Datagridview
【发布时间】:2020-07-13 21:29:54
【问题描述】:

我目前有一个 C# winform,它正在以零错误和零警告进行编译:

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

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

但是这个需要在winform中。

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

(这不能是过滤数据网格类型的解决方案,它会杀死生产中的服务器。)

winform 非常简单,包括:

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

问题:当我在文本框中输入一个值并单击搜索按钮时,我希望在数据网格视图中返回一个 Parent_Container_Id。但是点击按钮什么也没做。

我已经测试了存储过程代码,并且运行正常:


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

在 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 个小时左右,我一直在努力尝试许多不同的事情,阅读了大量的文章/教程/帮助票无济于事。

我很乐意解决这个问题。

【问题讨论】:

    标签: c# winforms search stored-procedures textbox


    【解决方案1】:

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

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

    或者您可以订阅MouseClick

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

    您可以在属性窗口上订阅事件,而不是编写代码。

    【讨论】:

    • 感谢 ibocon 和 jimi 的宝贵帮助,我的表单正在运行 :) -- jimi 这篇文章是缺失的部分 -- 现在我该如何为你投票??
    • @joopswip 为了更新某人的答案,您需要获得一些声誉。我想这是您的第一篇文章,我认为您的声誉不够。
    • 谢谢 ibocon .. 现在可以赚取一些代表点数了 :) -- 并回报和帮助他人。
    猜你喜欢
    • 2016-08-09
    • 2012-01-09
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多