Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > TextBox binding to other property
 

TextBox binding to other property

Hello,

I have extended the texbox controladding to it property called [Value].
I did created form contains datagridview and the extended textbox.

DataTable dt = new DataTable();

dt.Columns.Add("One");

dt.Columns.Add("Two");

dt.Columns.Add("Three");

dt.Rows.Add(new string[] { "1", "2", "3" });

dt.Rows.Add(new string[] { "4", "5", "6" });

dt.Rows.Add(new string[] { "7", "8", "9" });

dataGridView1.DataSource = dt;

dt.ColumnChanging += new DataColumnChangeEventHandler(dt_ColumnChanging);
extendedTextBox1.DataBindings.Add(
"Value", dt, "One"
);
extendedTextBox2.DataBindings.Add(
"Text", dt, "Two"
);
extendedTextBox3.DataBindings.Add(
"Text", dt, "Three"
);

When extendedTextBox2 focused and i move the tab to extendedTextBox3 the event ColumnChanging not raised, But when extendedTextBox1focused and i move the tab to extendedTextBox2 the event ColumnChanging raised.

This occurs because i set the extendedTextBox1.DataBindings to property [Value], why this occurs and what is the solution for this issue?

Thanks,
khalil

khalil salhi  Wednesday, July 22, 2009 10:09 AM

Hi khalil,

I tested the code and found that the value in the One column is being set, though the old value is equal to the new value. We can create our own DataTable to ignore this case. This is the code snippet:
public class MyDataTable : DataTable

{

protected override void OnColumnChanging(DataColumnChangeEventArgs e)

{

if (e.Row[e.Column] != e.ProposedValue)

//Raise the event only when the old value not equal to the new value.

base.OnColumnChanging(e);

else

//If equal, recover.

e.Row.RejectChanges();

}

}

Let me know if this helps.
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 23, 2009 12:27 PM
Hi khalil,
I tried as you said, found that its really not working. But when I implement the interface INotifyPropertyChanged, bingo..its working....

 public class ExtendedTextBox : TextBox,INotifyPropertyChanged
    {
        private string sVal;
        public string Value
        {
            get { return sVal; }
            set {
                if (sVal != value)
                {
                    sVal = value;
                    OnPropertyChanged("Value");
                }
                
            }
        }

    
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string properyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(properyName));
        }
        

        #endregion
    }

NareshG  Friday, July 24, 2009 9:34 PM
Hello,
Can you provide Code for property Value ?
NareshG  Wednesday, July 22, 2009 12:47 PM

public class ExtendedTextBox : TextBox
{
private string sVal;

public string Value
{
get { return sVal; }
set { sVal = value; }
}
}

khalil salhi  Wednesday, July 22, 2009 1:08 PM

Hello,
Anybody there ?

khalil salhi  Thursday, July 23, 2009 7:28 AM

Hi khalil,

I tested the code and found that the value in the One column is being set, though the old value is equal to the new value. We can create our own DataTable to ignore this case. This is the code snippet:
public class MyDataTable : DataTable

{

protected override void OnColumnChanging(DataColumnChangeEventArgs e)

{

if (e.Row[e.Column] != e.ProposedValue)

//Raise the event only when the old value not equal to the new value.

base.OnColumnChanging(e);

else

//If equal, recover.

e.Row.RejectChanges();

}

}

Let me know if this helps.
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 23, 2009 12:27 PM
Thnx,

Iknow this bypass, but i think thatit is not the solution because :

1. e.Row.RejectChanges(); reject the changesfor all the columns in the row.
2. I need to know why thisissue occur when i set the binding to[Value] property, what is the diff between [Text] property and value property?

khalil salhi  Thursday, July 23, 2009 1:12 PM
Thnx,

Iknow this bypass, but i think thatit is not the solution because :

1. e.Row.RejectChanges(); reject the changesfor all the columns in the row.
2. I need to know why thisissue occur when i set the binding to[Value] property, what is the diff between [Text] property and value property?


Another thing as you suggest this is bug in the datatable object or the of the databinding working with custom property,
i thought that this case is bug, what is your opinion about this case ?
khalil salhi  Thursday, July 23, 2009 7:36 PM
Thnx,

Iknow this bypass, but i think thatit is not the solution because :

1. e.Row.RejectChanges(); reject the changesfor all the columns in the row.
2. I need to know why thisissue occur when i set the binding to[Value] property, what is the diff between [Text] property and value property?


Another thing as you suggest this is bug in the datatable object or the wayof the databinding working with custom property,
i thought that this case is bug, what is your opinion about this case ?

khalil salhi  Thursday, July 23, 2009 7:36 PM
Hi Aland,

did you suggest another solution for this issue ?

Thnx,
khalil
khalil salhi  Friday, July 24, 2009 7:13 PM
Hi khalil,
I tried as you said, found that its really not working. But when I implement the interface INotifyPropertyChanged, bingo..its working....

 public class ExtendedTextBox : TextBox,INotifyPropertyChanged
    {
        private string sVal;
        public string Value
        {
            get { return sVal; }
            set {
                if (sVal != value)
                {
                    sVal = value;
                    OnPropertyChanged("Value");
                }
                
            }
        }

    
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string properyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(properyName));
        }
        

        #endregion
    }

NareshG  Friday, July 24, 2009 9:34 PM
Hi NareshG,

Inoder to create my custom binding property i should implement INotifyPropertyChanged.
This is the solution for this issue, but i need few days inorder to check it so i will let you know after few days.

Thnx alot

khalil salhi  Sunday, July 26, 2009 7:07 AM

You can use google to search for other answers

Custom Search

More Threads

• How to retrieve the data in DataGridView?
• how to reposition my user controls over a web page
• How do i get currently login user directory
• the binded DateTimePicker don't save the date into the database
• How does the datagrid know when to "update" from changes in a dataset
• is there any structural engineer in this forums ?
• DataGridView cell focus and paste data problem
• Dataset Designer not generating delete statements
• DataGridViewComboBoxCell fill but at last with zero items
• creating controls at runtime