2012-01-02 21 views
0

Im在寫入if語句中的數據時遇到了問題,其中if(numRows> 0)代碼假設創建在網格的底部插入一行。它用文本框填充插入行,但它不顯示我的數據,所以我可能需要迭代數據源並將數據添加到我不知道如何的相應單元格中。所以如果有人知道什麼待辦事項,並能夠幫助,將不勝感激示例代碼將有所幫助。謝謝在自定義gridview控件中寫出每行的數據,並使用IEnumerable在底部添加插入行使用IEnumerable

public class CustomGridView : GridView 
{ 
    protected GridViewRow _footerRow2; 
    protected GridViewRow _headerRow2; 

    protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding) 
    { 
     // Call base method and get number of rows 
     int numRows = base.CreateChildControls(dataSource, dataBinding); 

     if (numRows == 0) 
     { 
      //no data rows created, create empty table 
      //create table 
      Table table = new Table(); 
      table.ID = this.ID; 

      //convert the exisiting columns into an array and initialize 
      DataControlField[] fields = new DataControlField[this.Columns.Count]; 
      this.Columns.CopyTo(fields, 0); 

      if (this.ShowHeader) 
      { 
       //create a new header row 
       _headerRow2 = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal); 

       this.InitializeRow(_headerRow2, fields); 
       _headerRow2.EnableTheming = true; 
       table.Rows.Add(_headerRow2); 
      } 

      GridViewRow row = base.CreateRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert); 
      this.InitializeRow(row, fields); 
      row.EnableTheming = true; 

      if (this.ShowFooter) 
      { 
       //create footer row 
       _footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal); 

       this.InitializeRow(_footerRow2, fields); 
       _footerRow2.EnableTheming = true; 
       table.Rows.Add(_footerRow2); 
      } 

      this.Controls.Clear(); 
      this.Controls.Add(table); 
     } 
     else if (numRows > 0) 
     { 
      // problem is in here how do i write out a collection 
      // of IEnumerable when I don't know what the collection type is 

      DataControlField[] fields = new DataControlField[this.Columns.Count]; 
      this.Columns.CopyTo(fields, 0); 

      Table table = new Table(); 
      table.ID = this.ID; 

      GridViewRow insertRow = base.CreateRow(numRows + 1, numRows + 1, DataControlRowType.DataRow, DataControlRowState.Insert); 
      this.InitializeRow(insertRow, fields); 
      insertRow.Cells[0].Controls.Add(new Button { Text = "Insert", CommandName = "Insert", ID = "Insert" }); 
      table.Rows.Add(insertRow); 

      this.Controls.Clear(); 
      this.Controls.Add(table); 
     } 

     return numRows; 
    } 
} 

回答

1

簡短的回答是,您可能需要使用反射來確定IEnumerable的類型。從dataSource.GetType()開始。

+0

我覺得這是超越我,我不知道是什麼待辦事項,如果能做到和編碼UPO,可以幫助我瞭解 – ONYX 2012-01-02 10:41:41

+0

有可能是一個更容易 辦法。你能解釋一下你爲什麼重寫GridView控件嗎? – RickNZ 2012-01-02 10:51:18

相關問題