Aracılığıyla paylaş


Nasıl yapılır: Windows Forms DataGrid Denetiminde Sütunları Silme veya Gizleme

Dekont

Denetim DataGridView , denetimin DataGrid yerini alır ve denetime işlevsellik ekler; ancak DataGrid isterseniz denetim hem geriye dönük uyumluluk hem de gelecekteki kullanım için korunur. Daha fazla bilgi için bkz . Windows Forms DataGridView ile DataGrid Denetimleri Arasındaki Farklar.

ve DataGridColumnStyle nesnelerinin (sınıfın üyeleri DataGridTableStyle olan) özelliklerini ve yöntemlerini kullanarak Windows Forms DataGrid denetimindeki GridColumnStylesCollection sütunları program aracılığıyla silebilir veya gizleyebilirsiniz.

Silinen veya gizli sütunlar, kılavuzun bağlı olduğu veri kaynağında hala bulunur ve yine de program aracılığıyla erişilebilir. Bunlar artık datagrid'de görünmez.

Dekont

Uygulamanız belirli veri sütunlarına erişmiyorsa ve bunların datagrid'de görüntülenmesini istemiyorsanız, bunları ilk etapta veri kaynağına eklemek büyük olasılıkla gerekli değildir.

DataGrid'den bir sütunu program aracılığıyla silmek için

  1. Formunuzun bildirimler alanında sınıfının yeni bir örneğini DataGridTableStyle bildirin.

  2. DataGridTableStyle.MappingName özelliğini, stili uygulamak istediğiniz veri kaynağınızdaki tabloya ayarlayın. Aşağıdaki örnek, zaten ayarlandığını varsaydığı özelliğini kullanır DataGrid.DataMember .

  3. Yeni DataGridTableStyle nesneyi datagrid'in tablo stilleri koleksiyonuna ekleyin.

  4. RemoveAt Silinecek sütunun DataGridsütun dizinini belirterek 's GridColumnStyles koleksiyonunun yöntemini çağırın.

    ' Declare a new DataGridTableStyle in the  
    ' declarations area of your form.  
    Dim ts As DataGridTableStyle = New DataGridTableStyle()  
    
    Sub DeleteColumn()  
       ' Set the DataGridTableStyle.MappingName property  
       ' to the table in the data source to map to.  
       ts.MappingName = DataGrid1.DataMember  
    
       ' Add it to the datagrid's TableStyles collection  
       DataGrid1.TableStyles.Add(ts)  
    
       ' Delete the first column (index 0)  
       DataGrid1.TableStyles(0).GridColumnStyles.RemoveAt(0)  
    End Sub  
    
    // Declare a new DataGridTableStyle in the  
    // declarations area of your form.  
    DataGridTableStyle ts = new DataGridTableStyle();  
    
    private void deleteColumn()  
    {  
       // Set the DataGridTableStyle.MappingName property  
       // to the table in the data source to map to.  
       ts.MappingName = dataGrid1.DataMember;  
    
       // Add it to the datagrid's TableStyles collection  
       dataGrid1.TableStyles.Add(ts);  
    
       // Delete the first column (index 0)  
       dataGrid1.TableStyles[0].GridColumnStyles.RemoveAt(0);  
    }  
    

DataGrid'de bir sütunu program aracılığıyla gizlemek için

  1. Formunuzun bildirimler alanında sınıfının yeni bir örneğini DataGridTableStyle bildirin.

  2. MappingName özelliğiniDataGridTableStyle, stili uygulamak istediğiniz veri kaynağınızdaki tabloya ayarlayın. Aşağıdaki kod örneği, zaten ayarlandığını varsaydığı özelliğini kullanır DataGrid.DataMember .

  3. Yeni DataGridTableStyle nesneyi datagrid'in tablo stilleri koleksiyonuna ekleyin.

  4. Özelliğini 0 olarak ayarlayarak Width , gizlenecek sütunun sütun dizinini belirterek sütunu gizleyin.

    ' Declare a new DataGridTableStyle in the  
    ' declarations area of your form.  
    Dim ts As DataGridTableStyle = New DataGridTableStyle()  
    
    Sub HideColumn()  
       ' Set the DataGridTableStyle.MappingName property  
       ' to the table in the data source to map to.  
       ts.MappingName = DataGrid1.DataMember  
    
       ' Add it to the datagrid's TableStyles collection  
       DataGrid1.TableStyles.Add(ts)  
    
       ' Hide the first column (index 0)  
       DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0  
    End Sub  
    
    // Declare a new DataGridTableStyle in the  
    // declarations area of your form.  
    DataGridTableStyle ts = new DataGridTableStyle();  
    
    private void hideColumn()  
    {  
       // Set the DataGridTableStyle.MappingName property  
       // to the table in the data source to map to.  
       ts.MappingName = dataGrid1.DataMember;  
    
       // Add it to the datagrid's TableStyles collection  
       dataGrid1.TableStyles.Add(ts);  
    
       // Hide the first column (index 0)  
       dataGrid1.TableStyles[0].GridColumnStyles[0].Width = 0;  
    }  
    

Ayrıca bkz.