0

Hi All,
In my application, i have a datagridview with 5 colums, at the form load the datagridview contains one row, I am showing a combobox in each of the 4 columns to select the values (i doesnot type any thing in that 4 columns), when i press tab the focus goes from column 4 to column 5.
Now when again i press tab the focus leaves the datagridview, but here i want that when i leave the column 5, there must be a row added.
How can i do that, can anyone help me on that?
Thanks in advance.
Regards,
Vidya

3
Contributors
9
Replies
15
Views
8 Years
Discussion Span
Last Post by sknake
0

Not sure why your datagridview doesn't always have an unpopulated record shown. Check this code. As soon as you type anything in a cell, it will append a new unpopulated record/row to the end of the grid, which will again repeat as soon as you type anything in the new row...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ForumSolutions
{
    public class Form_DataGridView_AddRow : Form
    {
        public Form_DataGridView_AddRow()
        {
            InitializeComponent();
        }

        private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        #region Designer generated code

        /// <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()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1,
            this.Column2,
            this.Column3});
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(489, 278);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.dataGridView1_KeyPress);
            // 
            // Column1
            // 
            this.Column1.HeaderText = "Column1";
            this.Column1.Name = "Column1";
            // 
            // Column2
            // 
            this.Column2.HeaderText = "Column2";
            this.Column2.Name = "Column2";
            // 
            // Column3
            // 
            this.Column3.HeaderText = "Column3";
            this.Column3.Name = "Column3";
            // 
            // Form_DataGridView_AddRow
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(489, 278);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form_DataGridView_AddRow";
            this.Text = "Form_DataGridView_AddRow";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column3;

        #endregion // Designer code
    }
}
0

You could also override the tab behavior of your form. In my case this DataGridView is databound to "dt" which is a datatable. You'll have to handle the adding of a new row depending on how you implemented the grid:

protected override bool ProcessTabKey(bool forward)
    {
      if (forward && (this.ActiveControl == dataGridView1)) //Stop it from advancing
      {
        DataRow row = dt.NewRow();
        dt.Rows.Add(row);
        int idx = dataGridView1.Rows.GetLastRow(DataGridViewElementStates.Visible);
        dataGridView1.Rows[idx].Selected = true;
        return false;
      }
      else
      {
        return base.ProcessTabKey(forward);
      }
    }
0

The records are not fetched from database, only thing is that i want to add row on the cell leave event of the last column without writing any thing in any cell of the row. The data in first 4 columns are added by using combo boxes which i make visible on keypress of particular cell.

0

This should do the trick:

protected override bool ProcessTabKey(bool forward)
    {
      if (forward && (this.ActiveControl == dataGridView1)) //Stop it from advancing
      {
        int idx = dataGridView1.Rows.Add();
        dataGridView1.Rows[idx].Selected = true;
        return false;
      }
      else
      {
        return base.ProcessTabKey(forward);
      }
    }
0

You're welcome

Please mark this thread as solved if we have answered your question and good luck!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.