views:

232

answers:

1

Howdy

using c#, vs2008 winforms

I have a datagridview that i am programtically binding to a binding source, that is bound to a dataset with 1 table.

After filling the dataset using the sqlAdaptor, i want to add a new column to the dataset, and in the new column populate it with a result derived from a call to a custom method in the form.

Code Eg is bellow but i get a "Expression contains undefined function call this.Test()

Is it allowed to call methods as such in the expression of the column

thanks in advance for any help cheers

this.dsProdOrdLst1.ProdOrder.Columns.Add("sOrderType", typeof(string), 
    "this.Test(order_type)"); // order_type is another column in the dataset

elsewhere in the form class is the method

public int Test(int orderType)
{
    return 10; //return a test value
}
A: 

AFAIK, these expressions (known as ADO.NET expressions) have no way of calling any kind of user defined functions. It should however by trivial to manually populate the new column:

this.dsProdOrdLst1.ProdOrder.Columns.Add("sOrderType", typeof(string));

foreach (DataRow row in dsProdOrdLst1.ProdOrder.Rows)
{
    row["sOrderType"] = Test((int) row["order_type"]);
}

Alternatively, you might be able to implement your entire function in an ADO.NET expression, but you really only want to do this for very simple functions, as otherwise it will be a mess:

IIF(order_type = 0, 'Mail', IIF(order_type = 1, 'Phone', 'Online'))

The documentation of DataColumn.Expression has a list of supported functions (scroll pretty far down to find it).

Jørn Schou-Rode
Yeah, i wanted to avoid that by doing it in expressions i thought it would be faster as it would execute on each row, rather than having to iterate through the whole dataset. Well nevermind if its not possible its not possible
Spooky2010