Custom method call from datagridview column expression

using c #, vs2008 winforms

I have a datagridview that I programmatically binds to a binding source bound to a dataset with 1 table.

After filling the data set with sqlAdaptor, I want to add a new column to the data set, and in the new column I want to fill it with the result obtained from calling the custom method in the form.

The Eg code is below, but I get the expression "Expression contains undefined this.Test ()

Is it allowed to call methods as such in a column expression

well in advance for any help greetings

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 a method

 public int Test(int orderType) { return 10; //return a test value } 
+4
source share
1 answer

AFAIK, these expressions (known as ADO.NET expressions ) have no way to call any user-defined functions. However, however, it is trivial to manually populate a 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 can implement all of your function in an ADO.NET expression, but you really want to do this only for very simple functions, because otherwise it will be a mess:

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

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

0
source

Source: https://habr.com/ru/post/1302102/


All Articles