Solved

dataset expression

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now