2012-02-15 82 views
0

我想将存储的CustomDataGridViewRow添加到绑定的DataGridView中。就像如下:将行添加到绑定的datagridview

CustomDataGridViewRow rowTemplate = new CustomDataGridViewRow(); 
dataGridView1.RowTemplate = rowTemplate; 

Datenbank.cmd = 
    new SqlCommand("[Terminauswertung_Bericht_Gesamt]", Datenbank.connection); 
Datenbank.cmd.CommandType = CommandType.StoredProcedure; 
Datenbank.cmd.Parameters.AddWithValue("@berichtsnr", 1); 

SqlDataAdapter adapter = new SqlDataAdapter(Datenbank.cmd); 
dataSet1.Tables.Clear(); 
adapter.Fill(dataSet1, "Table"); 
bs = new BindingSource(); 
bs.DataSource = dataSet1.Tables["Table"]; 
dataGridView1.DataSource = bs; 

想到它会是这样的:

dataSet1.Tables[0].Rows.Add(Cache.getRow(1)); 

public class cache 
{ 
    Dictionary<int, CustomDataGridViewRow> _cache = 
     new Dictionary<int, CustomDataGridViewRow>(); 

    public CustomDataGridViewRow getRow(int index) 
    { 
     foreach (KeyValuePair<int, CustomDataGridViewRow> dic in _cache) 
     { 
      if (dic.Key == index) 
       return (dic.Value); 
     } 
     return (new CustomDataGridViewRow());    
    } 
} 

但它只能说明我的DataGridViewRow {指数= 1}在第一个单元格。

+1

你介意张贴您的解决方案作为一个回答你自己的问题? (并接受它)。这样做完全可以。 – 2012-02-15 22:47:38

+0

@GertArnold 好吧,我今后这样做,不知道是不是因为这个: “声誉低于100的用户在询问后8小时内无法回答自己的问题,您可以自己回答3小时,在此之前请使用评论,或者编辑你的问题。“ 生病3小时 – 2012-02-15 23:48:01

回答

0

解决它

DataRow newRow =test.Tables[0].NewRow(); 


newRow.ItemArray = Cache.getRowValues(child); 
test.Tables[0].Rows.InsertAt(newRow, c.Index+1); 

public string[] getRowValues(int index) 
     { 
      List<string> temp = new List<string>(); 
      foreach (KeyValuePair<int, CustomDataGridViewRow> dic in _cache) 
      { 
       if (dic.Key == index) 
       { 
        foreach (DataGridViewCell cell in dic.Value.Cells) 
         temp.Add(cell.Value.ToString()); 
       } 
      } 
      string[] result = temp.ToArray(); 

      return (result); 
     }