Solved

dataset expression

Posted on 2014-03-01
3
555 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL SELECT query help 7 41
CPU high usage when update statistics 2 30
SQL view 2 27
Rename a column in the output 3 14
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

778 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