Nasıl yapılır: Windows Forms DataGrid Denetiminde Sütunları Silme veya Gizleme
Not
Denetim, denetimin yerini alan ve denetime işlevsellik ekler; ancak, siz seçerseniz denetim hem geriye dönük uyumluluk hem de gelecekteki DataGridViewDataGrid kullanım için DataGrid korunur. Daha fazla bilgi için bkz. Windows Forms DataGridView ve DataGrid Denetimleri Arasındaki Farklar.
ve nesnelerinin özelliklerini ve yöntemlerini (sınıfın üyeleri olan) kullanarak Windows Forms denetiminde sütunları program aracılığıyla silebilir DataGridGridColumnStylesCollection veya DataGridColumnStyleDataGridTableStyle gizleyebilirsiniz.
Kılavuzun bağlı olduğu veri kaynağında silinmiş veya gizli sütunlar hala mevcuttur ve yine de program aracılığıyla erişilebilir. Bunlar artık datagrid'de görünmez.
Not
Uygulamanız belirli veri sütunlarına erişene ve veri kılavuzunda görüntülenebilir olmasını istemiyorsanız, en başta bunları veri kaynağına eklemek büyük olasılıkla gerekli değildir.
DataGrid'den program aracılığıyla sütun silmek için
Formun bildirim alanında sınıfının yeni bir örneğini DataGridTableStyle bildirin.
özelliğini, DataGridTableStyle.MappingName veri kaynağınıza stili uygulamak istediğiniz tablo olarak ayarlayın. Aşağıdaki örnek, DataGrid.DataMember zaten ayarlanmış olduğunu varsayan özelliğini kullanır.
Yeni nesneyi DataGridTableStyle datagrid'in tablo stilleri koleksiyonuna ekleyin.
Silinecek RemoveAtDataGrid sütunun sütun GridColumnStyles dizinini belirterek koleksiyonunun yöntemini çağırma.
' 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
Formun bildirim alanında sınıfının yeni bir örneğini DataGridTableStyle bildirin.
özelliğini, MappingName veri DataGridTableStyle kaynağınıza stili uygulamak istediğiniz tablo olarak ayarlayın. Aşağıdaki kod örneği, DataGrid.DataMember zaten ayarlanmış olduğunu varsayan özelliğini kullanır.
Yeni nesneyi DataGridTableStyle datagrid'in tablo stilleri koleksiyonuna ekleyin.
Gizlenen sütunun sütun
Widthdizinini belirterek özelliğini 0 olarak ayarerek sütunu gizleyebilirsiniz.' 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; }