locked
add column value to specific row in datatable RRS feed

  • Question

  • hey i am trying to add new column value into existing row of datatable at specific position.

    my datatable has column like

    sr.no. | sampledetail  | parameter1  |parameter2 |parameter3

    1       |ABC               |  xyz              |                 |

    2       |XYZ                |                    | 12             |

    Now third time when i am adding third row which has sample detail as "ABC" and value should be added to parameter3 column like following

    1       |ABC                |xyz               |                 | 10

    but i got result like this

    2       | XYZ               |                     |12             |10

    In this sampledetail is my primary key.

    I want data should add at specific row.

    any one can please help for this.

     

     

    Wednesday, April 27, 2011 7:34 AM

Answers

  • try doing

     

    using(DataSet dSet = new DataSet)
    {
    using(DataTable dTable = dSet.Tables[0])
    {
    // you can replace the value using this
    dTable.Rows[0][0] = "Hello";
    }
    }
    

    Note: I haven't try my code yet. Specify the index of your row you want to truncate.

    Hope that it helps.


    Failure is not the worst thing in the world. The very worst is not to try.
    • Marked as answer by Aspen VJ Friday, May 6, 2011 1:39 AM
    Thursday, April 28, 2011 8:20 AM
  • You need to have some comparision to which row you would like to insert new value (this is like WHERE in the sql). So you have to rely only on two columns; or on Serial Number, or on SamoleDetail. But I would rather rely on Serial number, which is a primary key column of the dataTable. So you somehow have to get the INDEX (or Serial number) or the row you want to do a new insertion.

    Can you tell me how do you know, you wanto to insert value "10" to the row at index 0, with serial number 1?

     

    If you can answer on this question, from then on its simple, you only do:

    //table is an instance of DataTable:
    table.Rows[index]["parameter3"] = 10; //if column is type of integer
    //or:
    table.Rows[index]["parameter3"] = "10"; //if column is type of string

     


    Mitja
    • Marked as answer by Aspen VJ Friday, May 6, 2011 1:39 AM
    Thursday, April 28, 2011 9:38 AM

