簡體   English   中英

使用自定義數據源時如何隱藏 DataGridView 的列?

[英]How to hide column of DataGridView when using custom DataSource?

我在 C# 中有一個小應用程序,它有一個 DataGridView,它使用以下方法填充:

grid.DataSource = MyDatasource array;

MyClass 保存列的結構,它看起來像這樣:

class MyDatasource
{
    private string column1;        
    private string column2;

    public MyDatasource(string arg1, string arg2)
    {
        this.column1 = arg1;
        this.column2 = arg2;
    }

    public string column1
    {
        get
        {
            return this.column1;
        }
        set
        {
            this.column1 = value;
        }
    }

    public string column2
    {
        get
        {
            return this.column2;
        }
        set
        {
            this.column1 = value;
        }
    }
}

一切正常,DataGridView 填充了正確的數據,但現在我想隱藏 column2。 我嘗試在列聲明上方添加[Browsable(false)] ,這將隱藏它,但我還需要從代碼中訪問列值,當我使用[Browsable(false)]並嘗試讀取它的內容時,它的行為就像如果該列不存在。 如果我不使用它,我可以毫無問題地讀取該列,但它在 DataGridView 中可見。

如何隱藏該列但仍然能夠從代碼中讀取其內容?

在某些情況下,首先將列添加到 DataGridView 然后將其隱藏可能不是一個好主意。

例如,我有一個類,該類具有公司徽標的 Image 屬性的 NHibernate 代理。 如果我訪問該屬性(例如,通過調用其 ToString 方法在 DataGridView 中顯示該屬性),它將從 SQL 服務器下載圖像。 如果我有一個 Company 對象列表並將其用作 DataGridView 的數據源,那么(我懷疑)它會在我可以隱藏該列之前下載所有徽標。

為了防止這種情況,我使用了自定義屬性

 [System.ComponentModel.Browsable(false)]

在圖像屬性上,以便 DataGridView 忽略該屬性(不創建列並且不調用 ToString 方法)。

 public class Company
 {
     ...
     [System.ComponentModel.Browsable(false)]
     virtual public MyImageClass Logo { get; set;}

您必須在網格視圖控件而不是數據源中隱藏該列。 將它隱藏在數據源中,它根本不會呈現到網格視圖中,因此您將無法訪問網格視圖中的值。 按照您建議的方式進行操作,您必須通過數據源而不是網格視圖訪問列值。

要隱藏網格視圖控件上的列,您可以使用如下代碼:

dataGridView1.Columns[0].Visible = false;

要從數據源訪問列,您可以嘗試以下操作:

object colValue = ((DataTable)dataGridView.DataSource).Rows[dataSetIndex]["ColumnName"];

我注意到,如果以編程方式使用,如果在panel1.Controls.Add(dataGridView);之前使用,它會呈現不完整(整個表單根本不會“繪制”任何東西panel1.Controls.Add(dataGridView); 然后dataGridView.Columns["ID"].Visible = false; 將打破整個表格並使其空白,因此要在 EG 之后設置此設置:

 panel1.Controls.Add(dataGridView);
 dataGridView.Columns["ID"].Visible = false; 
 //works

 dataGridView.Columns["ID"].Visible = false; 
 panel1.Controls.Add(dataGridView);
 //fails miserably

我不確定是否為時已晚,但問題是,如果您在運行時綁定,則無法在設計模式下設置列。因此,如果您在運行時綁定,請繼續從設計模式中刪除列,然后務實地做

前任..

     if (dt.Rows.Count > 0)
    {
        dataGridViewProjects.DataSource = dt;
        dataGridViewProjects.Columns["Title"].Width = 300;
        dataGridViewProjects.Columns["ID"].Visible = false;
    }

設置該特定列的Visible屬性 = false

dataGridView[ColumnName or Index].Visible = false;

編輯抱歉錯過了Columns屬性dataGridView.Columns[ColumnName or Index].Visible = false;

我有同樣的問題

這是可能適合您的解決方案。 它對我有用

    GridView1.DataBind();
if (GridView1.Columns.Count > 0)
    GridView1.Columns[0].Visible = false;
else
{
    GridView1.HeaderRow.Cells[0].Visible = false;
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        gvr.Cells[0].Visible = false;
    }
}

只需設置DataGridView.AutoGenerateColumns = false ;

您需要單擊右上角的箭頭(在datagridview )以添加列,並且在DataPropertyName您需要將您的屬性名稱放入您的類中。

然后,在datagridview定義列后,您可以設置datagridview.datasource = myClassViewModel

MyDataGridView.RowHeadersVisible = False; 在綁定和重命名每列標題並設置列寬之前。 為了在我搜索時幫助我失敗的記憶,因為我會搜索......這是肯定的;-)

如果要使用 BrowsableAttribute,則可以在運行時在模型上查找它並相應地隱藏該列:

private void Form_Load(object sender, EventArgs e)
{
    //add this line after your DataGridView initialization
    HideColumns<MyModel>(myDvg);
}

private void HideColumns<T>(DataGridView dvg)
{
    var type = typeof(T);
    foreach (var column in dvg.Columns.Cast<DataGridViewColumn>())
        column.Visible = IsBrowsable(type.GetProperty(column.Name));
}

private bool IsBrowsable(PropertyInfo propertyInfo)
{
    var attribute = propertyInfo.GetCustomAttributes(true).FirstOrDefault(att => att.GetType() == typeof(BrowsableAttribute));
    return attribute == null || (attribute as BrowsableAttribute).Browsable;
}

嘗試遵循:

DataGridView1.Columns["YourColumnName"].Visible = false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM