locked
Cells in DataGridView cannot edit RRS feed

  • Question

  • I use Visual Studio 2005 and create a simple Windows application.

     

    I created a DataGridView with 2 textbox columns. Databinding is OK, but I cannot edit any cell (no matter I press F2, or click the cell). I only see the cell is highlighted with blue backgound color.

     

    This is my code for binding data to DataGridView:

     

    private void BindData(KeyValueConfigurationCollection keyValueCollection)

    {

    DataTable myDataTable = CreateTableForGridView();

    DataRow myDataRow;

    foreach (KeyValueConfigurationElement keyValue in keyValueCollection)

    {

    if (!keyValue.Key.Equals("lookupCategoryForCheckin") && !keyValue.Key.Equals("useOriginialPrintData"))

    {

    myDataRow = myDataTable.NewRow();

    myDataRow["PrinterIP"] = keyValue.Key;

    myDataRow["PrinterName"] = keyValue.Value;

    myDataTable.Rows.Add(myDataRow);

    }

    }

    // Binds the data to GridView Control

    DataView dtView = new DataView();

    try

    {

    BindingSource bs = new BindingSource();

    bs.DataSource = myDataTable;

    dgvPrinter.DataSource = bs;

    dgvPrinter.EditMode = DataGridViewEditMode.EditOnEnter;

    }

    catch (Exception ex) { }

    }

     

    If I don't bind code,  then I can see a blank datagridview, and I can edit.

     

    Please help.... and many thanks!!!

     

    Tuesday, November 13, 2007 9:44 PM

Answers

  • OK. Problem solved.

     

    In CreateTableForGridView(), I should set myDataColumn.ReadOnly=false.

     

     

    Wednesday, November 14, 2007 1:57 PM
  • I workaround this issue by adding 2 events. Note: dvgPrinter is my datagridview.

     

    private void dgvPrinter_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

    {

    dgvPrinter.CurrentCell.ReadOnly = true;

    }

    private void dgvPrinter_CellClick(object sender, DataGridViewCellEventArgs e)

    {

    dgvPrinter.CurrentCell.ReadOnly = false;

    }

     

    It works fine. But I'd like to know some better solutions.  Thanks.

     

     

    Wednesday, November 14, 2007 4:52 PM

All replies

  • Hi

    I think that your DataTable doesn't support updating.

    Check your CreateTableForGridView method !

    Kind regards
    Wednesday, November 14, 2007 12:33 PM
  • Thanks, Damiaan.
    This is the code of CreateTableForGridView. Could you tell me where should I change?

     

    private DataTable CreateTableForGridView()

    {

    // Instantiate a DataTable object. Set its name property as ChannelManagementTbl

    DataTable myDataTable = new DataTable("PrinterTbl");

    // Get a pair of DataColumn and DataRow reference

    DataColumn myDataColumn;

    // Create PrinterIP Column

    myDataColumn = new DataColumn();

    myDataColumn.DataType = System.Type.GetType("System.String");

    myDataColumn.ColumnName = "PrinterIP";

    myDataColumn.ReadOnly = true;

    myDataColumn.Unique = false;

    myDataTable.Columns.Add(myDataColumn);

    //Create PrinterName Column

    myDataColumn = new DataColumn();

    myDataColumn.DataType = System.Type.GetType("System.String");

    myDataColumn.ColumnName = "PrinterName";

    myDataColumn.ReadOnly = true;

    myDataColumn.Unique = false;

    myDataTable.Columns.Add(myDataColumn);

    return myDataTable;

    }

    Wednesday, November 14, 2007 1:54 PM
  • OK. Problem solved.

     

    In CreateTableForGridView(), I should set myDataColumn.ReadOnly=false.

     

     

    Wednesday, November 14, 2007 1:57 PM
  • Indeed.

     

    If you left out this line, it would have worked also.

     

    Kind regards

    Damiaan

    Wednesday, November 14, 2007 1:59 PM
  • Now I've another issue.

     

    When I click RowHeader, I cannot select an entire row, but the first cell is automatically in edit mode. This causes me cannot delete a row!!

     

    I tried to set EditMode of this datagridview as EditOnKeystroke, EditOnKeystrokeOrF2, EditOnF2, EditPrgrammatically, but always got this issue.

     

     

    Any solutions??? Many thanks.

    Wednesday, November 14, 2007 2:41 PM
  • Hi

     

    Try the SelectionMode property must be set to FullRowSelect or RowHeaderSelect ...

     

    Kind regards

    Wednesday, November 14, 2007 3:17 PM
  • I tried to set SelectionMode property to FullRowSelect or RowHeaderSelect ... still the same problem.

     

    When using "FullRowSelect", after datagridview is loaded the 1st row selected (all cells in 1st row are blue color).

     

    When using "RowHeaderSelect", after datagridview is loaded the 1st cell selected.

     

    Then if I click any other row header, the first cell is automatically in edit mode, and other cells are in blue.

     

    Is this becuase my CreateTableForGridView() method still has bug?

     

    Wednesday, November 14, 2007 3:31 PM
  • I workaround this issue by adding 2 events. Note: dvgPrinter is my datagridview.

     

    private void dgvPrinter_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

    {

    dgvPrinter.CurrentCell.ReadOnly = true;

    }

    private void dgvPrinter_CellClick(object sender, DataGridViewCellEventArgs e)

    {

    dgvPrinter.CurrentCell.ReadOnly = false;

    }

     

    It works fine. But I'd like to know some better solutions.  Thanks.

     

     

    Wednesday, November 14, 2007 4:52 PM
  • There are probably better solutions.  If you put your DataGridView on your form using the DataSource Explorer (SHIFT + ALT + D), this should be default behavior.

    Friday, November 16, 2007 3:12 PM
  • 11 years old ~ .

    my solution is 

    you have to check your datagridview properties  on  rowTemplate item

    i think that it's subitem  maybe setted readOnly is true.  you have to change it to false .

    in my case , it is simple  problem,but hard to solve( or find).

    Wednesday, April 25, 2018 2:00 PM
  • I just created an account to thank you for this comment. It literately saved my life. Thanks so much!
    Friday, July 24, 2020 7:43 PM