如何使datagrid列可编辑

时间:2012-09-13 12:49:55

标签: c#

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

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

由于

我去了DataDridView Task,在那个特定的列上我改变了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;
    }
}

2 个答案:

答案 0 :(得分:0)

在WinForms中,您可以将DataGrid的EditMode属性设置为 EditOnEnter 。否则,datagrid视图的readonly属性应为false。然后你可以为每一列设置ReadOnly。

答案 1 :(得分:0)

好的,我发现了你的问题。您最合适的是不使用DataSource或使用ReadOnlyCollection作为数据源。然后DataGridView根本不允许编辑。

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

这对我有用:

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;
}