Click here to Skip to main content
15,886,689 members
Home / Discussions / C#
   

C#

 
QuestionHow to set restriction on a created group Pin
rune_norway15-Feb-09 22:31
rune_norway15-Feb-09 22:31 
QuestionClosing a User Control from itself Pin
caksabre215-Feb-09 22:29
caksabre215-Feb-09 22:29 
AnswerRe: Closing a User Control from itself Pin
Calin Tatar15-Feb-09 22:40
Calin Tatar15-Feb-09 22:40 
AnswerRe: Closing a User Control from itself Pin
ABitSmart15-Feb-09 22:42
ABitSmart15-Feb-09 22:42 
GeneralRe: Closing a User Control from itself Pin
caksabre215-Feb-09 23:14
caksabre215-Feb-09 23:14 
AnswerRe: Closing a User Control from itself Pin
kbalias15-Feb-09 22:51
kbalias15-Feb-09 22:51 
AnswerRe: Closing a User Control from itself Pin
Deresen15-Feb-09 22:54
Deresen15-Feb-09 22:54 
QuestionProblem with editing data via databound Textbox Pin
kbalias15-Feb-09 22:02
kbalias15-Feb-09 22:02 
Hi
I am having some problems with binding a TextBox to a data source. Actually the problem is in editing the data via the TextBox.

I am using Visual Studio 2005, C# and MS Access 2003 to develop a Windows Application.

On the Form I have the following controls:

DataGridView (name = dgv)
TextBox (name = txtWord)
BindingSource (name = bindingSource1)
DataSet (name = ds)
Button (name = btnSave)

I also have a class that I created to manage the data interaction. This class is called TestDataManager_MSAccess.
In this class I specify the table name of the relevant table in the database. (This is also the name that the DataTable will be called after the data loaded).
The class also has a load data method (TestDataManager_MSAccess.LoadTestData()) and a method to update the database (TestDataManager_MSAccess.UpdateDataTable()).

When the Form loads the data is loaded from the database and bound to the DataGridView via the BindingSource.

this.bindingSource1.DataSource = ds;
this.bindingSource1.DataMember = TestDataManager_MSAccess.TableName;

dgv.DataSource = bindingSource1;

I want the following to happen:

When the user double-clicks on a DataGridViewCell, the TextBox must be bound to the value in the DataGridViewRow. I bind the TextBox as follows:

private void dgv_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
    dgv.EndEdit();

    //when a cell is double clicked, the textbox is populated with the relevant value
    txtWord.DataBindings.Add("Text", this.bindingSource1, TestDataManager_MSAccess.Word_ColName);
}

This works ok and the relevant value in the DataGridViewCell is displayed in the TextBox.

But I also want to be able to edit the value via the TextBox, but it does not seem to work.

I look at the DataTable and even though the new value shows in the DataRow in the DataTable, the RowState still shows Unchanged. So when I call the Update method in my TestDataManager_MSAccess class the row will not update the database.


My code in the Form:

public Form_TxtBinding()
{
    InitializeComponent();
}



private void Form_TxtBinding_Load(object sender, EventArgs e)
{
    try
    {
        this.LoadDataController();
        this.DisplayData();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}



private void LoadDataController()
{
    try
    {
        if (ds == null)
        {
            ds = new DataSet();
        }

        this.LoadData();
    }
    catch (Exception e)
    {
        throw e;
    }
}



private void LoadData()
{
    try
    {
        string connectionString = Properties.Settings.Default.German_Genie_2ConnectionString;

        //load the required data into a DataTable in the DataSet
        TestDataManager_MSAccess.LoadTestData(connectionString, ds);
    }
    catch (Exception e)
    {
        throw e;
    }
}



private void DisplayData()
{
    try
    {
        if (ds == null)
        {
            return;
        }

        this.bindingSource1.DataSource = ds;
        this.bindingSource1.DataMember = TestDataManager_MSAccess.TableName;

        dgv.DataSource = bindingSource1;
    }
    catch (Exception e)
    {
        throw e;
    }
}



private void dgv_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
    dgv.EndEdit();

    //when a cell is double clicked, the textbox is populated with the relevant value
    txtWord.DataBindings.Add("Text", this.bindingSource1, TestDataManager_MSAccess.Word_ColName);
}




