如何重置/重新加载/卸载我的数据网格视图?



我在处理作业/家庭作业时遇到了这个问题。我想将多个数据表从我的SQL数据库加载到多个数据网格视图中,但是当我单击另一个选项卡(在TabControl上使用SelectedIndexChanged)时,旧加载表的列仍然存在。我只希望每个选项卡显示特定的表(列)。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace assignment2Database
{
public partial class Form1 : Form
{
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
DataTable table = new DataTable();
string str = @"Data Source=DESKTOP-S1O2044SQLEXPRESS;Initial Catalog=ElectroShopDB;Integrated Security=True";
private void tabSupplier_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl.SelectedIndex == 0)
{
connection = new SqlConnection(str);
connection.Open();
loadCatalogue();
}
else if (tabControl.SelectedIndex == 1)
{
connection = new SqlConnection(str);
connection.Open();
loadSupplier();
}
}
void loadCatalogue()
{
command = connection.CreateCommand();
command.CommandText = "select catalogueID,catalogueName from Catalogue";
adapter.SelectCommand = command;
table.Clear();
adapter.Fill(table);
dgvCatalogue.DataSource = table;
}
private void Form1_Load(object sender, EventArgs e)
{
connection = new SqlConnection(str);
connection.Open();
loadCatalogue();
}
void loadSupplier()
{
command = connection.CreateCommand();
command.CommandText = "select supplierID,supplierName from Supplier";
adapter.SelectCommand = command;
table.Clear();
adapter.Fill(table);
dgvSupplier.DataSource = table;
}

我希望在选项卡控件上的每个选项卡上触发 SelectedIndexChanged 事件时,不会在新加载的数据网格视图上显示以前的数据网格视图的旧列。或者我只希望每个单独的数据网格视图保存我的 SQL 数据库中的表。

取消绑定第一个数据源,然后重新绑定:

dgvSupplier.DataSource = null;
dgvSupplier.DataSource = table;

这将踢掉所有较旧的列,只填充您需要的列。在用于重新填充网格的每种方法中执行此操作:

void loadCatalogue()
{
command = connection.CreateCommand();
command.CommandText = "select catalogueID,catalogueName from Catalogue";
adapter.SelectCommand = command;
table.Clear();
adapter.Fill(table);
dgvSupplier.DataSource = null;
dgvCatalogue.DataSource = table;
}
void loadSupplier()
{
command = connection.CreateCommand();
command.CommandText = "select supplierID,supplierName from Supplier";
adapter.SelectCommand = command;
table.Clear();
adapter.Fill(table);
dgvSupplier.DataSource = null;
dgvSupplier.DataSource = table;
}

这样。

编辑:

此外,您可以在填充方法中创建新的适配器来清空它们:

void loadCatalogue()
{
SqlDataAdapter catalogueAdapter = new SqlDataAdapter();
command = connection.CreateCommand();
command.CommandText = "select catalogueID,catalogueName from Catalogue";
catalogueAdapter.SelectCommand = command;
table.Clear();
catalogueAdapter.Fill(table);
dgvSupplier.DataSource = null;
dgvCatalogue.DataSource = table;
}
void loadSupplier()
{
SqlDataAdapter supplierAdapter = new SqlDataAdapter();
command = connection.CreateCommand();
command.CommandText = "select supplierID,supplierName from Supplier";
supplierAdapter.SelectCommand = command;
table.Clear();
supplierAdapter.Fill(table);
dgvSupplier.DataSource = null;
dgvSupplier.DataSource = table;
}

最新更新