locked
Update DataTable Column

    Question

  • Good morning.

        I am trying to update the columns in a DataTable, the problem is that he bring the value to 0 when it should be 1,

         I think the problem is in the update here, because if I comment these two lines

          tabela_booleana.Rows[index_linha][index_coluna] = 0;
                                                tabela_booleana.Rows[index_linha][0] = id;

     it assumes the values to zero while the other values to 1 correctly.
     
        What could be causing this error.


     tabela_booleana.Rows.Add(id);
                                
                            for (i = 0; i < comp; i++)
                            {
                                linha = "";
                                caracter = Convert.ToChar(input.Substring(i, 1));
                                if (caracter == ',')
                                {

                                    pos = i;
                                    item = Convert.ToString(input.Substring(pos_anterior, pos - pos_anterior));
                                    pos_anterior = pos + 1;
                                    foreach (DataColumn dc in tabela_booleana.Columns)
                                    {
                                        if (item == "25")
                                        {
                                            MessageBox.Show("ok");
                                        }
                                       
                                        index_coluna = tabela_booleana.Columns.IndexOf(dc);
                                        index_linha = tabela_booleana.Rows.Count - 1;

                                        if (dc.ColumnName == item)
                                        {
                                            tabela_booleana.Rows[index_linha][index_coluna] = 1;
                                           
                                        }
                                        else
                                            {
                                                index_linha = tabela_booleana.Rows.Count-1;
                                                tabela_booleana.Rows[index_linha][index_coluna] = 0;
                                                tabela_booleana.Rows[index_linha][0] = id;
                                            }

                                    }
                                   


                                }

    Friday, April 17, 2009 11:51 AM

Answers

  • Hi Ricardo M.S.,

    Please check the following sample code. Correct me if I misunderstand you. Thank you.
            private void Form1_Load(object sender, EventArgs e)
            {
                fillData();
            }
    
            private void fillData()
            {
                DataTable dt = new DataTable();
    
                dt.Columns.Add("LineID", typeof(int));
                dt.Columns.Add("A", typeof(int));
                dt.Columns.Add("B", typeof(int));
                dt.Columns.Add("C", typeof(int));
                dt.Columns.Add("D", typeof(int));
    
                StreamReader sr = new StreamReader(@"c:\ficheiro.txt");
                string readDate;
                string[] sTemp;
                DataRow dr;
                int i = 1;
                while ((readDate = sr.ReadLine()) != null)
                {
                    dr = dt.NewRow();
                    dr["LineID"] = i;
                    dr["A"] = 0;
                    dr["B"] = 0;
                    dr["C"] = 0;
                    dr["D"] = 0;
    
                    sTemp = readDate.Split(',');
                    foreach (string  s1 in sTemp)
                    {
                        switch (s1)
                        { 
                            case "A":
                                dr["A"] = 1;
                                break;
                            case "B":
                                dr["B"] = 1;
                                break;
                            case "C":
                                dr["C"] = 1;
                                break;
                            case "D":
                                dr["D"] = 1;
                                break;
                            default :
                                break;
                        }
                    }
    
                    dt.Rows.Add(dr);
    
                    i++;
                }
    
                dataGridView1.DataSource = dt;
            }

    My test data:
    data in ficheiro.txt:
    A,B,C
    C,D
    A,C

    output in DataGridVie:
    LineID  A  B  C  D
    1       1  1  1  0
    2       0  0  1  1
    3       1  0  1  0


    Best regards,
    Guo
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Ricardo M.S. _ Wednesday, April 22, 2009 10:52 PM
    Wednesday, April 22, 2009 5:50 PM

All replies

  • From where the value of item is coming from

    item = Convert.ToString(input.Substring(pos_anterior, pos - pos_anterior));
    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Friday, April 17, 2009 12:09 PM
  • Its not going in the IF-Loop. The condition dc.ColumnName == item is not being satisfied.

    Are you getting your casing right while doing a comparison? Its better if you would convert both into UpperCase

    dc.ColumnName.ToUpper() == item.ToUpper()

    Ganesh Ranganathan
    [Please mark the post as answer if you find it helpful]
    Friday, April 17, 2009 12:31 PM
  • Hi Ricardo M.S.,

    Have you resolved your problem?
    If you still have problem, please have a try using Building and Debugging (Visual C#)  , Breakpoints and Tracepoints to test your program and see how it runs. Or please provide more sample code that can be debugged just as AmaL has mentioned. Thank you!

    Best regards,
    Guo
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, April 20, 2009 9:13 AM
  •                   The problem is here.
                                                 index_linha = tabela_booleana.Rows.Count-1;
                                                 tabela_booleana.Rows [index_linha] [index_coluna] = 0;
                                                 tabela_booleana.Rows [index_linha] [0] = id;

                               If I comment this lines, the values are to null, but if they are active replaces values that had been placed at 1 for 0. 
                               The idea is subtituir all values are null for zero, because if the item! = Dc.column should be 0.


    StreamReader sr = new StreamReader(ficheiro);

                        string input = null;
                        decimal id;
                        id = 1;
                        while ((input = sr.ReadLine()) != null)
                        {
                            string item;
                            int comp, pos_id = 0, pos_anterior = 0;
                            comp = input.Length;
                          
                            int index_coluna, index_linha;
                            pos_id = -1;
                            pos_anterior = pos_id + 1;
                            tabela_booleana.Rows.Add(id);
                            string[] conjunto_itens = input.Split(',');
                            foreach (string item_aux in conjunto_itens)
                            {
                                item = item_aux;
                                 foreach (DataColumn dc in tabela_booleana.Columns)
                                    {
                                        index_coluna = tabela_booleana.Columns.IndexOf(dc);
                                        index_linha = tabela_booleana.Rows.Count - 1;
                                        if (dc.ColumnName.ToUpper() == item.ToUpper())
                                        {
                                            tabela_booleana.Rows[index_linha][index_coluna] = 1;
                                        }
            else
         {
           tabela_booleana.Rows[index_linha][index_coluna] = 0;
         } 
                                        }
                                    }
                            }
                            id = id + 1;
                        }
                        sr.Close();
    Monday, April 20, 2009 9:47 AM
  • Hi Ricardo M.S.,

    I'm sorry that I still can't clearly understand your purpose.
                            if (dc.ColumnName.ToUpper() == item.ToUpper())
                            {
                                tabela_booleana.Rows[index_linha][index_coluna] = 1;
                            }
                            else
                            {
                                tabela_booleana.Rows[index_linha][index_coluna] = 0;
                            }
    About your sample code above, dc.ColumnName will be your column name, and item will be your values from ficheiro. So the two values will be unequal normally. So the value of tabela_booleana.Rows[index_linha][index_coluna] will be always 0.

    Please follow up with more necessary information about your specific purpose if you still have problem. Thank you!


    Best regards,
    Guo
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 21, 2009 9:38 AM


  • Hello Sufer

        The purpose is, I have a table with the following headers
        Id A B C  
         
         the file (ficheiro) has the following data
         1 A, C
         2 B
         3 A, B, C

         with the code above, is comment "//tabela_booleana.Rows[index_linha][index_coluna] = 0" 

          A B C id
          1 1 1
          2 1
          3 1 1 1

       The idea is
    instead of null to the value 0.
    Tuesday, April 21, 2009 10:51 AM
  • Hi Ricardo M.S.,

    Thanks for your response. But I'm very sorry that I still can't understand your main purpose. The only information I know now that you have a table with four columns(ID, A, B, C) and file named ficheiro with follow data:
    line1: A, C
    line2: B
    line3: A, B, C
    Is it right?

    What the following date as you mentioned? I don't know what the relation among these data.
          A B C id
          1 1 1
          2 1
          3 1 1 1

    Please provide more necessary information and correct me if I'm wrong. Thank you.


    Best regards,
    Guo


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 21, 2009 6:21 PM
  • Hello,


         The goal is to see if the products in the line of the file, if the product exists in the put 1 in column in the datatable with same name, if not bring the value zero.
          The header of the columns correspond to products (A, B, C),
    Tuesday, April 21, 2009 10:29 PM
  • Hi Ricardo M.S.,

    Thanks for your response. But I'm very sorry that I still can't understand your main purpose. Would you mind being more specific.
    1. How many columns in your datatable? What's the name of each column? What does each column mean?
    2. What's the type of your file ficheiro? (e.g. a .txt file?)  What's the content in it(each line)? And what do they represent for?
    3. Give an sample in your scenario. e.g. What's the data you have or input and what's the result or output do your want to get?
    Please follow up. Thank you!

    Best regards,
    Guo
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 22, 2009 5:56 AM
  • Hello, Guo SurferMSFTUsers MedalsUsers MedalsUsers MedalsUsers Medals

          First of all thank you for your help.

         The program must read a text file, each line has a series of products.
          Example:
          A, B, C
          C, D
        
         The first reading notes which products in the file. In the example above
         3 products (A, C, D). The products found will match the columns in a DataTable x.
        
         The second objective is through the data and verify that the product is present on the line.
      for example in line 1 the product is not present D:
         once the table
             Line_ID A B C D
              1         1 1 1 0
       
    for example in line 2 has only the products C, D, then the table will thus arise.
           Line_ID A B C D
            2         0 0 1 1
    Wednesday, April 22, 2009 8:32 AM
  • Hi Ricardo M.S.,

    Please check the following sample code. Correct me if I misunderstand you. Thank you.
            private void Form1_Load(object sender, EventArgs e)
            {
                fillData();
            }
    
            private void fillData()
            {
                DataTable dt = new DataTable();
    
                dt.Columns.Add("LineID", typeof(int));
                dt.Columns.Add("A", typeof(int));
                dt.Columns.Add("B", typeof(int));
                dt.Columns.Add("C", typeof(int));
                dt.Columns.Add("D", typeof(int));
    
                StreamReader sr = new StreamReader(@"c:\ficheiro.txt");
                string readDate;
                string[] sTemp;
                DataRow dr;
                int i = 1;
                while ((readDate = sr.ReadLine()) != null)
                {
                    dr = dt.NewRow();
                    dr["LineID"] = i;
                    dr["A"] = 0;
                    dr["B"] = 0;
                    dr["C"] = 0;
                    dr["D"] = 0;
    
                    sTemp = readDate.Split(',');
                    foreach (string  s1 in sTemp)
                    {
                        switch (s1)
                        { 
                            case "A":
                                dr["A"] = 1;
                                break;
                            case "B":
                                dr["B"] = 1;
                                break;
                            case "C":
                                dr["C"] = 1;
                                break;
                            case "D":
                                dr["D"] = 1;
                                break;
                            default :
                                break;
                        }
                    }
    
                    dt.Rows.Add(dr);
    
                    i++;
                }
    
                dataGridView1.DataSource = dt;
            }

    My test data:
    data in ficheiro.txt:
    A,B,C
    C,D
    A,C

    output in DataGridVie:
    LineID  A  B  C  D
    1       1  1  1  0
    2       0  0  1  1
    3       1  0  1  0


    Best regards,
    Guo
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Ricardo M.S. _ Wednesday, April 22, 2009 10:52 PM
    Wednesday, April 22, 2009 5:50 PM