2012-11-05 48 views
4

我想寫一個將datagridview的數據源轉換爲列表的通用函數(然後我想將一個空對象添加到列表中,數據源可以是任何類型)?將Datagridview的數據源轉換爲列表?

我有些絕望的嘗試是這樣的:

Type datagrid_type = dgv.DataSource.GetType(); 
List<object> the_source = dgv.DataSource as List<object>; 
Convert.ChangeType(the_source, datagrid_type); 
Object c = Activator.CreateInstance(datagrid_type); 
Convert.ChangeType(c, datagrid_type); 
the_source.Add(c); 

..但the_source只是空。即使它不是它仍然可能無法工作。我相信你們會有一個更聰明的方式(實際上可行的方法)來實現這一目標。

p.s.我正在使用EF創建一個數據源列表,因此將數據源強制轉換爲DataTable可能與此處不相關

+0

您應該添加C#標記。 –

+1

我想知道爲什麼這個問題沒有足夠的重視。感謝@ Overflow012,我只是用正確的類別重新標記了這個問題。希望有人能提供出色的解決方案 – Esen

回答

2

沒有人在這裏回答我,所以我最終將類型傳遞給函數,儘管如果我會更快樂我會避免讓該函數來確定正確的類型

public static void Add_Empty_Row_At_To_Grid<T>(DataGridView dgv) 
    { 
    List<T> the_source = dgv.DataSource as List<T>; 
    T c = (T)Activator.CreateInstance(typeof(T)); 
    the_source.Add(c); 
    } 
3

我做到了以下列方式(工作,只有當你使用時綁定的數據源)

創建自定義DataGridView控件和繼承的DataGridView

public partial class MyGridView : DataGridView 

聲明持有列名,summaryvalue和格式的類中顯示

[Serializable] 
    public class SummaryDataProperty 
    { 
     public string ColumnName { get; set; } 
     public string Format { get; set; } 
     internal decimal Value { get; set; } 
    } 

彙總數據財產申報清單在MyDataGridView

public List<SummaryDataProperty> SummaryDataPropertyNames { get; set; } 

數據綁定完成後,計算總結和在列標題處顯示它。

protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e) 
    { 
     base.OnDataBindingComplete(e); 
     if (SummaryDataPropertyNames.Count > 0) 
     { 
      if (DataSource is BindingSource) 
      { 
       var ds = (DataSource as BindingSource); 
       foreach (var prop in SummaryDataPropertyNames) 
       { 
        prop.Value = 0; 
        var col = this.Columns[prop.ColumnName]; 
        foreach (var l in ds.List) 
        { 
         decimal val; 
         if (decimal.TryParse(Convert.ToString(l.GetType().GetProperty(col.DataPropertyName).GetValue(l, null)), out val)) 
          prop.Value += val; 
        } 
        col.HeaderText = col.HeaderText.Split('[')[0].TrimEnd(' ') + " [" + prop.Value.ToString(prop.Format) + "]"; 
       } 

      } 
     } 
    } 

由於綁定數據源提供了數據源中對象的列表。它很容易在綁定源列表中循環。我不知道如何用對象數據源或BindingDatasource.Current做到這一點。我仍在尋找解決方案。

1

如果你想有一個泛型函數/方法,並傳遞diferent對象的列表,你可以做到這一點(在我的應用程序中提取爲例):

public void SaveAll<T>(IEnumerable<T> objects) 
{ 
    foreach (object obj in objects) 
    { 
     T specificObject = (T)obj; 
     Session.Save(specificObject); 
    } 
    Session.Flush(); 
} 

所以,你可以調用此方法與任何類對象:

List<Product> products = Product.GetAll(); 
SaveAll<Product>(products); 

List<Vendor> vendors = Vendor.GetAll(); 
SaveAll<Vendor>(vendors); 

etc 

另一方面手,如果你有一個DataGridView,你想添加行,你可以使用一個BindingSourceDataGridView's DataSource。例如:

... 
private BindingSource Source; 
... 

private void LoadProducts(List<Product> products) 
{ 
     Source.DataSource = products; 
     ProductsDataGrid.DataSource = Source; 
} 

private void addProductBtn_Click(object sender, EventArgs e) 
{ 
     Source.Add(new Product()); 
}