如何重置/重新加载/卸载我的DataGridView?

时间:2019-08-24 08:11:16

标签: c# sql-server datagridview relational-database

我在处理作业/作业上的问题时遇到了这个问题。我想将SQL数据库中的多个数据表加载到多个datagridviews中,但是当我单击另一个选项卡(在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-S1O2044\SQLEXPRESS;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事件不使以前的datagridview的旧列出现在新加载的datagridview上。或者我只希望每个单独的datagridview都保存我的SQL数据库中的一个表。

1 个答案:

答案 0 :(得分:1)

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

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;
        }