0

我想让 DataGrid 中的 2 列可编辑。我尝试将 readonly 更改为 false,但这没有帮助。

所以任何人都可以告诉我我可以尝试的任何其他方式,或者这是不允许它可编辑的东西。

谢谢

我去了 DataDridView 任务,在那个特定的列上我更改了 ReadOnly 'false'。但这没有帮助。是的,它是一个 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;
    }
}
4

2 回答 2

0

在 WinForms 中,您可以将 DataGrid 的 EditMode 属性设置为EditOnEnter。datagrid 视图的只读属性也应该为 false。然后您可以为每个列设置 ReadOnly。

于 2012-09-13T13:07:17.030 回答
0

好的,我发现了你的问题。您最有可能不使用数据源或使用 aReadOnlyCollection作为数据源。然后 DataGridView 将根本不允许编辑。

如果我只将行添加到数据网格,我会看到与您相同的行为。只要我使用 aBindingList作为数据源,我就可以进行编辑(单击一次进入单元格并开始输入)。您也可以考虑删除您的双击处理程序,因为这会干扰默认行为。

这对我有用:

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;
}
于 2012-09-13T14:34:37.733 回答