none
Execute and update the expression from one column to another column in data table C# RRS feed

  • Question

  • hi,

    I have to update one column based on the expression on the other column in the data table.

    Col1             Col2                  Col3                   ColExp                                                                CalcCol

    1                   0                        1                      Col1>Col2            

    1                   1                       1                      Col1+Col2 

    1                  2                       1                      Col1+Col2 +Col3

    Actually, I have to read the each row in the above data table and update the value on the calcCol column based on the ColExp column value. So finally my data table would be like this below table.

    

    Col1             Col2                  Col3                   ColExp                       CalcCol

    1                   0                        1                      Col1>Col2                   True

    1                   1                       1                      Col1+Col2                       2

    1                  2                       1                      Col1+Col2+Col3               4


    Thanks

    Arivazhagan K





    Tuesday, August 27, 2019 12:23 PM

Answers

All replies

  • Hi Arivazhagan K, 

    Thank you for posting here.

    According to your description, you can set the ColExp column value pattern string of Regex class, then you can calculate CalcCol column value by using Regex.IsMatch() method.

    Here’s my code.

            DataTable dt = new DataTable();
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                dt.Columns.Add("Col1");
                dt.Columns.Add("Col2");
                dt.Columns.Add("Col3");
                dt.Columns.Add("ColExp");
                dt.Columns.Add("CalcCol");
                dt.Rows.Add("1","1","1", "11.", "");
                dt.Rows.Add("1", "0", "1","1.0","");
                dataGridView1.DataSource = dt;
            }
            private void Button1_Click(object sender, EventArgs e)
            {
                for (int i=0;i<dt.Rows.Count;i++)
                {
                    string str = dt.Rows[i][0].ToString() + dt.Rows[i][1] + dt.Rows[i][2];
                    if (Regex.IsMatch(str, dt.Rows[i][3].ToString()))
                    {
                        dt.Rows[i][4] = "Yes";
                    }
                    else
                    {
                        dt.Rows[i][4] = "No";
                    }
                }
            }

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 28, 2019 3:17 AM
    Moderator
  • In the CalExp column contains any type of expression like below

    If(Cal1>Cal2) CalcCol= "Ok"

    If(Cal1>Cal2) CalcCol= "No"

    Cal1+Cal3  

    whatever expression coming under CalcExp Column, I have to evaluate in and update the result on CalcCol column. Everything should be done in C# code itself.

    Thanks

    Wednesday, August 28, 2019 4:51 AM
  • Hi Arivazhagan K,

    Thanks for your feedback.

    I have some questions to confirm with you before I make a test.

    How many Columns do you have? Are there only three columns?

    What's type of expression in ColExp column? Are there only basic operations?

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 30, 2019 9:51 AM
    Moderator
  • Actually I am reading the excel into data table so the column are very based on the excel I am going to import. I have to evaluate the expression available in one column and update the result in another column. Normally logical expression would be there in the expression column, for examble

    col1 > col2 && col3 >col4

    col1 < col2 || col3< col4

    col1 < col2

    Saturday, August 31, 2019 10:13 AM
  • Hi Arivazhagan K.,

    I regret that C# cannot analyze logical expression in the expression column without uniform format.

    The following refernce shows how to convert string value to operator, but the format in it is uniform.

    Convert string value to operator in C#

    You can write related logic expressions in the code to update the result.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, September 2, 2019 9:16 AM
    Moderator
  • Finally below post was used to achieve this

    https://www.west-wind.com/presentations/DynamicCode/DynamicCode.htm

    • Marked as answer by Arivazhagan K Thursday, September 12, 2019 7:53 AM
    Thursday, September 12, 2019 7:53 AM