Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

dataset expression

Posted on 2014-03-01
3
561 Views
Last Modified: 2014-03-02
i want defin function by visual studio 2010 to make some thing and call this function in dataset by DataColumn proprieties  ---> expression : myfunction ()
dataset
0
Comment
Question by:mahmuod
  • 2
3 Comments
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39897220
Not possible. The expression parser behind the Expression property is simple and not extensible. Making arbitrary function calls is not one of its capabilities. There are several ways to work around this, especially ones that don't require an expensive reflection lookup. Consider the DataTable.RowChanged event for example.
0
 

Author Comment

by:mahmuod
ID: 39897593
Thank u
But how to make same  by anther way
For example
I want datacolumn to calculate datediff between to date i have function but i can insert it to datacolumn
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 500 total points
ID: 39898855
Here is sample code for that:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private DataGridView dataGridView1;
        private Button button1;
        private DataTable dataTable = null;
        public Form1()
        {
            InitializeComponent();
            InitializeDataTable();
        }

        private void InitializeDataTable()
        {
            dataTable = new DataTable();
            DataColumn colFromDate = new DataColumn("FromDate", typeof(DateTime));
            DataColumn colToDate = new DataColumn("ToDate", typeof(DateTime));
            DataColumn colDateDiff = new DataColumn("DateDiff", typeof(string));
            dataTable.Columns.AddRange(new DataColumn[] { colFromDate, colToDate, colDateDiff });
            dataTable.RowChanged += dataTable_RowChanged;

            for (int i = 0; i < 10; i++)
            {
                DateTime fromDate=DateTime.Now.AddYears(i);
                DateTime toDate= fromDate.AddDays(i);
                dataTable.Rows.Add(fromDate, toDate);
            }
        }


        void dataTable_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Add)
            {
                e.Row["DateDiff"] = String.Format("{0} Days", Convert.ToDateTime(e.Row["ToDate"]) - Convert.ToDateTime(e.Row["FromDate"]));
            }

        }

        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(25, 12);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(240, 150);
            this.dataGridView1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(34, 189);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = dataTable;
        }
    }
}

Open in new window

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question