2012-09-13 72 views
-1

我想讓DataGrid中的列可以編輯。我嘗試將readonly更改爲false,但這沒有幫助。如何使DataGrid列可編輯

所以任何人都可以告訴我任何其他方式,我可以嘗試或是這是不讓它是可編輯的。

感謝

我去DataDridView任務和特定的專欄中,我改變了只讀「假」。但是這沒有幫助。 是的,它是一個WinForm。

namespace Payroll 
{ 
    partial class AttendanceLog 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); 
      this.label11 = new System.Windows.Forms.Label(); 
      this.groupBox1 = new System.Windows.Forms.GroupBox(); 
      this.btnShow = new System.Windows.Forms.Button(); 
      this.cmbMonth = new System.Windows.Forms.ComboBox(); 
      this.label6 = new System.Windows.Forms.Label(); 
      this.cmbYear = new System.Windows.Forms.ComboBox(); 
      this.btnClose = new System.Windows.Forms.Button(); 
      this.dgAttendanceLog = new System.Windows.Forms.DataGridView(); 
      this.btnSave = new System.Windows.Forms.Button(); 
      this.statusStrip1 = new System.Windows.Forms.StatusStrip(); 
      this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); 
      this.slno = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.empId = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.empName = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.workedDays = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.absentDays = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.weeklyOff = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.lateCominBy = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.earlyGoingBy = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
      this.groupBox1.SuspendLayout(); 
      ((System.ComponentModel.ISupportInitialize)(this.dgAttendanceLog)).BeginInit(); 
      this.statusStrip1.SuspendLayout(); 
      this.SuspendLayout(); 
      // 
      // label11 
      // 
      this.label11.AutoSize = true; 
      this.label11.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.label11.ForeColor = System.Drawing.SystemColors.ControlText; 
      this.label11.Location = new System.Drawing.Point(14, 42); 
      this.label11.Name = "label11"; 
      this.label11.Size = new System.Drawing.Size(49, 13); 
      this.label11.TabIndex = 106; 
      this.label11.Text = "Month :"; 
      // 
      // groupBox1 
      // 
      this.groupBox1.Controls.Add(this.btnShow); 
      this.groupBox1.Controls.Add(this.cmbMonth); 
      this.groupBox1.Controls.Add(this.label11); 
      this.groupBox1.Controls.Add(this.label6); 
      this.groupBox1.Controls.Add(this.cmbYear); 
      this.groupBox1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.groupBox1.ForeColor = System.Drawing.Color.MidnightBlue; 
      this.groupBox1.Location = new System.Drawing.Point(12, 12); 
      this.groupBox1.Name = "groupBox1"; 
      this.groupBox1.Size = new System.Drawing.Size(688, 79); 
      this.groupBox1.TabIndex = 108; 
      this.groupBox1.TabStop = false; 
      this.groupBox1.Text = "Select"; 
      // 
      // btnShow 
      // 
      this.btnShow.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.btnShow.ForeColor = System.Drawing.Color.Black; 
      this.btnShow.Location = new System.Drawing.Point(533, 34); 
      this.btnShow.Name = "btnShow"; 
      this.btnShow.Size = new System.Drawing.Size(75, 33); 
      this.btnShow.TabIndex = 108; 
      this.btnShow.Text = "Show"; 
      this.btnShow.UseVisualStyleBackColor = true; 
      this.btnShow.Click += new System.EventHandler(this.btnShow_Click); 
      // 
      // cmbMonth 
      // 
      this.cmbMonth.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.cmbMonth.FormattingEnabled = true; 
      this.cmbMonth.Items.AddRange(new object[] { 
      "Month", 
      "Jan", 
      "Feb", 
      "Mar", 
      "Apr", 
      "May", 
      "Jun", 
      "Jul", 
      "Aug", 
      "Sep", 
      "Oct", 
      "Nov", 
      "Dec"}); 
      this.cmbMonth.Location = new System.Drawing.Point(83, 34); 
      this.cmbMonth.Name = "cmbMonth"; 
      this.cmbMonth.Size = new System.Drawing.Size(125, 21); 
      this.cmbMonth.TabIndex = 104; 
      // 
      // label6 
      // 
      this.label6.AutoSize = true; 
      this.label6.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.label6.ForeColor = System.Drawing.SystemColors.ControlText; 
      this.label6.Location = new System.Drawing.Point(283, 42); 
      this.label6.Name = "label6"; 
      this.label6.Size = new System.Drawing.Size(39, 13); 
      this.label6.TabIndex = 107; 
      this.label6.Text = "Year :"; 
      // 
      // cmbYear 
      // 
      this.cmbYear.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.cmbYear.FormattingEnabled = true; 
      this.cmbYear.Location = new System.Drawing.Point(341, 34); 
      this.cmbYear.Name = "cmbYear"; 
      this.cmbYear.Size = new System.Drawing.Size(125, 21); 
      this.cmbYear.TabIndex = 105; 
      // 
      // btnClose 
      // 
      this.btnClose.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.btnClose.ForeColor = System.Drawing.Color.Black; 
      this.btnClose.Location = new System.Drawing.Point(625, 501); 
      this.btnClose.Name = "btnClose"; 
      this.btnClose.Size = new System.Drawing.Size(75, 33); 
      this.btnClose.TabIndex = 9; 
      this.btnClose.Text = "Close"; 
      this.btnClose.UseVisualStyleBackColor = true; 
      this.btnClose.Click += new System.EventHandler(this.btnClose_Click); 
      // 
      // dgAttendanceLog 
      // 
      this.dgAttendanceLog.AllowUserToAddRows = false; 
      dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightCyan; 
      this.dgAttendanceLog.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1; 
      this.dgAttendanceLog.BackgroundColor = System.Drawing.Color.WhiteSmoke; 
      this.dgAttendanceLog.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
      this.dgAttendanceLog.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { 
      this.slno, 
      this.empId, 
      this.empName, 
      this.workedDays, 
      this.absentDays, 
      this.weeklyOff, 
      this.lateCominBy, 
      this.earlyGoingBy}); 
      this.dgAttendanceLog.Location = new System.Drawing.Point(12, 107); 
      this.dgAttendanceLog.Name = "dgAttendanceLog"; 
      this.dgAttendanceLog.RowHeadersVisible = false; 
      this.dgAttendanceLog.Size = new System.Drawing.Size(688, 388); 
      this.dgAttendanceLog.TabIndex = 109; 
      this.dgAttendanceLog.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgAttendanceLog_CellDoubleClick); 
      // 
      // btnSave 
      // 
      this.btnSave.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.btnSave.ForeColor = System.Drawing.Color.Black; 
      this.btnSave.Location = new System.Drawing.Point(544, 501); 
      this.btnSave.Name = "btnSave"; 
      this.btnSave.Size = new System.Drawing.Size(75, 33); 
      this.btnSave.TabIndex = 110; 
      this.btnSave.Text = "Save"; 
      this.btnSave.UseVisualStyleBackColor = true; 
      this.btnSave.Click += new System.EventHandler(this.btnSave_Click); 
      // 
      // statusStrip1 
      // 
      this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 
      this.toolStripStatusLabel1}); 
      this.statusStrip1.Location = new System.Drawing.Point(0, 545); 
      this.statusStrip1.Name = "statusStrip1"; 
      this.statusStrip1.Size = new System.Drawing.Size(711, 22); 
      this.statusStrip1.TabIndex = 111; 
      this.statusStrip1.Text = "statusStrip1"; 
      // 
      // toolStripStatusLabel1 
      // 
      this.toolStripStatusLabel1.Name = "toolStripStatusLabel1"; 
      this.toolStripStatusLabel1.Size = new System.Drawing.Size(298, 17); 
      this.toolStripStatusLabel1.Text = "Note : Please Single click on respective field for manual entry"; 
      // 
      // slno 
      // 
      this.slno.HeaderText = "Sl No."; 
      this.slno.Name = "slno"; 
      this.slno.Width = 50; 
      // 
      // empId 
      // 
      this.empId.HeaderText = "Emp. ID"; 
      this.empId.Name = "empId"; 
      this.empId.Width = 75; 
      // 
      // empName 
      // 
      this.empName.HeaderText = "Emp. Name"; 
      this.empName.Name = "empName"; 
      this.empName.Width = 125; 
      // 
      // workedDays 
      // 
      this.workedDays.HeaderText = "Worked Days"; 
      this.workedDays.Name = "workedDays"; 
      this.workedDays.ReadOnly = true; 
      this.workedDays.Width = 75; 
      // 
      // absentDays 
      // 
      this.absentDays.HeaderText = "Absent Days"; 
      this.absentDays.Name = "absentDays"; 
      this.absentDays.ReadOnly = true; 
      this.absentDays.Width = 75; 
      // 
      // weeklyOff 
      // 
      this.weeklyOff.HeaderText = "Weekly Off"; 
      this.weeklyOff.Name = "weeklyOff"; 
      this.weeklyOff.Width = 75; 
      // 
      // lateCominBy 
      // 
      this.lateCominBy.HeaderText = "Late Going By (in Days)"; 
      this.lateCominBy.Name = "lateCominBy"; 
      // 
      // earlyGoingBy 
      // 
      this.earlyGoingBy.HeaderText = "Early Going By (in Days)"; 
      this.earlyGoingBy.Name = "earlyGoingBy"; 
      // 
      // AttendanceLog 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.BackColor = System.Drawing.Color.WhiteSmoke; 
      this.ClientSize = new System.Drawing.Size(711, 567); 
      this.ControlBox = false; 
      this.Controls.Add(this.statusStrip1); 
      this.Controls.Add(this.btnSave); 
      this.Controls.Add(this.dgAttendanceLog); 
      this.Controls.Add(this.btnClose); 
      this.Controls.Add(this.groupBox1); 
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
      this.Name = "AttendanceLog"; 
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
      this.Text = "Attendance Log"; 
      this.Load += new System.EventHandler(this.AttendanceLog_Load); 
      this.groupBox1.ResumeLayout(false); 
      this.groupBox1.PerformLayout(); 
      ((System.ComponentModel.ISupportInitialize)(this.dgAttendanceLog)).EndInit(); 
      this.statusStrip1.ResumeLayout(false); 
      this.statusStrip1.PerformLayout(); 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.Label label11; 
     private System.Windows.Forms.GroupBox groupBox1; 
     private System.Windows.Forms.Button btnShow; 
     private System.Windows.Forms.Button btnClose; 
     private System.Windows.Forms.DataGridView dgAttendanceLog; 
     private System.Windows.Forms.ComboBox cmbMonth; 
     private System.Windows.Forms.Label label6; 
     private System.Windows.Forms.ComboBox cmbYear; 
     private System.Windows.Forms.Button btnSave; 
     private System.Windows.Forms.StatusStrip statusStrip1; 
     private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; 
     private System.Windows.Forms.DataGridViewTextBoxColumn slno; 
     private System.Windows.Forms.DataGridViewTextBoxColumn empId; 
     private System.Windows.Forms.DataGridViewTextBoxColumn empName; 
     private System.Windows.Forms.DataGridViewTextBoxColumn workedDays; 
     private System.Windows.Forms.DataGridViewTextBoxColumn absentDays; 
     private System.Windows.Forms.DataGridViewTextBoxColumn weeklyOff; 
     private System.Windows.Forms.DataGridViewTextBoxColumn lateCominBy; 
     private System.Windows.Forms.DataGridViewTextBoxColumn earlyGoingBy; 
    } 
} 
+0

