使用列表框删除 datagridview 中的列
我有两个列表框,一个 datagridview 和一个添加和删除按钮。 Listbox1 包含 datagridview 的列标题。 Listbox2 包含当前显示在 datagridview 上的列列表(带标题)。
我不知道要做的是:当您从第一个列表框中选择一个标题并单击“添加”按钮时,如何让带有该标题的列出现在 datagridview 上?
我是否需要将列标题的字符串值绑定到列,然后在按下正确的按钮时添加和删除它们?使困惑
I have two list boxes, a datagridview and an Add and Remove button. Listbox1 contains the headers for the columns of the datagridview. Listbox2 contains the list of columns(with headers) currently displayed on the datagridview.
What I can't figure out to do is: when you select a header from the first listbox and hit the Add button how can I get a column with that header to appear on the datagridview?
Do I need to bind the string values of the column headers to a column and then have them add and delete when the correct button is pressed? confused
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用它来显示和隐藏列
隐藏
dataGridView.Columns["ColumnName"].Visible = false
显示
dataGridView.Columns["ColumnName"].Visible = true
use this to show and hide column
Hide
dataGridView.Columns["ColumnName"].Visible = false
Show
dataGridView.Columns["ColumnName"].Visible = true
我将已经添加所有列,但设置了可见性,以便在不应该显示的情况下不会显示它们。当您单击“添加列”按钮时,只需按名称找到该列并更改可见性。删除它也一样。
然后你就不需要担心重新绑定或类似的事情。
I would have all of the columns already added, but have the visibility set so that they aren't shown if they shouldn't be. When you click the button to "add a column" just find that column by name and change the visibility. Same for removing it.
Then you don't need to worry about re-binding or anything like that.