private void UpdateDatabase()
{
    try
    {
        if (ds.HasChanges() == true)
        {
            this.dgv.EndEdit();
            string connString = Properties.Settings.Default.German_Genie_2ConnectionString;

            //calls method in the Manager class that updates database from the DataTable
            TestDataManager_MSAccess.UpdateDataTable(connString, ds.Tables[TestDataManager_MSAccess.TableName]);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}



private void btnSave_Click(object sender, EventArgs e)
{
    try
    {
        this.UpdateDatabase();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}



private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == -1)    //RowHeaderCell clicked
    {
        dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
        dgv.EndEdit();
    }
    else if (e.RowIndex == -1)  //ColumnHeaderCell clicked
    {
        dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
        dgv.EndEdit();
    }
    else
    {
        //when any other cell clicked the EditMode is changed to EditOnEnter
        dgv.EditMode = DataGridViewEditMode.EditOnEnter;
    }
}


I can edit the data from within the DataGridView and update the database, but I would like to be able to edit the data in this way as well, because I find if you have a DataGridView with many DataGridViewComboBoxColumns and data formatting, the performance is very much affected. So for those types of situations I would like to be able to select the DataGridViewCell and the values from the row must be bound to other controls (like TextBoxes) and the user must be able to edit the values on those controls.

Am I missing something in the binding of the TextBox to a data source?

Thanks.

Kobus
QuestionThread Stop with Excel Object Pin
MumbleB15-Feb-09 21:54
MumbleB15-Feb-09 21:54 
AnswerRe: Thread Stop with Excel Object Pin
ABitSmart15-Feb-09 22:33
ABitSmart15-Feb-09 22:33 
QuestionGet location of Open With dialog result Pin
pedersen-roxen15-Feb-09 21:52
pedersen-roxen15-Feb-09 21:52 
QuestionMonitoring a database Pin
hadad15-Feb-09 21:16
hadad15-Feb-09 21:16 
AnswerRe: Monitoring a database Pin
Expert Coming15-Feb-09 21:55
Expert Coming15-Feb-09 21:55 
Questioncan not run OPENGL Program Pin
chandrapal15-Feb-09 21:02
chandrapal15-Feb-09 21:02 
AnswerRe: can not run OPENGL Program Pin
Expert Coming15-Feb-09 21:08
Expert Coming15-Feb-09 21:08 
AnswerRe: can not run OPENGL Program Pin
Nuri Ismail15-Feb-09 21:50
Nuri Ismail15-Feb-09 21:50 
Questiondifferent Behaviour of controls according to login Pin
AlokZanwar15-Feb-09 21:01
AlokZanwar15-Feb-09 21:01 
AnswerRe: different Behaviour of controls according to login Pin
Expert Coming15-Feb-09 21:09
Expert Coming15-Feb-09 21:09 
AnswerRe: different Behaviour of controls according to login Pin
Nuri Ismail15-Feb-09 21:43
Nuri Ismail15-Feb-09 21:43 
GeneralRe: different Behaviour of controls according to login Pin
Expert Coming15-Feb-09 21:57
Expert Coming15-Feb-09 21:57 
AnswerRe: different Behaviour of controls according to login Pin
MumbleB15-Feb-09 22:08
MumbleB15-Feb-09 22:08 
AnswerRe: different Behaviour of controls according to login Pin
arun_pk15-Feb-09 23:24
arun_pk15-Feb-09 23:24 
QuestionC# :: Updating SQL Table Form DataGridView Pin
WinSolution15-Feb-09 21:00
WinSolution15-Feb-09 21:00 
QuestionHow to show a selected treeview node' s attribute & value in listbox using C#? [modified] Pin
rasingh115-Feb-09 20:57
rasingh115-Feb-09 20:57 
QuestionHow to Debug C# Class Library that is invoked by an external Application? Pin
undecypherable15-Feb-09 19:45
undecypherable15-Feb-09 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.