【发布时间】:2014-05-22 01:20:07
【问题描述】:
我遇到了数据网格问题,并在添加新数据时刷新它。我试图让它工作的方式是。
在主窗体上,一个按钮(“添加”)单击事件显示一个带有字段的 form2,用于将新数据输入到主窗体中的表中。一旦输入了数据,然后一个按钮(“插入/添加”)单击事件关闭 form2 并在主表单数据网格中显示新输入的数据。
问题是我不知道如何刷新或更新数据网格以显示新信息。任何帮助将不胜感激。
主要形式:
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 Microsoft.Win32;
namespace WindowsFormsApplication2
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'userLoginDataSet.WeaponData' table. You can move, or remove it, as needed.
this.weaponDataTableAdapter.Fill(this.userLoginDataSet.WeaponData);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
AddWeapon aw = new AddWeapon();
aw.Show();
}
}
}
添加武器形态:
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 System.Data.SqlClient;
using System.Data.Sql;
using Microsoft.Win32;
using System.Threading;
namespace WindowsFormsApplication2
{
public partial class AddWeapon : Form
{
public AddWeapon()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\brmcbrid\Documents\Visual Studio 2010\Projects\WindowsFormsApplication2\WindowsFormsApplication2\UserLogin.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand cmd = new SqlCommand("INSERT into WeaponData values('" + serialNumber.Text + "','" + brand.Text + "','" + model.Text + "','" + caliber.Text + "','" + type.Text + "' , '" + dateAcquired.Text + "', '" + dateSold.Text + "', '" + purchasePrice.Text + "', '" + sellPrice.Text + "', '" + notes.Text + "')", con);
this.Close();
}
}
}
【问题讨论】:
-
这个要求似乎很接近这个:Reload data using TableAdapter