65.9K
CodeProject 正在变化。 阅读更多。
Home

在运行时隐藏不打算显示的 DataGrid 列

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.22/5 (3投票s)

2007年11月3日

CPOL

2分钟阅读

viewsIcon

30097

downloadIcon

230

在运行时隐藏不打算显示的 DataGrid 列

引言

本文介绍如何在 DataGrid(在 Windows 窗体中)中隐藏特定的列(例如,ProductIDCategoryID),这些列不打算向最终用户显示。在本文中,我将专门介绍在使用相关数据时如何在 DataGrid 中隐藏列。

背景

在处理我的项目时,我需要在 DataGrid 中显示相关数据。 但我不想在父列和子列中显示 ID 列,为此我不得不进行一些研究,最后我找到了解决方案。 我在这里发布是为了帮助可能也需要做和我一样事情的其他人。

Using the Code

在提交的示例代码中,我使用了 Northwind 数据库。 从中,我使用了 CategoriesProducts 表来解释通过隐藏不需要的列来表示关系数据。

首先,使用以下代码块从数据库加载数据。

private void LoadData()
{ 
    DataTable dtCategories = new DataTable("Categories");
    DataTable dtProducts = new DataTable("Products");
    ds = new DataSet();
    using (SqlConnection connection = new SqlConnection
	("Server=localhost;Initial Catalog=Northwind;Integrated Security=True"))
    {
        using (SqlCommand command = new SqlCommand
	("Select CategoryID,CategoryName,Description from Categories", connection))
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                adapter.Fill(dtCategories);
                ds.Tables.Add(dtCategories);
            }
        } 
        using (SqlCommand command = new SqlCommand
	("select ProductID,CategoryID,ProductName,UnitPrice,
	UnitsInStock,UnitsOnOrder from Products;", connection))
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {  
                adapter.Fill(dtProducts);
                ds.Tables.Add(dtProducts); 
            } 
        }
    }
    //Now add the relations to the dataset.
    DataRelation dataRelationCategoryProduct = 
	new DataRelation("CategoryProduct",dtCategories.Columns["CategoryID"], 
	dtProducts.Columns["CategoryID"]);
        ds.Relations.Add(dataRelationCategoryProduct);
    dataGrid1.DataSource = ds;
    dataGrid1.DataMember = "Categories";
}  

在这里,我在运行时建立了 CategoriesProducts 表的关系,并将其添加到数据集的 Relations 集合中。

加载数据后,使用以下行来注册特定 datagridTableStyles.CollectionChanged 事件。

dataGrid1.TableStyles.CollectionChanged += 
	new CollectionChangeEventHandler(TableStyles_CollectionChanged);

完成此操作后,将表格样式添加到 datagrid,这将从 datagrid 中删除不需要的列。 使用以下代码执行此操作

private void AddTableStyles()
{
    if (!dataGrid1.TableStyles.Contains(tsCategory))
    {
        tsCategory = new DataGridTableStyle();
        tsCategory.MappingName = "Categories";
        dataGrid1.TableStyles.Add(tsCategory);
    }
    if (!dataGrid1.TableStyles.Contains(tsProduct))
    {
        tsProduct = new DataGridTableStyle();
        tsProduct.MappingName = "Products";
        dataGrid1.TableStyles.Add(tsProduct);
    }
} 

在这里,我为每个表添加了两个 DataGridTableStyles 对象。

现在实现主要逻辑,它隐藏 datagrid 中不需要的列。

private void TableStyles_CollectionChanged(object sender, CollectionChangeEventArgs e)
{
    if (dataGrid1.TableStyles.Contains(tsCategory))
   {
        if(dataGrid1.TableStyles["Categories"].GridColumnStyles
            .Contains(dataGrid1.TableStyles["Categories"].GridColumnStyles["CategoryID"]))
        {                 
            dataGrid1.TableStyles["Categories"].GridColumnStyles
            .Remove(dataGrid1.TableStyles["Categories"].GridColumnStyles["CategoryID"]);
        }                
    }
    if (dataGrid1.TableStyles.Contains(tsProduct))
    {
        if(dataGrid1.TableStyles["Products"].GridColumnStyles
            .Contains(dataGrid1.TableStyles["Products"].GridColumnStyles["ProductID"]))
        {
            dataGrid1.TableStyles["Products"].GridColumnStyles
            .Remove(dataGrid1.TableStyles["Products"].GridColumnStyles["ProductID"]);
        }
        if(dataGrid1.TableStyles["Products"].GridColumnStyles
            .Contains(dataGrid1.TableStyles["Products"].GridColumnStyles["CategoryID"]))
        {
            dataGrid1.TableStyles["Products"].GridColumnStyles
            .Remove(dataGrid1.TableStyles["Products"].GridColumnStyles["CategoryID"]);
        }
    }
} 

对于上面的代码块,首先我检查了 DataGridTableStyles 集合是否包含特定的 TableStyle。 如果我们不检查这一点,它会抛出一个异常。 上述代码的第一个 if 块将 TableStyle 应用于“Categories”表,以隐藏 datagrid 中的“CategoryID”列,第二个 if 块隐藏“Products”表中的“ProductID”和“CategoryID”。

还有一件事要做,就是你必须注册 DataGridNavigate 事件。 使用“属性”窗口执行此操作,或使用以下行注册该事件。

dataGrid1.Navigate += new NavigateEventHandler(dataGrid1_Navigate);

FormLoad 方法中编写以上行。

最后,编写以下代码行来响应 datagrid 的已注册 Navigate 事件。

private void dataGrid1_Navigate(object sender, NavigateEventArgs ne)
{
    /*On each navigation, call the TableStyles_CollectionChanged 
    method to apply our settings.*/
    TableStyles_CollectionChanged(null, null);            
} 

就这样!!!

现在你可以测试了....

datagrid 显示“Categories”表数据时,它将隐藏“CategoryID”列,当显示“Products”表数据时,它将隐藏“ProductID”和“CategoryID”列。

© . All rights reserved.