如何过滤每个来自DataTable的DataGridView中的列? [英] How to filter columns in each DataGridView coming from DataTable?

查看:127
本文介绍了如何过滤每个来自DataTable的DataGridView中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计和实现软件系统的过程中,我遇到了各种性能问题.结果,我正在尝试找到最小化内存使用并提高读写性能的方法.我偶然发现了使用DataTable来存储和处理数据库部分的可能性.但是,我不太熟悉它以及它与DataGridView的关联,因此我要求在此领域提供一些指导.

我想要做的是拥有一个大的DataTable.我想在DataGridView中显示表的某些列-每个DataGridView中的不同列.如果更改DataTable的内容,则更改将反映在所有DataGridView中.

我的麻烦是我无法弄清楚三件事:

1. DataTable的更改是否会在所有关联的DataGridView上反映为相似的更改?

2.当内容来自DataTable时,如何过滤出DataGridView的某些列?它只能通过使用DataView.ToTable()来实现吗,因为它会创建表的副本;因此,使用更多的内存?

3.是否可以将每个DataGridView中的最后一列设置为隐藏?

Hi, in the process of designing and implementing a software system, I have been running into various performance issues. As a result, I am trying to figure ways to minimize memory usage and increase reading and writing performance. I stumbled upon the possibilities of using a DataTable for storing and handling parts of my database; however, I am not quite familiar with it, and its association with DataGridView, so I am asking for some pointers in this area.

What I want to do is to have a single large DataTable. I want to display certain columns of the table in a DataGridView - different columns in each DataGridView. If I change the contents of the DataTable, the changes will be reflected back in all the DataGridView.

My trouble is that I can''t figure out three things:

1. Will a change on DataTable be reflected as similar change on all associated DataGridView?

2. How do I filter out certain columns of DataGridView when the contents are coming from DataTable? Is it doable only with using DataView.ToTable() because it creates a copy of the table; thus, using more memory?

3. Can I set the last column as hidden in each of the DataGridView?

推荐答案

如果将数据表绑定到datagridview作为数据源,则对数据表所做的更改也会发生在datagridview上.我不知道是否有一个简短的方法,但是您可以编写一种方法来过滤数据表的特定字段.请检查
If you bind a datatable to datagridview as datasource, the changes you made on datatable also occurs on datagridview. I don''t know if there is a short way, but you can code a method in order to filter a specific field of datatable. Please check
DataRow.Field(string name)

方法.


是的,控件通过databind方法(回发,或使用ajax进行手动调用)后,数据表中的任何更改都将反映在DataGridView的(DGV)上.

我看到的最简单的过滤列的方法是将AutoGenerateColumns设置为false,然后在DGV的Columns容器中创建所需的列.您仍然可以使用绑定字段,只需手动定义它们.如果需要动态代码,也可以从代码隐藏中进行.

然后,您可以完全控制可见的内容和不可见的内容.您所要做的只是在列条目上设置可见"选项.另外,如果要使用内置的编辑/删除"按钮,则需要将DataKeyNames设置为保存行ID条目的列名.
Yes, any change within the datatable will be reflected on the DataGridView''s(DGV) once the control goes through a databind method (postback, or manual call if using ajax).

The easiest way that I''ve seen to filter the columns is to set the AutoGenerateColumns to false and then create the columns you want within the Columns container of the DGV.You can still use bound fields, you will just have to define them manually. You can also do this from codebehind if you need it to be dynamic.

You then have total control over what''s visible and what''s not. All you have to do is set the Visible option on the column entry. Also, if you will be using the built in edit/delete buttons you will need to set the DataKeyNames to the column name that holds the row''s ID entry.


这篇关于如何过滤每个来自DataTable的DataGridView中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