none
Dataset does not update RRS feed

  • Question

  •  

    I am trying to edit a particular row in a datatable:

    string expression = "Name = '" + LeaveNameSelected + "'";
     DataRow[] selectRow = leaveHolDs.Tables[0].Select(expression);
                
     selectRow[0]["NumberOfDays"] = int.Parse(LeaveDaysTxtBox.Text);//Line 1
     selectRow[0]["Name"] = LeaveNameTxtBox.Text; //Line2

    When I run this code, I only can edit the number of days, as in only the changes in Number of days can be seen but not the name. But when I swapped the order of Line 1 and Line 2, I only can edit the name but not the number of days. Why is it so? Datasets do not allow pple to edit multiple fields from a datarow??




                             
    Thursday, May 23, 2013 8:07 AM

Answers

  • Here is a way:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable table = new DataTable("MyTable");
                table.Columns.Add("Name", typeof(String));
                table.Columns.Add("NumberOfDays", typeof(Int32));
    
                //Add rows
                table.Rows.Add("John Grove", 15);
                table.Rows.Add("Nava Tan", 31);
                table.Rows.Add("Bill Johnson", 3);
                table.Rows.Add("Pamela Anderson", 1);
    
                //Update row
                DataRow row = table.AsEnumerable()
                    .Where(i => i.Field<String>("Name") == "Nava Tan")
                    .Select(i => i)
                    .First();
                row.SetField<String>("Name", "Nava IS Tan");
                row.SetField<Int32>("NumberOfDays", 21);
    
                foreach (DataRow dr in table.Rows)            
                    Console.WriteLine("{0}, {1}", dr[0], dr[1]);            
                Console.ReadLine();
            }
        }
    }
    


    Thursday, May 23, 2013 5:26 PM
  • Try using an EndEdit(), like this:

    selectRow[0]["NumberOfDays"] = int.Parse(LeaveDaysTxtBox.Text);//Line 1 selectRow[0]["Name"] = LeaveNameTxtBox.Text; //Line2

    selectRow[0].EndEdit();

    Friday, May 24, 2013 1:11 AM
  • Wow, something weird is happening on this forum. The previous reply about using an .EndEdit() was posted by me, but it's not showing up as me. Let's hope this one correctly posts as being from me.

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Friday, May 24, 2013 1:16 AM
  • In my perception no problem at all, probably you made a mistake somewhere else.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dataTable = new DataTable();
                DataColumn NumberOfDays = new DataColumn("NumberOfDays");
                DataColumn Name = new DataColumn("Name");
                dataTable.Columns.Add(NumberOfDays);
                dataTable.Columns.Add(Name);
                dataTable.Rows.Add(dataTable.NewRow());
                dataTable.Rows[0]["Name"] = "Cor";
                string expression = "Name = '" + "Cor" + "'";
                DataRow[] selectRow = dataTable.Select(expression);
    
                selectRow[0]["NumberOfDays"] = 1;
                selectRow[0]["Name"] = "Bonnie";
                Console.WriteLine(dataTable.Rows[0]["Name"]);
                Console.ReadKey();
            }
        }
    }


    Success
    Cor

    Friday, May 24, 2013 4:10 PM

All replies

  • Here is a way:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable table = new DataTable("MyTable");
                table.Columns.Add("Name", typeof(String));
                table.Columns.Add("NumberOfDays", typeof(Int32));
    
                //Add rows
                table.Rows.Add("John Grove", 15);
                table.Rows.Add("Nava Tan", 31);
                table.Rows.Add("Bill Johnson", 3);
                table.Rows.Add("Pamela Anderson", 1);
    
                //Update row
                DataRow row = table.AsEnumerable()
                    .Where(i => i.Field<String>("Name") == "Nava Tan")
                    .Select(i => i)
                    .First();
                row.SetField<String>("Name", "Nava IS Tan");
                row.SetField<Int32>("NumberOfDays", 21);
    
                foreach (DataRow dr in table.Rows)            
                    Console.WriteLine("{0}, {1}", dr[0], dr[1]);            
                Console.ReadLine();
            }
        }
    }
    


    Thursday, May 23, 2013 5:26 PM
  • Wow, something weird is happening on this forum. The previous reply about using an .EndEdit() was posted by me, but it's not showing up as me. Let's hope this one correctly posts as being from me.

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Friday, May 24, 2013 1:16 AM
  • Wow, something weird is happening on this forum. The previous reply about using an .EndEdit() was posted by me, but it's not showing up as me. Let's hope this one correctly posts as being from me.

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    not only this one.

    Success
    Cor

    Friday, May 24, 2013 3:55 PM
  • In my perception no problem at all, probably you made a mistake somewhere else.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dataTable = new DataTable();
                DataColumn NumberOfDays = new DataColumn("NumberOfDays");
                DataColumn Name = new DataColumn("Name");
                dataTable.Columns.Add(NumberOfDays);
                dataTable.Columns.Add(Name);
                dataTable.Rows.Add(dataTable.NewRow());
                dataTable.Rows[0]["Name"] = "Cor";
                string expression = "Name = '" + "Cor" + "'";
                DataRow[] selectRow = dataTable.Select(expression);
    
                selectRow[0]["NumberOfDays"] = 1;
                selectRow[0]["Name"] = "Bonnie";
                Console.WriteLine(dataTable.Rows[0]["Name"]);
                Console.ReadKey();
            }
        }
    }


    Success
    Cor

    Friday, May 24, 2013 4:10 PM