R
Rudy Meijer
I have a problem.
I have bound two TextBoxes on a windows form to a
DataTable. Everything works fine but when I make one
column of the datatable readonly a ReadOnly Exception
occurs when the row position of the datatable is changed.
Unhandled Exception: System.Data.ReadOnlyException:
Column 'field2' is read only.
at System.Data.DataColumn.CheckReadOnly(DataRow row)
at System.Data.DataTable.RaiseColumnChanging(DataRow
row, DataColumnChangeEventArgs e)
at System.Data.DataRow.set_Item(DataColumn column,
Object value)
at System.Data.DataRowView.SetColumnValue(DataColumn
column, Object value)
at System.Data.DataColumnPropertyDescriptor.SetValue
(Object component, Object value)
at System.Windows.Forms.BindToObject.SetValue(Object
value)
at System.Windows.Forms.Binding.PullData()
at System.Windows.Forms.BindingManagerBase.PullData()
at
System.Windows.Forms.CurrencyManager.CurrencyManager_PullDa
ta()
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at
System.Windows.Forms.CurrencyManager.ChangeRecordState
(Int32 newPosition, Boolean validating, Boolean
endCurrentEdit, Boolean firePositionChange, Boolean
pullData)
at System.Windows.Forms.CurrencyManager.set_Position
(Int32 value)
at Form2.NextButton_Click(Object sender, EventArgs e)
in d:\projects\visual studio
projects\database\form2.cs:line 47
Below this line you will find the code snipped in which
the Readonly exception occurred.
------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button NextButton;
private System.Windows.Forms.Button PrevButton;
private DataTable dt;
public static void Main()
{
Application.Run(new Form2());
}
public Form2()
{
InitializeComponent();
// Create a datatable.
dt = new DataTable("myTable");
// Add two columns.
dt.Columns.Add("field1");
dt.Columns.Add("field2");
// Make second column readonly.
// This statement will give the exception
when the next/prev button is pressed!
dt.Columns["field2"].ReadOnly=true;
// Add two data rows.
DataRow row = dt.NewRow();
row["field1"] = "aaa";
row["field2"] = "bbb";
dt.Rows.Add(row);
row = dt.NewRow();
row["field1"] = "1aaa";
row["field2"] = "1bbb";
dt.Rows.Add(row);
// Bind both columns to a textbox.
textBox1.DataBindings.Add
("text",dt,"field1");
textBox2.DataBindings.Add
("text",dt,"field2");
}
private void NextButton_Click(object sender,
System.EventArgs e)
{
BindingContext[dt].Position +=1;
}
private void PrevButton_Click(object sender,
System.EventArgs e)
{
BindingContext[dt].Position -=1;
}
}
I have bound two TextBoxes on a windows form to a
DataTable. Everything works fine but when I make one
column of the datatable readonly a ReadOnly Exception
occurs when the row position of the datatable is changed.
Unhandled Exception: System.Data.ReadOnlyException:
Column 'field2' is read only.
at System.Data.DataColumn.CheckReadOnly(DataRow row)
at System.Data.DataTable.RaiseColumnChanging(DataRow
row, DataColumnChangeEventArgs e)
at System.Data.DataRow.set_Item(DataColumn column,
Object value)
at System.Data.DataRowView.SetColumnValue(DataColumn
column, Object value)
at System.Data.DataColumnPropertyDescriptor.SetValue
(Object component, Object value)
at System.Windows.Forms.BindToObject.SetValue(Object
value)
at System.Windows.Forms.Binding.PullData()
at System.Windows.Forms.BindingManagerBase.PullData()
at
System.Windows.Forms.CurrencyManager.CurrencyManager_PullDa
ta()
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at
System.Windows.Forms.CurrencyManager.ChangeRecordState
(Int32 newPosition, Boolean validating, Boolean
endCurrentEdit, Boolean firePositionChange, Boolean
pullData)
at System.Windows.Forms.CurrencyManager.set_Position
(Int32 value)
at Form2.NextButton_Click(Object sender, EventArgs e)
in d:\projects\visual studio
projects\database\form2.cs:line 47
Below this line you will find the code snipped in which
the Readonly exception occurred.
------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button NextButton;
private System.Windows.Forms.Button PrevButton;
private DataTable dt;
public static void Main()
{
Application.Run(new Form2());
}
public Form2()
{
InitializeComponent();
// Create a datatable.
dt = new DataTable("myTable");
// Add two columns.
dt.Columns.Add("field1");
dt.Columns.Add("field2");
// Make second column readonly.
// This statement will give the exception
when the next/prev button is pressed!
dt.Columns["field2"].ReadOnly=true;
// Add two data rows.
DataRow row = dt.NewRow();
row["field1"] = "aaa";
row["field2"] = "bbb";
dt.Rows.Add(row);
row = dt.NewRow();
row["field1"] = "1aaa";
row["field2"] = "1bbb";
dt.Rows.Add(row);
// Bind both columns to a textbox.
textBox1.DataBindings.Add
("text",dt,"field1");
textBox2.DataBindings.Add
("text",dt,"field2");
}
private void NextButton_Click(object sender,
System.EventArgs e)
{
BindingContext[dt].Position +=1;
}
private void PrevButton_Click(object sender,
System.EventArgs e)
{
BindingContext[dt].Position -=1;
}
}