的WinForms?的WebForms? – trailmax

+0

你應該更準確地描述你的問題。例如發佈一些代碼或告訴我們你到底在做什麼。 –

+0

如果不雙擊單元格但單擊單元格會發生什麼?點擊哪個列? – BlueM

回答

0

好的,我發現你的問題。您最有可能使用的不是DataSource或使用ReadOnlyCollection作爲Datasource。然後DataGridView將不允許編輯。

我看到了與您相同的行爲,如果我只將行添加到數據網格。只要我使用BindingList作爲數據源,我可以編輯(單擊一次到單元格並開始鍵入)。您可能會考慮刪除雙擊處理程序,因爲這會干擾默認行爲。

這個工作對我來說:

public class Person 
{ 
    public string Name { get; set; } 
    public string EmpId { get; set; } 
} 

public MainForm() 
{ 
    InitializeComponent(); 

    // bind column to property of person 
    empName.DataPropertyName = "Name"; 
    empId.DataPropertyName = "EmpId"; 

    // set our bindinglist of persons as datasource 
    var bindingList = new BindingList<Person>(); 
    bindingList.Add(new Person(){ Name = "John", EmpId = "12345" }); 
    bindingList.Add(new Person(){ Name = "David", EmpId = "12346" }); 
    dgAttendanceLog.DataSource = bindingList; 
} 
+0

非常感謝@BlueM它的工作,在代碼中它只是只讀的,所以只是改變它,它工作正常。非常感謝您的支持。 – mark

0

在的WinForms,你可以DataGrid的編輯模式屬性設置爲EditOnEnter數據網格視圖的。也只讀屬性應該是假的。那麼你可以爲每個列設置ReadOnly。