Click here to Skip to main content
16,022,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGridView control in a form, in a C#.net applicaion. I am trying to make a cell of it to be editable in order to be able to change some cells values, with setting ReadOnly property to False. But unfortunatelly, no effect. When I run step over, although I change the ReadOnly Value to False, remains True!!! Where am I wrong? Thank you so much in advanced.

What I have tried:

C#
foreach (DataGridViewRow dgRow in ProductsDataGridView.Rows)
{
   foreach (DataGridViewColumn dgCol in ProductsDataGridView.Columns)
   {
       if (dgCol.Name == "OrderQuantity")
       {
          ProductsDataGridView.CurrentCell = 
          ProductsDataGridView.Rows[dgRow.Index].Cells[dgCol.Index];
          int columnIndex = ProductsDataGridView.CurrentCell.ColumnIndex;
          ProductsDataGridView.Columns[columnIndex].ReadOnly = false;
       }
       else
       {
          ProductsDataGridView.CurrentCell = 
          ProductsDataGridView.Rows[dgRow.Index].Cells[dgCol.Index];
          int columnIndex = ProductsDataGridView.CurrentCell.ColumnIndex;
          ProductsDataGridView.Columns[columnIndex].ReadOnly = true;
       }
   }
}
Posted
Updated 19-Jun-24 7:26am
v2
Comments
Maciej Los 19-Jun-24 13:51pm    
Does the datagridview is bound with datasource (database)? If yes, then you have to use bindingsource to enable two-way communication.

If I copy'n'paste your code into a test app and load it with data using the DGV DataSource to a DataTable, then it works exactly as I would expect - the single column of cells becomes editable, all the others don't.

Unless I set the whole DGV.ReadOnly property to true - in which case I see what you get: changing the individual cell property does not take effect at all.
Which is exactly what I'd expect: the "whole control" property should supercede the "local cell" property.

[edit]
There is a much shorter, simpler way to do what you want though: just set the column.ReadOnly property. You can do this in the designer, or programmatically:
C#
foreach (DataGridViewColumn dgCol in ProductsDataGridView.Columns)
    {
    dgCol.ReadOnly = dgCol.Name != "OrderQuantity";
    }

[/edit]
 
Share this answer
 
v2
Comments
Aggeliki Asimakopoulou 20-Jun-24 1:44am    
OMG, thank you so much. I had set the DataGridView ReadOnly Property to True to the search button. Thank you very much. Everything was fine except this.
OriginalGriff 20-Jun-24 2:19am    
You're welcome!
That's the way I bring the results to the DataGridView from another form and it runs perfectly.
My problem is that I can't edit any of the cells in the DataGridView, even I set the cell ReadOnly Property to False.

