4 Replies - 6309 Views - Last Post: 03 September 2011 - 05:39 AM Rate Topic: -----

#1 samipshr@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 26-July 09

Manage TabIndex for DataGridView

Posted 03 January 2010 - 03:40 AM

HI,
I have bound the data to datagridview which has 11 columns. Column 4 and Column 10 has to be filled by user.

I enter ID in column 4 and column 5 and 6 are filled automatically. And after i fill column 4 and click tab , focus should be column 10.Also after column10 is filled and clicking tab key next row of column 4 should be focused

Now my problem is after i enter the value in column 4 and click Tab Column 5 get foucs. It should rather focus on column 10.

How can make column 10 focus after i press Tab key?

My code is sth like this...
private void dgvBaking_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				if (e.ColumnIndex == 3 && e.RowIndex != -1)
				{
					// Get the contID from the the SP Ref No Column
					int contID = 0;
					if (dgvBaking.Rows[e.RowIndex].Cells["SPRefNo"].Value.ToString() == "0")
					{
						return;
					}
					if (string.IsNullOrEmpty(dgvBaking.Rows[e.RowIndex].Cells["SPRefNo"].Value.ToString()) == false && tmp.IsInteger(dgvBaking.Rows[e.RowIndex].Cells["SPRefNo"].Value.ToString()))
					{
						contID = Convert.ToInt32(dgvBaking.Rows[e.RowIndex].Cells["SPRefNo"].Value);
					}
					Contractor objCont = new Contractor();
					//Get contractor name and phone
					DataTable dtCont = objCont.getContractorForCSV(contID);
					if (dtCont.Rows.Count > 0)
					{
						dgvBaking.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value = dtCont.Rows[0][0].ToString() + " " + dtCont.Rows[0][1].ToString();
						dgvBaking.Rows[e.RowIndex].Cells[e.ColumnIndex + 2].Value = dtCont.Rows[0][2].ToString();
					}
					else
					{
						dgvBaking.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
					}

				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				string strLog = string.Empty;
				strLog = "========================\r\n";
				strLog = strLog + System.DateTime.Now.ToString() + "\r\n";
				strLog = strLog + "========================\r\n";
				strLog = strLog + "=====================================================================================================================================\r\n";
				strLog = strLog + "Form:'" + this.Name + "'\r\nError:"+ ex.Message +"\r\n" + ex.StackTrace.ToString() + "\r\n";
				strLog = strLog + "=====================================================================================================================================\r\n";
				ErrorLog rLog = new ErrorLog();
				rLog.writeLog(strLog);
			}

		}

		private void dgvBaking_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
		{
			if (dgvBaking.Columns[e.ColumnIndex].Name == "SPRefNo")
			{
				if (String.IsNullOrEmpty(e.FormattedValue.ToString())==false)
				{
					if (!tmp.IsInteger(e.FormattedValue.ToString()))
					{
						dgvBaking.Rows[e.RowIndex].ErrorText =
							"Enter the correct contractor reference number";
						e.Cancel = true;
					}
				}
			}
		}

		private void dgvBaking_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			dgvBaking.Rows[e.RowIndex].ErrorText = String.Empty;
		}



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Manage TabIndex for DataGridView

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Manage TabIndex for DataGridView

Posted 07 January 2010 - 08:05 AM

Something like that should do. If anything unclear, feel free to ask.


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 ControlsAutoGenerator
{
	public partial class Form1 : Form
	{
		private DataTable table = new DataTable();

		public Form1()
		{
			InitializeComponent();
			InitDataTable();
			PopulateTable();

			dataGridView1.DataSource = table;
		}

		private void InitDataTable()
		{
			table.Columns.Add("Id");
			table.Columns.Add("Name");
			table.Columns.Add("Surname");
			table.Columns.Add("Age");
		}

		private void PopulateTable()
		{
			table.Rows.Add(1, "John", "Doe", 26);
			table.Rows.Add(2, "Jane", "Doe", 30);
			table.Rows.Add(3, "Marry", "Anne", 15);
		}

		private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyValue == 9)
			{
				if (dataGridView1.CurrentCell.ColumnIndex == 0)
				{
					dataGridView1[2, dataGridView1.CurrentRow.Index].Selected = true;
					dataGridView1.CurrentCell = dataGridView1[2, dataGridView1.CurrentRow.Index];
					e.Handled = true;
				}
			}
		}
	}
}




Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Manage TabIndex for DataGridView

