How to properly set a datatable to an gridview in code with an ObjectDataSource?
Posted
by Xaisoft
on Stack Overflow
See other posts from Stack Overflow
or by Xaisoft
Published on 2008-11-06T16:24:47Z
Indexed on
2010/04/03
17:03 UTC
Read the original article
Hit count: 335
Hello,
I have an ObjectDataSource with an ID of ObjectDataSource1 on a webpage. I also have a gridview in which I am binding the ObjectDataSource.ID to the GridView.DataSourceID. The problem I get is when text is changed in a textbox, the code calls BrokerageTransactions.GetAllWithDt which returns a DataTable. I want to set this datatable as the DataSource for the GridView, but it is telling me that I can't set the DataSouce and DataSourceId together. How can I fix this? Code is below. Also. Why can't you set a DataSourceID and a DataSource when using an ObjectDataSource?
Thanks, 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(); }
© Stack Overflow or respective owner