从 winform 搜索按钮提交时,Datagridview 未填充
Datagridview is not populating when submitting from winform search button
我目前有一个正在编译的 C# winform,零错误和零警告:
这是我第一次参与 winform 项目:
注意:我在 ASPX 中有一个类似的项目按预期工作
但是这个需要在 winform 中。
我还需要从一个空的数据网格开始,因为在生产环境中,我们将调用的 table 包含数百万行。
(这不能是数据网格类型解决方案的过滤器,它会在生产环境中杀死服务器。)
winform 超级简单,包括:
- 一个表单:GetParentID
- 一个文本框:textBoxValueToSearch
- 一个按钮:BTN_SEARCH
- 数据网格视图:ParentIDOutput
- app.config 中的连接字符串:RCPDEV(已测试且有效)
问题:当我在文本框中输入一个值并单击搜索按钮时,我希望在数据网格视图中返回一个 Parent_Container_Id。然而点击按钮没有任何反应。
我已经测试了存储过程代码,它工作正常:
- 图片:
- get parent id stored procedure code
- get parent id win form
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());
}
}
}
IN 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 个小时左右,我一直在努力尝试许多不同的事情,并阅读了大量 articles/tutorials/and 帮助票,但无济于事。
能解决这个问题,我听你的摆布。
您应该订阅类似 Control.Click 的事件以响应用户在 winform 控件上的任何操作。
// BTN_SEARCH
//
this.BTN_SEARCH.Click += BTN_SEARCH_Click;
或者您可以订阅MouseClick。
您应该查看 Control.Event 列表以找出适合您情况的事件。
您可以在属性上订阅事件 Window 而不是编写代码。
我目前有一个正在编译的 C# winform,零错误和零警告:
这是我第一次参与 winform 项目:
注意:我在 ASPX 中有一个类似的项目按预期工作
但是这个需要在 winform 中。
我还需要从一个空的数据网格开始,因为在生产环境中,我们将调用的 table 包含数百万行。
(这不能是数据网格类型解决方案的过滤器,它会在生产环境中杀死服务器。)
winform 超级简单,包括:
- 一个表单:GetParentID
- 一个文本框:textBoxValueToSearch
- 一个按钮:BTN_SEARCH
- 数据网格视图:ParentIDOutput
- app.config 中的连接字符串:RCPDEV(已测试且有效)
问题:当我在文本框中输入一个值并单击搜索按钮时,我希望在数据网格视图中返回一个 Parent_Container_Id。然而点击按钮没有任何反应。
我已经测试了存储过程代码,它工作正常:
- 图片:
- get parent id stored procedure code
- get parent id win form
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());
}
}
}
IN 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 个小时左右,我一直在努力尝试许多不同的事情,并阅读了大量 articles/tutorials/and 帮助票,但无济于事。
能解决这个问题,我听你的摆布。
您应该订阅类似 Control.Click 的事件以响应用户在 winform 控件上的任何操作。
// BTN_SEARCH
//
this.BTN_SEARCH.Click += BTN_SEARCH_Click;
或者您可以订阅MouseClick。
您应该查看 Control.Event 列表以找出适合您情况的事件。
您可以在属性上订阅事件 Window 而不是编写代码。