如果我';m使用列表从数据库中收集信息
本文关键字:数据库 信息 列表 如果 | 更新日期: 2023-09-27 18:29:40
我正在像麦当劳的控制面板一样进行选择客户想要的东西。我正在使用Windows窗体使用C#。我创建了一个名为products的类在Form1.cs中之后,我列出了一个列表,将从数据库中读取的产品。我设法做到了这一点,并在DataGrid上显示了结果。我的问题是,当我选择产品时然后我点击发送按钮,数据网格不会自动刷新。我必须关闭程序,何时我重新启动它,我可以看到变化。
我的问题是,有人知道如何更新数据网格吗退出重新启动程序?
非常感谢,我在下面添加了我的代码:
[]我的_windows_form1
#using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
MySqlConnection conn = new MySqlConnection();
String connectionString = "Server=127.0.0.1; Database=mydatabase; Uid=root; Pwd=;";
List<products> listproducts = new List<products>();
public string product;
public string quantity;
public Form1()
{
InitializeComponent();
startConn();
}
private void startConn()
{
try
{
conn.ConnectionString = connectionString;
conn.Open();
textBox3.Text= "Correct connection";
//we call the function READ
read();
}
catch (MySqlException)
{
textBox3.Text="An error has ocurred";
}
}
public void read()
{
MySqlCommand instruccio = conn.CreateCommand();
instruccio.CommandText = "Select * from products";
MySqlDataReader search = instruccio.ExecuteReader();
while (search.Read())
{
products prod = new products();
prod.IdProd = search["idProd"].ToString();
prod.Name = search["nomProd"].ToString();
prod.Quantity = Int32.Parse(search["quantitat"].ToString());
listproducts.Add(prod);
}
dataGridView1.DataSource = listproducts;
search.Close();
search.Dispose();
}
private void btnEnviar_Click(object sender, EventArgs e) //Button Send (Enviar in spanish)
{
try
{
//before updating the stock in database we query the total quantity of the product selected
MySqlCommand instruccio1 = connexio.CreateCommand();
instruccio1.CommandText = "Select quantitat from productes where `nomProd`='"+ this.product +"'";
MySqlDataReader read = instruccio1.ExecuteReader();
int result = 0;
while (read.Read())
{
resultat=Int32.Parse(read["quantitat"].ToString());
}
read.Dispose();
instruccio1.Dispose();
if (this.quantity != 0)
{
if (result > this.quantity)
{
int difference = result - this.quantity;
MySqlCommand instruccio2 = conn.CreateCommand();
instruccio2.CommandText = "UPDATE products set `quantitat`='" + this.difference + "' where products.nomProd='" + this.product + "'";
instruccio2.ExecuteNonQuery();
conn.Close();
startConn();
textBox1.Text= "";
textBox2.Text = "";
this.quantity = "";
this.product = "";
}
else
{
MessageBox.Show("There's no quantity.");
}
}
catch (Exception xe)
{
MessageBox.Show("",xe.Message);
}
}
private void btnEsborrar_Click(object sender, EventArgs e) //Erase button
{
this.quantity = "";
this.product = "";
this.aEnviar = 0;
textBox1.Text = quantity;
textBox2.Text = product;
}
....
....
您可以执行以下操作:
- 在窗体上添加一个计时器控件,并设置要触发其Tick事件的时间,该时间以毫秒为单位放入Interval属性中
- 在此事件上调用函数
startConn
这很简单:)
在之前的read()
方法中
dataGridView1.DataSource = listproducts;
添加
dataGridView1.DataSource = null;
然后随时调用startConn()
方法。