怎么清空dataGridView
以下是我的代码:
public partial class FrmWareList : Form
{
private DataSet dataSet = new DataSet();
private SqlDataAdapter dataAdapter;
string Sql;
public FrmWareList()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
lb();
tj();
}
private void lb()
{
Sql = "SELECT WareNO,WareName,UnitPrice,WareId FROM Wares";
if(comboBox1.SelectedIndex == 0 || comboBox1.SelectedIndex == -1)
{
}
else if (comboBox1.SelectedIndex == 1)
{
Sql = "SELECT WareNO,WareName,UnitPrice,WareId FROM Wares Where CategoryId like '1' ";
}
else
{
Sql = "SELECT WareNO,WareName,UnitPrice,WareId FROM Wares Where CategoryId like '2' ";
}
dataAdapter = new SqlDataAdapter(Sql, DB.connection);
dataAdapter.Fill(dataSet, "Wares");
dataGridView1.DataSource = dataSet.Tables["Wares"];
}
private void tj()
{
Sql = "SELECT CatagoryName FROM WareManagement";
SqlCommand command = new SqlCommand(Sql, DB.connection);
try
{
DB.connection.Open();
SqlDataReader dataReader = command.ExecuteReader();
string lx = "";
while (dataReader.Read())
{
lx = (string)dataReader[0];
comboBox1.Items.Add(lx);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DB.connection.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确实要将修改保存到数据库中吗?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (result == DialogResult.OK)
{
SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter);
dataAdapter.Update(dataSet, "Wares");
}
}
private void button2_Click(object sender, EventArgs e)
{
lb();
}
}
谢谢了!