none
Update a value to entire rows in a column in dataset without looping RRS feed

  • Question

  • hi,

    I have to update a column in a set to a value like true or false. Like I have to update all rows in 'flag' column in the dataset to true.like following

     

    id      flag

    1       true

    2       true

    3       true

     

    I achieved the above concept using a for loop using the row count of the data table. But it takes long time if there is large data. So I wonder that is there any other way to change all the row's value of a particular column in a data table to a value without using loop.

     

    Thanks in advance,

    Elango

    Tuesday, April 27, 2010 6:52 AM

Answers

  • I think if there is another way, which still allows persistence of your data, it will internally also perform some sort of a loop which results in low performance when a large set of data ist available. Do you only set this value in your loop, I'm wondering how much data you must have that this takes a long time.
    Mattia Baldinger http://www.code-in.net/
    • Marked as answer by Harry Zhu Wednesday, May 5, 2010 6:26 AM
    Tuesday, April 27, 2010 7:16 AM

All replies

  • I think if there is another way, which still allows persistence of your data, it will internally also perform some sort of a loop which results in low performance when a large set of data ist available. Do you only set this value in your loop, I'm wondering how much data you must have that this takes a long time.
    Mattia Baldinger http://www.code-in.net/
    • Marked as answer by Harry Zhu Wednesday, May 5, 2010 6:26 AM
    Tuesday, April 27, 2010 7:16 AM
  • Hi,

    What is the purpose of updating this column, mening are going to update this value back to the database OR is it just used to caclulate or format the display etc..?

    Answer to the above question would let you  decide the right approach., either to use expression fields, or updating in the back end etc.



    Please do not forgot to mark it as an answer if it is. Thanks Gags
    Tuesday, April 27, 2010 8:13 AM
  • Hi Gags,

    See I've 2 grids. A and B

    A will have data which contains "IsSelected = false" and B will have "IsSelected = true"

    When the user presses a button, then IsSelected for entire rows in a column must be assigned to true.. This should happen for me without using the loops. The following is the coding which im currently using,

    for (int i = 0; i < Global.DataSet.DataTable.Rows.Count; i++)
                    {
                        // Making all the values to False, since the Exclusion Sites will have the IsSelected = true
                        Global.DataSet.DataTable.Rows[i]["IsSelected"] = true;
                    }

    I like to achieve the concept in the above coding without using the loop.

    Something Like This...

    Global.dstAdmin.GetFacilityExclusionFilter.IsselectedColumn.DefaultValue = True;

     

    Thanks in advance,

    Elango

     

     

    Tuesday, April 27, 2010 9:12 AM
  • you can try Linq but that also internally used for loop



    Please do not forgot to mark it as an answer if it is. Thanks Gags
    Tuesday, April 27, 2010 9:32 AM
  • will the data, the boolean field, be stored in the database? Can the boolean field be update otherwise or only with this special selection button?
    Mattia Baldinger http://www.code-in.net/
    Tuesday, April 27, 2010 9:57 AM
  • Hi Mattia,

    The boolean field is used only for the design only. Its not stored in the database. and its not updated in the database. Actually there is no column called 'flag' in the database. Its for design purpose only.

     

    Thanks in advance,

    Elango

    Tuesday, April 27, 2010 10:37 AM
  • in this case you could use an expression. When the button is hit, you set the expression property of your flag column to true. 

     

          DataTable dt = new DataTable();
          DataColumn dc = new DataColumn("flag", typeof(bool)); 
          dc.Expression = "true"; 
          dt.Columns.Add(dc);


    Mattia Baldinger http://www.code-in.net/
    Tuesday, April 27, 2010 11:36 AM
  • Hi Mattia,

    this what I exactly need but when i do the same thing in reverse, its throwing the error as:

    Column 'IsPass' is read only

    I tried by giving the read only = false for the column but still it is throwing the same error.

     

    See This is what I gave:

     private void MoveAllRight_Click(object sender, EventArgs e)
     {
       Global.dstClass.TableStudent.IsPass.Expression = "false";                 
     }

     

    private void MoveAllLeft_Click(object sender, EventArgs e)
     {
       Global.dstClass.TableStudent.IsPass.Expression = "true";                 
     }

     

    After performing any one of the action which is defined above, if i perform the below action

     private void btnSingleLeft_Click(object sender, EventArgs e)

     {

        for (int count = dgvExcludeSites.SelectedRows.Count - 1; count >= 0; count--)
       {
          Global.dstClass.TableStudent.IsPass.ReadOnly = false;
        }

    }

    Its throwing the Error, Column "IsPass" is readonly

    Even after giving this also, I couldn't resolve the problem

     private void btnSingleLeft_Click(object sender, EventArgs e)
    {

        for (int count = dgvExcludeSites.SelectedRows.Count - 1; count >= 0; count--)
        {
         Global.dstClass.TableStudent.IsPass.ReadOnly = false;
         dgvStudDet.SelectedRows[count].Cells["IsPass"].Value = false;
        }

    }

     

    Please help me..

    Again I'm requesting that i should not use any loop for doing this...

     

    Thanks in advance

    Elango


    Mark As Answer or Vote As Helpful if My Reply Does.
    Wednesday, April 28, 2010 5:33 AM
  • Hi,

    As far as I can see, there is no such property. You might need to loop all the rows and update them.

    How do you know it's taking long time to finish the work? do you mean the datagridview does not refresh quickly? I suggest you update the dataset in another thread and then show the change on the datagridview, just the first page at first.

    Could you please upload a simplified project or some code that can help us to reproduce the problem?

    Harry 


    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, May 3, 2010 3:28 AM
  • I know this thread is getting a bit old, but for people like me who stumble across it looking for answers, I found the following useful...

    This will only be of use if you want to update all rows, I am using a DataTable named dt, you could just use MyDataSet.Tables[0].etc instead.

    First, delete the column. Then, add it again with the 'expression' parameter set to whatever you want the data in the column to contain.

     

    dt.Columns.Remove(dt.Columns["flag"
    
    ]);
    
    dt.Columns.Add("flag"
    
    , Type.GetType("System.Boolean"
    
    ), "true"
    
    );
    

     

    Note that if you were using a text field instead of a boolean field, like I was, you would need to put quotes around your string i.e.

     

    dt.Columns.Add("flag"
    
    , Type.GetType("System.String"
    
    ), "'your text here'"
    
    );
    

     

    That's it from me, hopefully this will help someone and I have posted in the appropriate spot!!

     

    • Proposed as answer by Jack.Reacher Tuesday, December 14, 2010 12:27 AM
    • Edited by Jack.Reacher Tuesday, December 14, 2010 12:30 AM readability
    Tuesday, December 14, 2010 12:23 AM
  • I know this thread is getting a bit old, but for people like me who stumble across it looking for answers, I found the following useful...

    This will only be of use if you want to update all rows, I am using a DataTable named dt, you could just use MyDataSet.Tables[0].etc instead.

    First, delete the column. Then, add it again with the 'expression' parameter set to whatever you want the data in the column to contain.

     

    dt.Columns.Remove(dt.Columns["flag"
    ]);
    
    dt.Columns.Add("flag"
    , Type.GetType("System.Boolean"
    ), "true"
    );
    

     

    Note that if you were using a text field instead of a boolean field, like I was, you would need to put quotes around your string i.e.

     

    dt.Columns.Add("flag"
    , Type.GetType("System.String"
    ), "'your text here'"
    );
    

     

    That's it from me, hopefully this will help someone and I have posted in the appropriate spot!!

     

    • Proposed as answer by Jack.Reacher Tuesday, December 14, 2010 12:33 AM
    Tuesday, December 14, 2010 12:32 AM