Link to home
Start Free TrialLog in
Avatar of ANAT2403
ANAT2403Flag for Israel

asked on

can't get modified record in ADO.net 2.0 with getchanges

I have an application in winforms C# VS2005 ADO.NET
I have a datagridview bound to a dataset.
I fill the dataset with one datatable.
I allow the datagridview to update and change one cell.
I expect that when I ask for GetChanges(DataRowState.Modified) on the dataset or on the datatable
to get a true value but I don't.
I try to create a dataview with RowStateFilter = DataViewRowState.ModifiedOriginal - nothing. I get 0.
why can't I get the modified records?
Thankyou
Anat
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

You can try to call DataGridView.EndEdit, and see if that helps.

Bob
Avatar of ANAT2403

ASKER

I am changing the datatable myself in the dataGridView1_CellEndEdit.
Avatar of Gautham Janardhan
Gautham Janardhan

s i think u need to DataGridView.EndEdit or move the control away from the datagri view before calling getchanges. i had the same problem and what i did was before calling the Dataset.getchages i took the focus away from the gird by fousing on a button in the form. so that the underlying datasource would get updated.
<datatable myself in the dataGridView1_CellEndEdit>

can u post the code

how are u chaging are u directly changing the datatable ...
this isthe code:
I have a schema of datarable without dataadapter

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Windows.Forms;

namespace testDSChanges
{
    public partial class Form2 : Form
    {
        DataSet1 CDA;
        private string SqlString = ConfigurationManager.ConnectionStrings["testDSChanges.Properties.Settings.ContentConnectionString"].ToString();
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
             CDA = new DataSet1();
             ManufacturerListFill();
             dataGridView1.DataSource = CDA.Manufacturers;
        }
        private void ManufacturerListFill()
        {
            {
                SqlCommand Cmd;

                SqlConnection Conn = new SqlConnection(SqlString);
                SqlDataAdapter DA = new SqlDataAdapter();

                // Set up select command
                Cmd = new SqlCommand("ManufacturerListGet", Conn);
                Cmd.CommandType = CommandType.StoredProcedure;

                Conn.Open();
                SqlDataReader DR = null;
                DR = Cmd.ExecuteReader();
                DataRow rowReader;
                string Name;
                while (DR.Read())
                {
                    rowReader = CDA.Manufacturers.NewRow();
                    rowReader["ManufacturerId"] = DR.GetInt32(0);   //read ManufacturerId
                    Name = DR.GetString(1);     //read Name
                    rowReader["Manufacturer"] = Name;
                    CDA.Manufacturers.Rows.Add(rowReader);
                }

                Conn.Close();

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {

            DataSet dschanges;
             DataView dvchanged;

             DataTable dtchange1 = CDA.Manufacturers.GetChanges();
             DataTable dtchange2 = CDA.Manufacturers.GetChanges(DataRowState.Modified);


        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string ChangedValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            string keyIDStr = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            DataRow dr;
            dr = CDA.Manufacturers.Rows.Find(keyIDStr);
            dr.BeginEdit();
            dr["Manufacturer"] = ChangedValue;
            dr.EndEdit();
        }

 
    }
}
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are great!
 Everything you wrote is exactly what happend. It is the acceptchanges in the beginning after filling the datatable.
Thankyou very much
Anat