Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically entering data into a readonly datagridview

Status
Not open for further replies.

ggeorgiou01

Programmer
Apr 11, 2005
60
GB
Morning all,

I am encountering an extremly annoying prooblem. Which is doing my head in. Can you guys help ?

I my windows form i am using a datagridview.

Ok so heres what im doing:
__________________________________________________________

On Load, i am binding my columns to the datagrid:


dc = new DataColumn();
dc.DataType = Type.GetType("System.Int32");
dc.ColumnName = "#";
dc.ReadOnly = false;
dt.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "Code";
dc.MaxLength = 7;
dt.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "Description";
dc.ReadOnly = true;
dt.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = Type.GetType("System.Int32");
dc.ColumnName = "Qty";
dc.ReadOnly = false;
dt.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "Exp Code";
dc.ReadOnly = false;
dt.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "Unit";
dc.ReadOnly = true;
dt.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = Type.GetType("System.Decimal");
dc.ColumnName = "Price";
dc.ReadOnly = true;
dt.Columns.Add(dc);

dc = new DataColumn();
dc.DataType = Type.GetType("System.Decimal");
dc.ColumnName = "Total";
dc.ReadOnly = true;
dt.Columns.Add(dc);
__________________________________________________________

Then i have a CellValueChanged event which includes the following:


if (OrderLinesDg["Qty", RowNo].Value != DBNull.Value)
{

decimal TotalPrice = Convert.ToDecimal(OrderLinesDg["Qty", RowNo].Value) * Convert.ToDecimal(OrderLinesDg["Price", RowNo].Value);

OrderLinesDg["Total", RowNo].ReadOnly = false;
OrderLinesDg["Total", RowNo].Value = TotalPrice.ToString("F");
OrderLinesDg["Total", RowNo].ReadOnly = true;

}

__________________________________________________________


When i run this code, i get an error message saying the 'Total' cloumn is readonly. Which i cannot understand as i have made the readonly attribute to false. Before i right into the column.

Any help would be much apprciated.
 
Have you stepped through the code?

Have you taken off all of your changes to the read Only settings to see if your code works without them?
 
Hi,

Just a thought, can't you just make the Total column a computed column? I think the dataTable supports such (there's a column property named "Expression" i think, where you put the formula "Qty * Price"). This way you don't have to bother doing the computation yourself.

my 2 cents [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top