Posted 07 January 2010 - 08:10 AM

Even nicer solution would be to create two enums like this:

private enum ColumnsEnum
{
	ID = 0,
	NAME = 1,
	SURNAME = 2,
	AGE = 3
}

private enum KeysEnum
{
	TAB = 9
}



And then in KeyDown event call the code like this:

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
	if (e.KeyValue == (int)KeysEnum.TAB)
	{
		if (dataGridView1.CurrentCell.ColumnIndex == (int)ColumnsEnum.ID)
		{
			dataGridView1[(int)ColumnsEnum.SURNAME, dataGridView1.CurrentRow.Index].Selected = true;
			dataGridView1.CurrentCell = dataGridView1[(int)ColumnsEnum.SURNAME, dataGridView1.CurrentRow.Index];
			e.Handled = true;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 samipshr@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 26-July 09

Re: Manage TabIndex for DataGridView

Posted 11 January 2010 - 03:19 AM

HI Flash M,
I try to do in KeyDown event , but i faced the same problem, the next focus column was colulmn5. But when Column 4 is filled and click tab .....next focus cell was column 10. Its not what i wanted.

So instead of overriding KeyDown, i override KeyUp, it worked for me. But even in KeyUp event , after i fill column 4 , next focus column was 5 and then it finally focus on to column 10....

Here the code i wrote:
 protected override void onkeyup(KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Tab)
			{
				this.ClearSelection();
				int CurrentColumn, CurrentRow;
				CurrentColumn = this.CurrentCell.ColumnIndex;
				CurrentRow = this.CurrentCell.RowIndex;

				if (CurrentColumn == 10 && CurrentRow != this.Rows.Count - 1)
				{
					this.CurrentCell = this.Rows[CurrentRow + 1].Cells[3];
				}
				else
				{
					if (this.CurrentCell.ColumnIndex == 3 || this.CurrentCell.ColumnIndex == 4 )
					{
						this.CurrentCell = this.CurrentRow.Cells[9];
					}
					else
					{
						this.CurrentCell = this.CurrentRow.Cells[3];
					}
				}
				e.Handled = true;
			}
			base.onkeyup(e);
		}



later i got the answer in another forum where i posted my issue.

"when you edit a cell in the dataGridView, there maybe another control for your editing, so we could not get the Tab key event in KeyUp or keydown method. So we need to override ProcessCmdKey method to handle the Tab message."
Here is the code, it is very short.

	protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			if (keyData == Keys.Tab && dgvBaking.CurrentCell.ColumnIndex == 3)
			{
				dgvBaking.CurrentCell = dgvBaking.Rows[this.dgvBaking.CurrentCell.RowIndex].Cells[9];
				return true;
			}
			else if (keyData == Keys.Tab && dgvBaking.CurrentCell.ColumnIndex == 9 && dgvBaking.CurrentCell.RowIndex < dgvBaking.Rows.Count - 1)
			{
				dgvBaking.CurrentCell = dgvBaking.Rows[dgvBaking.CurrentCell.RowIndex + 1].Cells[3];
				return true;
			}
			else
				return base.ProcessCmdKey(ref msg, keyData);
		}



Thanks. Hope this willl help others

This post has been edited by samipshr@gmail.com: 11 January 2010 - 03:37 AM

Was This Post Helpful? 1
  • +
  • -

#5 raagav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-September 11

Re: Manage TabIndex for DataGridView

Posted 03 September 2011 - 05:39 AM

hi samipshr@gmail.com ...

thank you so much. I hv been looking for this for the past 2 weeks now i found with ur help. thanks buddy ...
the working of ProcessCmdKey() override method was clear, but
i want to know some brief explanation about its Base class ...can u do so ?

This post has been edited by raagav: 03 September 2011 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1