Click here to Skip to main content
15,881,898 members
Home / Discussions / C#
   

C#

 
QuestionRecommendation Pin
John L. DeVito1-May-06 15:05
professionalJohn L. DeVito1-May-06 15:05 
AnswerRe: Recommendation Pin
Michael A. Barnhart1-May-06 15:17
Michael A. Barnhart1-May-06 15:17 
AnswerRe: Recommendation Pin
Al Ortega1-May-06 15:56
Al Ortega1-May-06 15:56 
AnswerRe: Recommendation Pin
Joe Woodbury1-May-06 18:14
professionalJoe Woodbury1-May-06 18:14 
QuestionChanging Order of Items in a Bound ListBox Pin
myNameIsRon1-May-06 12:25
myNameIsRon1-May-06 12:25 
AnswerRe: Changing Order of Items in a Bound ListBox Pin
Josh Smith2-May-06 1:30
Josh Smith2-May-06 1:30 
GeneralRe: Changing Order of Items in a Bound ListBox Pin
myNameIsRon2-May-06 16:05
myNameIsRon2-May-06 16:05 
QuestionData Grid - Setting Current Column and arrow key capture Pin
Michael A. Barnhart1-May-06 12:15
Michael A. Barnhart1-May-06 12:15 
I am experimenting with the datagrid and have not found two issues (they are related so if I find one, I may have a working solution.)

One is I can not find what keydown event I need to handle to capture the arrow keys. The left and right arrows are events in the individual cells if you are moving the cursor along the characters (in a textbox) but the up and down or the left and right are not captured when moving which cell is selected. They are also not key events of the datagrid. What item/component are the arrow key down events associated with?

Alternately I could handle the current cell changing and keep track of the last and next cells. For then moving over a row I do not want selected I can set the selected row by using the datagrid.CurrentRowIndex. Which works fine for rows, but I find no equivalent for columns. Any suggestions on setting the current column?

Thanks for the help. Here is my sample dialg box code if any one can make use of the handling I have so far.