All replies

  • The code you're using to update the row is wrong. You're updating the wrong row. That's all can be said without seeing your code.
    Wednesday, April 27, 2011 9:33 AM
  • When you make changes to column values in a DataRow, the changes are immediately placed in the current state of the row. The DataRowState is then set to Modified, and the changes are accepted or rejected using the AcceptChanges or RejectChanges methods of the DataRow. The DataRow also provides three methods that you can use to suspend the state of the row while you are editing it. These methods are BeginEdit, EndEdit, and CancelEdit.

    When you modify column values in a DataRow directly, the DataRow manages the column values using the Current, Default, and Original row versions. In addition to these row versions, the BeginEdit, EndEdit, and CancelEdit methods use a fourth row version: Proposed. For more information about row versions, see Row States and Row Versions.

    The Proposed row version exists during an edit operation that begins by calling BeginEdit and that ends either by using EndEdit or CancelEdit, or by calling AcceptChanges or RejectChanges.

    During the edit operation, you can apply validation logic to individual columns by evaluating the ProposedValue in the ColumnChanged event of the DataTable. The ColumnChanged event holds DataColumnChangeEventArgs that keep a reference to the column that is changing and to the ProposedValue. After you evaluate the proposed value, you can either modify it or cancel the edit. When the edit is ended, the row moves out of the Proposed state.

    You can confirm edits by calling EndEdit, or you can cancel them by calling CancelEdit. Note that while EndEdit does confirm your edits, the DataSet does not actually accept the changes until AcceptChanges is called. Note also that if you call AcceptChanges before you have ended the edit with EndEdit or CancelEdit, the edit is ended and the Proposed row values are accepted for both the Current and Original row versions. In the same manner, calling RejectChanges ends the edit and discards the Current and Proposed row versions. Calling EndEdit or CancelEdit after calling AcceptChanges or RejectChanges has no effect because the edit has already ended.

    The following example demonstrates how to use BeginEdit with EndEdit and CancelEdit. The example also checks the ProposedValue in the ColumnChanged event and decides whether to cancel the edit.

    Visual Basic  CopyCode imageCopy Code
    Dim workTable As DataTable = New DataTable
    workTable.Columns.Add("LastName", Type.GetType("System.String"))
    
    AddHandler workTable.ColumnChanged, _
     New DataColumnChangeEventHandler(AddressOf OnColumnChanged)
    
    Dim workRow As DataRow = workTable.NewRow()
    workRow(0) = "Smith"
    workTable.Rows.Add(workRow)
    
    workRow.BeginEdit()
    ' Causes the ColumnChanged event to write a message and cancel the edit.
    workRow(0) = ""   
    workRow.EndEdit()
    
    ' Displays "Smith, New".
    Console.WriteLine("{0}, {1}", workRow(0), workRow.RowState) 
    
    
    Private Shared Sub OnColumnChanged( _
     sender As Object, args As DataColumnChangeEventArgs)
     If args.Column.ColumnName = "LastName" Then
      If args.ProposedValue.ToString() = "" Then
       Console.WriteLine("Last Name cannot be blank. Edit canceled.")
       args.Row.CancelEdit()
      End If
     End If
    End Sub
    
    C#  CopyCode imageCopy Code
    DataTable workTable = new DataTable();
    workTable.Columns.Add("LastName", typeof(String));
    
    workTable.ColumnChanged += 
     new DataColumnChangeEventHandler(OnColumnChanged);
    
    DataRow workRow = workTable.NewRow();
    workRow[0] = "Smith";
    workTable.Rows.Add(workRow);
    
    workRow.BeginEdit();
    // Causes the ColumnChanged event to write a message and cancel the edit.
    workRow[0] = "";   
    workRow.EndEdit();
    
    // Displays "Smith, New".
    Console.WriteLine("{0}, {1}", workRow[0], workRow.RowState); 
    
    protected static void OnColumnChanged(
     Object sender, DataColumnChangeEventArgs args)
    {
     if (args.Column.ColumnName == "LastName")
      if (args.ProposedValue.ToString() == "")
      {
       Console.WriteLine("Last Name cannot be blank. Edit canceled.");
       args.Row.CancelEdit();
      }
    }

     

    Regards,

    Narendran Ponpandiyan

    Thursday, April 28, 2011 5:31 AM
  • try doing

     

    using(DataSet dSet = new DataSet)
    {
    using(DataTable dTable = dSet.Tables[0])
    {
    // you can replace the value using this
    dTable.Rows[0][0] = "Hello";
    }
    }
    

    Note: I haven't try my code yet. Specify the index of your row you want to truncate.

    Hope that it helps.


    Failure is not the worst thing in the world. The very worst is not to try.
    • Marked as answer by Aspen VJ Friday, May 6, 2011 1:39 AM
    Thursday, April 28, 2011 8:20 AM
  • accoridng to your problem.. you whabt to update parameter field by applying condition on sampledetail column.. am i right? if right

    >> You have to select a row from your data table where sample detail == "something like ABC". Now we will select a row where sampledetail = abc

    foreach (DataRow dr in dt.Rows)
       {
        if (dr[0].Text == "ABC") // here we have select a row where sampledetail = "abc"
        {
         //Now Update you column value
         dr[3] = "your new value"; // here 3 is column index
        }
       }
    

     

    you also update your table value by using

    dt.Rows[rowindex][column index] = "your value goes here";

    hope it will help you.

     

     

     

     

     

     


    Hasibul Haque, MCPD http://blog.e-rains.com
    Thursday, April 28, 2011 9:25 AM
  • You need to have some comparision to which row you would like to insert new value (this is like WHERE in the sql). So you have to rely only on two columns; or on Serial Number, or on SamoleDetail. But I would rather rely on Serial number, which is a primary key column of the dataTable. So you somehow have to get the INDEX (or Serial number) or the row you want to do a new insertion.

    Can you tell me how do you know, you wanto to insert value "10" to the row at index 0, with serial number 1?

     

    If you can answer on this question, from then on its simple, you only do:

    //table is an instance of DataTable:
    table.Rows[index]["parameter3"] = 10; //if column is type of integer
    //or:
    table.Rows[index]["parameter3"] = "10"; //if column is type of string

     


    Mitja
    • Marked as answer by Aspen VJ Friday, May 6, 2011 1:39 AM
    Thursday, April 28, 2011 9:38 AM