2012-09-12 28 views
0

我有一個GridView綁定到ObjectDataSoure。GridView(綁定到ObjectDataSource)在字段對話框中的選定字段區域中不顯示字段

ObjectDataSource使用一個業務對象,該對象調用返回DataTable和查詢結果的DAL方法。

應用工程在運行時,但在設計時有一個問題: 當我使用GridView的任務嚮導 - >編輯列:沒有字段出現在選定字段的文本區域(這樣我就可以」使用嚮導來編輯列)。

DAL方法:

public override DataTable GetAllWorkers() 
    { 
     using (SqlConnection con = new SqlConnection(this.ConnectionString)) { 
      SqlCommand cmd = new SqlCommand("t_Workers_GetAllWorkers", con); 
      cmd.CommandType = CommandType.StoredProcedure; 
      con.Open(); 
      return GetTable(cmd); 
     } 

    } 

    private DataTable GetTable(SqlCommand cmd) 
    { 

     SqlDataAdapter adp = new SqlDataAdapter(); 
     adp.SelectCommand = cmd; 
     DataTable dt = new DataTable(); 
     adp.Fill(dt); 
     return dt; 

    } 

BLL方法:

public static DataTable GetAllWorkers() 
    { 
     return DataProvider.Instance().GetAllWorkers(); 
    } 

ASPX網頁:

 <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
     SelectMethod="GetAllWorkers" TypeName="BLL.BizImpl"></asp:ObjectDataSource> 

    <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" 
     EnableModelValidation="True" Height="156px" Width="270px"> 
    </asp:GridView> 

可有人請解釋什麼是該問題的原因是什麼?

Thanks 

回答

1

看來,因爲您沒有在aspx Page中明確聲明您的列,您將無法在嚮導中看到它們。目前您的GridView已填充數據,因爲

AutoGenerateColumns 

屬性默認爲true。您可以將其設爲false並自己定義列。然後,您可以更好地控制要在其中顯示的內容等。

+0

謝謝您的回答。當我嘗試使用SqlDataSource時,所有列都出現在設計時的選定字段文本區域中。那麼當我使用ObjectDataSoure的時候不喜歡乳清?在ASPX文件中手動定義列是我唯一的選擇嗎? – ProgNet

+0

爲什麼不使用SqlDataSource呢? – chead23

+0

說實話,我不太清楚爲什麼它能與一個而不是另一個協同工作。我會嘗試手動定義列。如果你能夠在SqlDataSource中完成而不需要定義列,那麼它在哪裏存儲格式化信息,你注意到它是否在aspx中? – chead23

相關問題