Enable read only column as editable when it is a new row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satenova
    New Member
    • Apr 2008
    • 3

    Enable read only column as editable when it is a new row

    Hello Friends,

    I am using datagrid to display records from access db. I got the first field as ID which is primary key. As users should not edit it i made that column as read only but i need that column to be editable when users tries to add a new record(row). I am using windows forms (vb.net)
  • Monomachus
    Recognized Expert New Member
    • Apr 2008
    • 127

    #2
    Try ro read this
    Customizing the DataGrid
    and this
    DataGridColumnS tyle.ReadOnly Property
    Also I think u should put ID autoincreasing value or smth like that. Because from my point of view it's a bad idea to make it visible, u could make ID hidden and other columns visible, and autoincreasing it will solve the problem. If u have smth like login u should make another column "TXT_LOGIN" or smth like that.

    I'll try to make that today.

    Comment

    • Monomachus
      Recognized Expert New Member
      • Apr 2008
      • 127

      #3
      I'll try to make that today.
      So I made a try, but I think it should be even a better way to do it.
      Code:
      // Step 1
                  DataGridTableStyle tableStyle = new DataGridTableStyle();
                  tableStyle.MappingName = "Products";
      
                  DataColumnCollection myDataColumns = ds.Tables["Products"].Columns;
      
                  // Step 2
                  DataGridTextBoxColumn dgTbCol;
      
      
      
                  foreach (DataColumn dataColumn in myDataColumns)
                  {
                      dgTbCol = new DataGridTextBoxColumn();
                      dgTbCol.MappingName = dataColumn.ColumnName;
                      dgTbCol.ReadOnly = true;
      
                      tableStyle.GridColumnStyles.Add(dgTbCol);
      
                  }
      
                  dataGrid1.TableStyles.Add(tableStyle);
      
                  // Step 3
      
                  ds.Tables["Products"].TableNewRow += new DataTableNewRowEventHandler(Form1_TableNewRow);
                  ds.Tables["Products"].RowChanged += new DataRowChangeEventHandler(Form1_RowChanged);
                  dataGrid1.CurrentCellChanged +=new EventHandler(dataGrid1_CurrentCellChanged);
      
               private void Form1_TableNewRow(Object sender, DataTableNewRowEventArgs e)
              {
                  currentRow = (sender as DataTable).Rows.Count;
                  GridColumnStylesCollection gcsc = dataGrid1.TableStyles["Products"].GridColumnStyles;
                  DataRowCollection drc;
                  foreach (DataGridTextBoxColumn dgTbCol in gcsc)
                  {
                      if (dataGrid1.CurrentCell.RowNumber == currentRow)
                      {
                          Console.WriteLine(dataGrid1.CurrentCell.RowNumber);
                          dgTbCol.ReadOnly = false;
                      }
                  }
              }
      
              private void Form1_RowChanged(Object sender, DataRowChangeEventArgs e) 
              {
                  GridColumnStylesCollection gcsc = dataGrid1.TableStyles["Products"].GridColumnStyles;
                  foreach (DataGridTextBoxColumn dgTbCol in gcsc)
                  {
                      dgTbCol.ReadOnly = true;
                  }
              }
      
              private void dataGrid1_CurrentCellChanged(Object sender, EventArgs e)
              {
                  GridColumnStylesCollection gcsc = dataGrid1.TableStyles["Products"].GridColumnStyles;
                  foreach (DataGridTextBoxColumn dgTbCol in gcsc)
                  {
                      // currentRow should be a global variable
                      if (dataGrid1.CurrentCell.RowNumber != currentRow)
                      {
                          Console.WriteLine(dataGrid1.CurrentCell.RowNumber);
                          dgTbCol.ReadOnly = true;
                      }
                  }
              }
      One REMARK: i've made all columns readonly and then is added a new row all are editable in new row. U can make minor changes to accomodate it to ur requirements.
      U should pay attention to events TableNewRow, RowChanged, and CurrentCellChan ged, if u have questions ask, and let me know if it helped u.

      Comment

      • satenova
        New Member
        • Apr 2008
        • 3

        #4
        Originally posted by Monomachus
        So I made a try, but I think it should be even a better way to do it.
        Code:
        // Step 1
                    DataGridTableStyle tableStyle = new DataGridTableStyle();
                    tableStyle.MappingName = "Products";
        
                    DataColumnCollection myDataColumns = ds.Tables["Products"].Columns;
        
                    // Step 2
                    DataGridTextBoxColumn dgTbCol;
        
        
        
                    foreach (DataColumn dataColumn in myDataColumns)
                    {
                        dgTbCol = new DataGridTextBoxColumn();
                        dgTbCol.MappingName = dataColumn.ColumnName;
                        dgTbCol.ReadOnly = true;
        
                        tableStyle.GridColumnStyles.Add(dgTbCol);
        
                    }
        
                    dataGrid1.TableStyles.Add(tableStyle);
        
                    // Step 3
        
                    ds.Tables["Products"].TableNewRow += new DataTableNewRowEventHandler(Form1_TableNewRow);
                    ds.Tables["Products"].RowChanged += new DataRowChangeEventHandler(Form1_RowChanged);
                    dataGrid1.CurrentCellChanged +=new EventHandler(dataGrid1_CurrentCellChanged);
        
                 private void Form1_TableNewRow(Object sender, DataTableNewRowEventArgs e)
                {
                    currentRow = (sender as DataTable).Rows.Count;
                    GridColumnStylesCollection gcsc = dataGrid1.TableStyles["Products"].GridColumnStyles;
                    DataRowCollection drc;
                    foreach (DataGridTextBoxColumn dgTbCol in gcsc)
                    {
                        if (dataGrid1.CurrentCell.RowNumber == currentRow)
                        {
                            Console.WriteLine(dataGrid1.CurrentCell.RowNumber);
                            dgTbCol.ReadOnly = false;
                        }
                    }
                }
        
                private void Form1_RowChanged(Object sender, DataRowChangeEventArgs e) 
                {
                    GridColumnStylesCollection gcsc = dataGrid1.TableStyles["Products"].GridColumnStyles;
                    foreach (DataGridTextBoxColumn dgTbCol in gcsc)
                    {
                        dgTbCol.ReadOnly = true;
                    }
                }
        
                private void dataGrid1_CurrentCellChanged(Object sender, EventArgs e)
                {
                    GridColumnStylesCollection gcsc = dataGrid1.TableStyles["Products"].GridColumnStyles;
                    foreach (DataGridTextBoxColumn dgTbCol in gcsc)
                    {
                        // currentRow should be a global variable
                        if (dataGrid1.CurrentCell.RowNumber != currentRow)
                        {
                            Console.WriteLine(dataGrid1.CurrentCell.RowNumber);
                            dgTbCol.ReadOnly = true;
                        }
                    }
                }
        One REMARK: i've made all columns readonly and then is added a new row all are editable in new row. U can make minor changes to accomodate it to ur requirements.
        U should pay attention to events TableNewRow, RowChanged, and CurrentCellChan ged, if u have questions ask, and let me know if it helped u.
        Thanks for your suggestion. I am newbie to the total .net environment. Can u help me doing the same in visual basic.

        Comment

        Working...