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.Threading.Tasks;
using System.Windows.Forms;
namespace Green_Sales_1._0
{
public partial class SupplierFrm : Form
{
SqlConnection con = new SqlConnection();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
public SupplierFrm()
{
InitializeComponent();
}
这用于在db
中插入记录 private void Button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=DESKTOP- HSN6TIC\\SQLEXPRESS;Initial Catalog=ShopManager;Integrated Security=True";
con.Open();
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO [ShopManager].[dbo].[supplier]([supName],[cno],[emailId],[supAdd],[city],[pincode])VALUES('" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
MessageBox.Show("Supplier Saved");
this.dataGridView1.Refresh();
this.dataGridView1.Parent.Refresh();
}
catch (Exception exp)
{
String error = exp.ToString();
MessageBox.Show("Database Error : "+error);
}
}
private void SupplierFrm_Load(object sender, EventArgs e)
{
this.supplierTableAdapter.Fill(this.shopManagerDataSet.supplier);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
string supId = dataGridView1.SelectedRows[0].Cells[0].Value + string.Empty;
string supName = dataGridView1.SelectedRows[0].Cells[1].Value + string.Empty;
string cno = dataGridView1.SelectedRows[0].Cells[2].Value + string.Empty;
string email = dataGridView1.SelectedRows[0].Cells[3].Value + string.Empty;
string address = dataGridView1.SelectedRows[0].Cells[4].Value + string.Empty;
string city = dataGridView1.SelectedRows[0].Cells[5].Value + string.Empty;
string pincode = dataGridView1.SelectedRows[0].Cells[6].Value + string.Empty;
textBox1.Text = supId;
textBox2.Text = supName;
textBox3.Text = cno;
textBox4.Text = email;
textBox5.Text = address;
textBox6.Text = city;
textBox7.Text = pincode;
}
}
这是为了更新记录 我想在点击刷新按钮
后重新加载datagridview private void button3_Click(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=DESKTOP-HSN6TIC\\SQLEXPRESS;Initial Catalog=ShopManager;Integrated Security=True";
con.Open();
try
{
string supid = textBox1.Text;
SqlCommand cmd = new SqlCommand("UPDATE [ShopManager].[dbo].[supplier] SET [supName] = '" + textBox2.Text + "',[cno] ='" + textBox3.Text + "',[emailId] = '" + textBox4.Text + "',[supAdd] = '" + textBox5.Text + "',[city] = '" + textBox6.Text + "',[pincode] = '" + textBox7.Text + "'WHERE [supId] = '" + supid + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
MessageBox.Show("Supplier Updated");
}
catch (Exception exp)
{
String error = exp.ToString();
MessageBox.Show("Database Error : "+error);
}
}
这里我想点击Button5
后刷新我的Datagridview private void button5_Click(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=DESKTOP-HSN6TIC\\SQLEXPRESS;Initial Catalog=ShopManager;Integrated Security=True";
con.Open();
try
{
String str1 = "select * from [ShopManager].[dbo].[supplier] ";
SqlCommand cmd = new SqlCommand(str1, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand = new SqlCommand(str1, con);
da.Fill(dt);
con.Close();
dataGridView1.DataSource = ds.Tables;
dataGridView1.RefreshEdit();
}
catch (Exception exp)
{
MessageBox.Show("Error : "+ exp);
}
}
}
}
我尝试了很多解决方案,但这些不适用于我的代码
答案 0 :(得分:0)
添加此代码以刷新dataGridView
this.supplierTableAdapter.Fill(this.shopManagerDataSet.supplier);
this.dataGridView1.DataSource = this.shopManagerDataSet.supplier;
this.dataGridView1.Refresh();