C#
<pre>
public void LoadRecord(Form sourceForm)
{
     //11122023 analoga me to onoma tis formas ta controls
           
     int indx;
     IEnumerable<GroupBox> GroupBoxes;
     GroupBoxes=SaveSQLquery.GetControls<GroupBox>(this.Controls);
     List<DataTableField> TmpDtFLst=new List<DataTableField>();
     bool exitFlg = false;
     IEnumerable<Panel> Panels;
     Panels = SaveSQLquery.GetControls<Panel>(this.Controls);
     int OrderDetailsGridRowsCnt = 0;
     decimal CurrentFinalPrice, RoundCrntFinalPrice;
     CurrentFinalPrice = 0; RoundCrntFinalPrice = 0;


     if (Forms.ProductsSearchForm._ProductsSearchForm != null)
        if (sourceForm.Name.Equals(Forms.ProductsSearchForm._ProductsSearchForm.Name))
        {
            OrderDetailsGridRowsCnt = ProductsDataGridView.Rows.Count;
                    
            //12122023
            IEnumerable<DataGridView> dtGrdViews =Enumerable.Empty<DataGridView>();
                    
            var index = ProductsDataGridView.Rows.Add();
                   
            if (Forms.ProductsSearchForm.GridRowsCnt > 0)
            {
                 indx = Forms.ProductsSearchForm.selectedRow.Index;
                 ProductSearchGrid = Forms.ProductsSearchForm.DtGrid;
                 string CellValue_, ColumnName;
                   
                 for (int i = 0; i < ProductSearchGrid.Columns.Count; i++)
                 {
                     for (int j = 0; j < ProductsDataGridView.Columns.Count; j++)
                        if (ProductsDataGridView.Columns[j].Name.Equals(ProductSearchGrid.Columns[i].Name))
                        {
                           CellValue_ = Forms.ProductsSearchForm.selectedRow.Cells[i].Value.ToString();
                           ColumnName = ProductsDataGridView.Columns[j].Name;
                                   
                           if (!Exists(CellValue_) && ColumnName == "ProductID")
                               ProductsDataGridView.Rows[index].Cells[ColumnName].Value = CellValue_;
                           else if (!ColumnName.Equals("ProductID"))
                                ProductsDataGridView.Rows[index].Cells[ColumnName].Value = CellValue_;
                           else if (Exists(CellValue_) && ColumnName == "ProductID")
                                { exitFlg = true; break; }
                         }
                         else if (ProductSearchGrid.Columns[i].Name.Equals("ProductStockQuantity"))
                         {
                            StockQuantity = Int32.Parse(Forms.ProductsSearchForm.selectedRow.
                            Cells["ProductStockQuantity"].Value.ToString());
                         }
                            
                         if (exitFlg)
                         {
                             ProductsDataGridView.Rows.Remove
                                        (ProductsDataGridView.Rows[ProductsDataGridView.Rows.Count-1]); 
                              break;
                         }
                    
            }
      }
      if (ProductsDataGridView.Rows.Count>0)
      {
          int indxOfOrderQuantity=0;
          //Βάζουμε από i=OrderDetailsGridRowsCnt διότι το πρώτο προϊόν/ντα που είναι στο grid,
          //μπορεί να έχει/ουν παραπάνω ποσότητα.
          //Άρα πρέπει να ξέρουμε πόσες είναι οι ήδη υπάρχουσες εγγραφές.

          //1906224
          string ConvertedFinalPrice;
                       
          if (!String.IsNullOrEmpty(FinalPrice.Text))
          {
             ConvertedFinalPrice = OrderDetails.ReplaceCommaWithDot(FinalPrice.Text);
             CurrentFinalPrice = Convert.ToDecimal(ConvertedFinalPrice, new CultureInfo("en-US"));
             RoundCrntFinalPrice = decimal.Round(CurrentFinalPrice, 2);
          }
          //

          for (int i = OrderDetailsGridRowsCnt; i < ProductsDataGridView.Rows.Count; i++)
             for (int j = 0; j < ProductsDataGridView.Columns.Count; j++)
                 if (ProductsDataGridView.Columns[j].Name.Equals("OrderQuantity"))//"OrderProductStock")
                 {
                     // var test = ProductsDataGridView.Rows[i].Cells["ProductName"].Value.ToString();
                     indxOfOrderQuantity = j;
                     if (StockQuantity > 0)
                     {
                         Quantity = 1;// 
                         decimal.Parse(ProductsDataGridView.Rows[i].Cells[j].Value.ToString());
                         ProductsDataGridView.Rows[i].Cells[j].Value = Quantity.ToString();
                     }
                  }
            //03012024 editable cell OrderQuantity
            foreach (DataGridViewRow row in ProductsDataGridView.Rows)
            {
                row.Cells[indxOfOrderQuantity].ReadOnly = false;
            }
                        
            indx = ProductsDataGridView.Rows.Count - 1;
            productGrdSelectedRow = ProductsDataGridView.Rows[indx];

                        
            CalculateSum(Quantity, RoundCrntFinalPrice);

            DataGridViewColumn ProdNameCol = ProductsDataGridView.Columns["ProductName"];
            ProdNameCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
           }
       }
          
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900