using System;<br />
using System.Drawing;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
using System.Data;<br />
<br />
namespace TestDlg<br />
{<br />
	/// <summary><br />
	/// Summary description for Form1.<br />
	/// </summary><br />
	public class Form1 : System.Windows.Forms.Form<br />
	{<br />
		private System.Windows.Forms.Button PressMe;<br />
		private System.Windows.Forms.DataGrid m_dataGrid;<br />
		private System.Data.DataSet m_dataSet;<br />
		private System.Data.DataTable m_dataTable;<br />
<br />
		private System.Windows.Forms.DataGridCell m_CurCell;<br />
		/// <summary><br />
		/// Required designer variable.<br />
		/// </summary><br />
		private System.ComponentModel.Container components = null;<br />
<br />
		public Form1()<br />
		{<br />
			//<br />
			// Required for Windows Form Designer support<br />
			//<br />
			InitializeComponent();<br />
<br />
			//<br />
			// TODO: Add any constructor code after InitializeComponent call<br />
			//<br />
			m_dataTable = new DataTable("test");<br />
			m_dataTable.Columns.Add("Name");<br />
			m_dataTable.Columns.Add("Col-1");<br />
			m_dataTable.Columns.Add("Col-2");<br />
			// Add The Style and Assing table to grid.<br />
			this.SetTableStyle(m_dataGrid,m_dataTable);<br />
<br />
			string[] row = new string[3] {"R-name","11111","22222"};<br />
<br />
			m_dataTable.Rows.Add(row);<br />
			m_dataTable.Rows.Add(row);<br />
			m_dataTable.Rows.Add(row);<br />
			m_dataTable.Rows.Add(row);<br />
			m_dataTable.Rows.Add(row);<br />
<br />
		}<br />
<br />
		/// <summary><br />
		/// Clean up any resources being used.<br />
		/// </summary><br />
		protected override void Dispose( bool disposing )<br />
		{<br />
			if( disposing )<br />
			{<br />
				if (components != null) <br />
				{<br />
					components.Dispose();<br />
				}<br />
			}<br />
			base.Dispose( disposing );<br />
		}<br />
<br />
		#region Windows Form Designer generated code<br />
		/// <summary><br />
		/// Required method for Designer support - do not modify<br />
		/// the contents of this method with the code editor.<br />
		/// </summary><br />
		private void InitializeComponent()<br />
		{<br />
			this.PressMe = new System.Windows.Forms.Button();<br />
			this.m_dataGrid = new System.Windows.Forms.DataGrid();<br />
			((System.ComponentModel.ISupportInitialize)(this.m_dataGrid)).BeginInit();<br />
			this.SuspendLayout();<br />
			// <br />
			// PressMe<br />
			// <br />
			this.PressMe.Location = new System.Drawing.Point(608, 24);<br />
			this.PressMe.Name = "PressMe";<br />
			this.PressMe.Size = new System.Drawing.Size(80, 64);<br />
			this.PressMe.TabIndex = 0;<br />
			this.PressMe.Text = "PressMe";<br />
			// <br />
			// m_dataGrid<br />
			// <br />
			this.m_dataGrid.DataMember = "";<br />
			this.m_dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;<br />
			this.m_dataGrid.Location = new System.Drawing.Point(16, 16);<br />
			this.m_dataGrid.Name = "m_dataGrid";<br />
			this.m_dataGrid.Size = new System.Drawing.Size(552, 312);<br />
			this.m_dataGrid.TabIndex = 1;<br />
			this.m_dataGrid.KeyDown += new System.Windows.Forms.KeyEventHandler(this.m_dataGrid_KeyDown);<br />
			this.m_dataGrid.MouseDown += new System.Windows.Forms.MouseEventHandler(this.m_dataGrid_MouseDown);<br />
			this.m_dataGrid.Click += new System.EventHandler(this.m_dataGrid_Click);<br />
			this.m_dataGrid.CurrentCellChanged += new System.EventHandler(this.m_dataGrid_CurrentCellChanged);<br />
<br />
			// <br />
			// Form1<br />
			// <br />
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);<br />
			this.ClientSize = new System.Drawing.Size(696, 341);<br />
			this.Controls.Add(this.m_dataGrid);<br />
			this.Controls.Add(this.PressMe);<br />
			this.Name = "Form1";<br />
			this.Text = "TestDlg";<br />
			((System.ComponentModel.ISupportInitialize)(this.m_dataGrid)).EndInit();<br />
			this.ResumeLayout(false);<br />
<br />
		}<br />
		#endregion<br />
<br />
		/// <summary><br />
		/// The main entry point for the application.<br />
		/// </summary><br />
		[STAThread]<br />
		static void Main() <br />
		{<br />
			Application.Run(new Form1());<br />
		}<br />
<br />
		private void SetTableStyle(DataGrid dg, DataTable dt)<br />
		{<br />
			try<br />
			{<br />
				DataGridTableStyle ts = new DataGridTableStyle();<br />
				ts.HeaderForeColor = Color.Black;  <br />
				ts.HeaderFont = new Font("Verdana", (float)8.25);   <br />
				ts.MappingName = dt.TableName;<br />
				ts.BackColor = Color.Linen;<br />
				ts.AlternatingBackColor = Color.Aquamarine;<br />
            <br />
				DataGridColumnStyle cs;<br />
				DataGridTextBoxColumn tbc;<br />
<br />
				foreach (System.Data.DataColumn col in dt.Columns)<br />
				{<br />
					if(col.ColumnName.CompareTo("Name")==0)<br />
					{<br />
						cs = new DataGridTextBoxColumn();<br />
						tbc = (DataGridTextBoxColumn) cs;<br />
						cs.ReadOnly=true;<br />
					}<br />
					else if(col.DataType.ToString().CompareTo("System.String")==0)<br />
					{<br />
						cs = new DataGridTextBoxColumn();<br />
						tbc = (DataGridTextBoxColumn) cs;<br />
						tbc.TextBox.KeyDown += new KeyEventHandler(m_curCell_KeyDown);<br />
						tbc.TextBox.MouseDown += new MouseEventHandler(m_curCell_MouseDown);<br />
						tbc.TextBox.Click += new EventHandler(m_curCell_Click);<br />
					}<br />
					else if(col.DataType.ToString().CompareTo(DbType.Boolean.ToString())==0)<br />
					{<br />
						cs = new DataGridBoolColumn();<br />
						((DataGridBoolColumn)cs).AllowNull = false;<br />
					}<br />
					else <br />
					{<br />
						cs = new DataGridTextBoxColumn();<br />
						tbc = (DataGridTextBoxColumn) cs;<br />
					}<br />
					cs.HeaderText = col.ColumnName;<br />
					cs.MappingName = col.ColumnName;<br />
					ts.GridColumnStyles.Add(cs);<br />
                    if(col.ColumnName.CompareTo("Name")==0) cs.ReadOnly=true;<br />
				}<br />
<br />
				dg.TableStyles.Clear();<br />
				dg.TableStyles.Add(ts);<br />
				dg.DataSource = dt;<br />
<br />
			}<br />
			catch ( Exception e )<br />
			{<br />
				throw e;<br />
			}<br />
		}<br />
<br />
<br />
		private void m_dataGrid_Click(object sender, System.EventArgs e)<br />
		{<br />
			int curr = m_dataGrid.CurrentRowIndex;<br />
		}<br />
<br />
		private void m_dataGrid_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)<br />
		{<br />
			int curr = m_dataGrid.CurrentRowIndex;<br />
		}<br />
