如何使用ObjectDataSource在代码中正确设置数据表到gridview?

时间:2008-11-06 16:24:47

标签: data-binding datatable objectdatasource

我在网页上有一个ID为ObjectDataSource1的ObjectDataSource。我还有一个gridview,我将ObjectDataSource.ID绑定到GridView.DataSourceID。我得到的问题是在文本框中更改文本时,代码调用BrokerageTransactions.GetAllWithDt返回DataTable。我想将此数据表设置为GridView的DataSource,但它告诉我,我无法将DataSouce和DataSourceId设置在一起。我怎样才能解决这个问题?代码如下。也。为什么在使用ObjectDataSource时无法设置DataSourceID和DataSource?

谢谢, X

protected void BrokerageChange(Object sender, EventArgs e)
{
    BrokerageTransactions brokerageaccountdetails = 
                          new BrokerageTransactions();

    DataSet ds = BrokerageAccount.GetBrkID2(new 
                 Guid(Membership.GetUser().ProviderUserKey.ToString()), 
                 ddlBrokerageDetails.SelectedItem.Text.ToString());

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        brokerageaccountdetails.BrokerageId = 
                                new Guid(dr["BrkrgId"].ToString());
    }

    ddlBrokerageDetails.SelectedItem.Value = 
                        brokerageaccountdetails.BrokerageId.ToString();

    if (txtTransactionsTo.Text != "" 
        && txtTransactionsFrom.Text != "")
        ObjectDataSource1.FilterExpression = 
        "convert(CreateDt,System.DateTime)>Convert('" + 
         Convert.ToDateTime(txtTransactionsFrom.Text) + "',System.DateTime) 
         and Convert(CreateDt,System.DateTime)<convert('"
         + Convert.ToDateTime(txtTransactionsTo.Text.ToString()) + 
         "',System.DateTime)";
    else if (txtTransactionsFrom.Text != "")
            ObjectDataSource1.FilterExpression =
            "convert(CreateDt,System.DateTime)>convert('" +
             Convert.ToDateTime(txtTransactionsFrom.Text) + 
            "',System.DateTime)";
    else if (txtTransactionsTo.Text != "")
            ObjectDataSource1.FilterExpression = 
            "convert(CreateDt,System.DateTime)
            <convert('" 
            + Convert.ToDateTime(txtTransactionsTo.Text.ToString()) +  
            "',System.DateTime)";
    else
        ObjectDataSource1.FilterExpression = " ";

    grvBrokerage.DataSourceID = ObjectDataSource1.ID;
    grvBrokerage.DataBind();

    DateTime dtTransFrom = Convert.ToDateTime("1/1/1900");
    DateTime dtTransTo = System.DateTime.Today;

    //TransactionsTo Box is Empty
    if ((txtTransactionsFrom.Text.Length > 2) 
    && (txtTransactionsTo.Text.Length < 2)) 
    {
        dtTransFrom = Convert.ToDateTime(txtTransactionsFrom.Text);
        dtTransTo = System.DateTime.Today;
    }

    //TransactionsFrom Box is Empty
    if ((txtTransactionsFrom.Text.Length < 2) 
    && (txtTransactionsTo.Text.Length > 2))
    {
       dtTransFrom = Convert.ToDateTime("1/1/1900");
       dtTransTo = Convert.ToDateTime(txtTransactionsTo.Text);
    }

    //TransactionsFrom Box and TransactionsTo Box is Not Empty
    if ((txtTransactionsFrom.Text.Length > 2) 
    && (txtTransactionsTo.Text.Length > 2))
    {
        dtTransFrom = Convert.ToDateTime(txtTransactionsFrom.Text);
        dtTransTo = Convert.ToDateTime(txtTransactionsTo.Text);
    }
    // Fails Here
    grvBrokerage.DataSource = 
    BrokerageTransactions.GetAllWithDt(brokerageaccountdetails.BrokerageId, 
                                       dtTransFrom, 
                                       dtTransTo);
    grvBrokerage.DataBind();  }

1 个答案:

答案 0 :(得分:0)

你有两个选择:

  • 不要使用ObjectDataSource 所有。您只需使用DataSource 属性和语法设置 每一次。
  • 您使用GridView的DataSourceID属性和Add 2 plain asp:参数并设置默认值。在ObjectDataSource_Selecting事件上,您可以通过e.InputParameters []
  • 设置这些参数

希望这有帮助。