gpt4 book ai didi

c# - 我只需要处理特定选定表的列名

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:49 25 4
gpt4 key购买 nike

当我从 ComboBox 中选择一个表时,我会得到该表中存在的每一列(以及在我检查它时隐藏它的选项)。

enter image description here

当我从“组合框”中选择下一个表格时,我得到了新表格列以及第一个表格中的前一个表格。

enter image description here

如何解决这个问题并只保存选定的表格列?

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var tableName = comboBox1.SelectedItem;

sqlDa = new SqlDataAdapter($"SELECT * FROM {tableName}", form1.conn = new SqlConnection($"Server = {form1.ServerBox.Text }; Database = { form1.DBBox.Text}; Trusted_Connection = True"));
dataTable = new DataTable();
sqlDa.Fill(dataTable);
dataGridView1.DataSource = dataTable;

foreach (DataGridViewColumn column in dataGridView1.Columns)
{
var item = (ToolStripMenuItem)contextMenuStrip1.Items.Add(column.HeaderText);
var item2 = comboBox2.Items.Add(column.HeaderText);
item.Tag = column.Name;
item.Checked = column.Visible;
item.CheckOnClick = true;

item.CheckedChanged += (obj, args) =>
{
var i = (ToolStripMenuItem)obj;
dataGridView1.Columns[(string)i.Tag].Visible = i.Checked;
};
}
}

最佳答案

您需要先清除 contextMenuStrip1Items 集合,然后再填充它。它可以通过 contextMenuStrip1.Items.Clear() 简单地完成,但是为了防止应用程序中可能出现句柄/内存泄漏,最好在删除菜单项后立即处理它们:

var items = contextMenuStrip1.Items.Cast<ToolStripItem>().ToList();
contextMenuStrip1.Items.Clear();
items.ForEach(x => x.Dispose());

示例

这是一个工作示例,它需要一个 DataGridView、一个 ComboBox 和一个 ContextMenuStrip。它将数据库的表加载到 ComboBox 中,当您从 ComboBox 中选择索引时,它将所选表的数据加载到 DataGridView 中,并且如果您右键单击 DataGridView 的列标题,将显示一个 ContextMenuStrip,允许您设置列的可见性:

string server = ".";
string db = "TestDB";
private void Form1_Load(object sender, System.EventArgs e)
{
var connection = $"SERVER={server};DATABASE={db};Integrated Security=true";

//Hanlde combo box selected index changed
comboBox1.SelectedIndexChanged += (obj1, args1) =>
{
dataGridView1.DataSource = null;
if (comboBox1.SelectedIndex > -1)
{
//Fill data table and show data in data grid view
var data = GetDataTable($"SELECT * FROM {comboBox1.SelectedValue}", connection);
dataGridView1.Columns.Clear();
dataGridView1.DataSource = data;
}

//Clear existing items of context menu strip
var items = contextMenuStrip1.Items.Cast<ToolStripItem>().ToList();
contextMenuStrip1.Items.Clear();
items.ForEach(x => x.Dispose());

//Add the columns to context menu strip
foreach (DataGridViewColumn c in dataGridView1.Columns)
{
var item = (ToolStripMenuItem)contextMenuStrip1.Items.Add(c.HeaderText);
item.Tag = c.Name;
item.Checked = c.Visible;
item.CheckOnClick = true;
//Hanlde CheckStateChanged event of context menu strip items
item.CheckStateChanged += (obj, args) =>
{
var i = (ToolStripMenuItem)obj;
dataGridView1.Columns[(string)i.Tag].Visible = i.Checked;
};
}
};

//Load table names
var tables = GetDataTable("SELECT Name FROM Sys.Tables", connection);
comboBox1.ValueMember = "Name";
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = tables;

//Show context menu strip on right click on data grid veiw header
dataGridView1.CellMouseClick += (obj, args) =>
{
if (args.RowIndex == -1 && args.Button == MouseButtons.Right)
contextMenuStrip1.Show(Cursor.Position);
};
}

DataTable GetDataTable(string commandText, string connectionString)
{
using (var da = new SqlDataAdapter(commandText, connectionString))
{
var dt = new DataTable();
da.Fill(dt);
return dt;
}
}

关于c# - 我只需要处理特定选定表的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56932912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com