Solved

Disabling of Header Click in Datagrid

Posted on 2016-10-06
2
39 Views
Last Modified: 2016-11-28
Dear Sir/Madam,

Please be kind enough to tell me how I can disable header click after adding calendar and time using numeric updown inside datagridview.

Because both time and date clicks on the Header.

Any help would be greatly appreciated.

Kind Regards,
Indunil Sanjeewa
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 41834034
Assuming this is in reference to: https:/Q_28974717.html

Add a check to ensure that the event row index is not -1 to the OnCellClick event; e.g. -
private void OnCellClick(object sender, DataGridViewCellEventArgs e)
{
	if (sender is DataGridView)
	{
		var grid = sender as DataGridView;
		// If the grid has rows and the cell that sent the event is not in the header row
		if (grid.Rows.Count > 0 && e.RowIndex != -1)
		{
			var dtp = default(DateTimePicker);
			var columns = new[] { "startdate", "starttime", "enddate", "endtime" };
			var column = grid.Columns[e.ColumnIndex].Name;
			if (columns.Any(x => x.Equals(column, StringComparison.OrdinalIgnoreCase)))
			{
				dtp = new DateTimePicker();
				grid.Controls.Add(dtp);
				dtp.Format = column.IndexOf("time", StringComparison.OrdinalIgnoreCase) > -1 ? DateTimePickerFormat.Time : DateTimePickerFormat.Short;
				if (dtp.Format.Equals(DateTimePickerFormat.Time))
					dtp.ShowUpDown = true;
				var rectangle = grid.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
				dtp.Size = new Size(rectangle.Width, rectangle.Height);
				dtp.Location = new Point(rectangle.X, rectangle.Y);
				//dtp.CloseUp += OnCloseUp;
				dtp.Leave += OnLeave;
				dtp.Validating += OnValidating;
				dtp.Visible = true;
			}
		}
	}
}

Open in new window


-saige-
1
 
LVL 14

Expert Comment

by:frankhelk
ID: 41903877
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: it_saige (https:#a41834034)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question