<br />
		private void m_dataGrid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)<br />
		{<br />
			int curr = m_dataGrid.CurrentRowIndex;<br />
		}<br />
<br />
		private void m_dataGrid_CurrentCellChanged(object sender, System.EventArgs e)<br />
		{<br />
			int curr = m_dataGrid.CurrentRowIndex;<br />
			curr = m_dataGrid.CurrentCell.RowNumber;<br />
			if(curr==0) m_dataGrid.CurrentRowIndex = 1;<br />
			int curc = m_dataGrid.CurrentCell.ColumnNumber;<br />
			<br />
			m_CurCell = m_dataGrid.CurrentCell;<br />
		}<br />
<br />
		private void m_curCell_Click(object sender, System.EventArgs e)<br />
		{<br />
			int curr = m_dataGrid.CurrentRowIndex;<br />
			<br />
		}<br />
<br />
		private void m_curCell_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)<br />
		{<br />
			int curr = m_dataGrid.CurrentRowIndex;<br />
		}<br />
<br />
		private void m_curCell_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)<br />
		{<br />
			int curr = m_dataGrid.CurrentRowIndex;<br />
		<br />
		}<br />
	<br />
	<br />
	}<br />
}


"Every new day begins with possibilities. It's up to us to fill it with things that move us toward progress and peace.” (Ronald Reagan)
QuestionDLL question Pin
Tom Wright1-May-06 12:12
Tom Wright1-May-06 12:12 
AnswerRe: DLL question Pin
Robin Panther1-May-06 13:07
Robin Panther1-May-06 13:07 
QuestionPassportIdentity.Compress - ?? Pin
BlackDice1-May-06 11:23
BlackDice1-May-06 11:23 
GeneralRe: PassportIdentity.Compress - ?? Pin
Office Lineman1-May-06 13:53
Office Lineman1-May-06 13:53 
GeneralRe: PassportIdentity.Compress - ?? Pin
BlackDice2-May-06 5:28
BlackDice2-May-06 5:28 
QuestionEnumerator question Pin
jbricker771-May-06 9:28
jbricker771-May-06 9:28 
AnswerRe: Enumerator question Pin
Josh Smith1-May-06 9:32
Josh Smith1-May-06 9:32 
GeneralRe: Enumerator question Pin
jbricker772-May-06 2:48
jbricker772-May-06 2:48 
AnswerRe: Enumerator question Pin
Robin Panther1-May-06 12:57
Robin Panther1-May-06 12:57 
GeneralRe: Enumerator question Pin
jbricker772-May-06 3:22
jbricker772-May-06 3:22 
AnswerRe: Enumerator question Pin
jbricker772-May-06 3:37
jbricker772-May-06 3:37 
AnswerRe: Enumerator question Pin
jbricker772-May-06 8:46
jbricker772-May-06 8:46 
QuestionPainting an ActiveX Control to an Image Pin
kingphotis1-May-06 8:41
kingphotis1-May-06 8:41 
AnswerRe: Painting an ActiveX Control to an Image Pin
Judah Gabriel Himango1-May-06 9:34
sponsorJudah Gabriel Himango1-May-06 9:34 
GeneralRe: Painting an ActiveX Control to an Image Pin
kingphotis1-May-06 9:42
kingphotis1-May-06 9:42 
QuestionHow to refresh Win Form [C# 2.0 ] Pin
student_rhr1-May-06 8:04
student_rhr1-May-06 8:04 
AnswerRe: How to refresh Win Form [C# 2.0 ] Pin
Josh Smith1-May-06 8:17
Josh Smith1-May-06 8:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.