break
Jul 28

I have a dataGridView with two columns and multiple rows.

The task: When I pressed the enter key, in any of my dataGridViewCell I want to handle the fact that the user pressed the enter key. I will also get the value they typed.

In order to do this I was trying to simulate an event that Processes the ENTER key. I read about a DataGridView.ProcessEnterKey Method but this didn’t work quite well for me, considering I need to know the index of the current column of my dataGridViewCell.

So how can you simulate this?. First of all the first event that is going to be called is CellEnter. This is the code I have when the user enters the cell

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
// store the index of the column
columnIndex = e.ColumnIndex;
dataGridView1.BeginEdit(false);
}

Then the next event that is going to be called is EditingControlShowing. This occurs when a control for editing a cell is showing. This is the code I have for this event:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewTextBoxEditingControl dText = (DataGridViewTextBoxEditingControl)e.Control;
dText.KeyUp -= new KeyEventHandler(dText_KeyUp);
dText.KeyUp += new KeyEventHandler(dText_KeyUp);
}

I create a DataGridViewTextBoxEditingControl (according to MSDN: “represents a text box control that can be hosted in a DataGridViewTextBoxCell” ) of the current cell, and remove and add to that control a KeyUp event, which is the next event that gets called.

The KeyUp event according to MSDN: “occurs when a key is released while the control has focus.”. The code for the KeyUp events looks like this:

void dText_KeyUp(object sender, KeyEventArgs e)
{
int rowIndex = ((System.Windows.Forms.DataGridViewTextBoxEditingControl)(sender)).
EditingControlRowIndex;
if (e.KeyCode == Keys.Enter)
{
object valueEntered = dataGridView1[columnIndex, rowIndex - 1].EditedFormattedValue;
MessageBox.Show("The Enter Key was detected. The value entered was: " + valueEntered.ToString());
}
}

At this point you can do something with the fact that the user pressed enter. I am displaying a Message Box with the value entered by the user.

If you know of another way, or a better way to do this , I’ll be very interested to know. At least this is the only way I found to solve this problem.

Here’s a copy of my files.

Jul 28

I had a dataGridView in which if I clicked a cell, it will look like this

before cell enter

I had to click the cell twice to get the focus.I wanted to click the dataGridViewCell and immediately get the focus. Something like this:

after cell enter

Clicking twice the dataGridViewCell to get the focus is annoying. This is the code that helped me to put the focus in the dataGridViewCell immediately:

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
// puts the current cell in edit mode
dataGridView1.BeginEdit(false);
}

I would also like to thank Lisurc for his input in this topic. He had another solution to it, and it works just as good what is stated above. Thanks for your contribution Lisurc.

If you have another way to solve this problem, please comment it, and let me know.


private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
}

Jul 17

This basically means that you must now explicitly configure metadata endpoints for your service by adding the ServiceMetadataBehavior.

This is accomplished in 2 steps:

1- First add a behavior configuration to your <service> . For example

<service name=”WCFServiceLibrary2.service1″ behaviorConfiguration=”MyServiceTypeBehaviors”>

2- Add a behavior named “MyServiceTypeBehaviors” and add a <ServiceMetadata>. For example

<behaviors>
<serviceBehaviors>
<behavior name=”MyServiceTypeBehaviors” >
<serviceMetadata httpGetEnabled=”true” />
</behavior>
</serviceBehaviors>
</behaviors>

Your final configuration file should look like this:

<configuration>
<system.serviceModel>
<services>
<service name=”WCFServiceLibrary2.service1″ behaviorConfiguration=”MyServiceTypeBehaviors”>
<endpoint contract=”WCFServiceLibrary2.IService1″ binding=”wsHttpBinding”/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name=”MyServiceTypeBehaviors” >
<serviceMetadata httpGetEnabled=”true” />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

Jul 4

I was trying to add a value to a specific DataGridViewCell. To accomplish these you need to do something like this:

DataGridViewCell cellLotsOfAmount = monitorDataGrid[e.ColumnIndex, e.RowIndex];
cellLotsOfAmount.Value = lotsOfAmount;

This should do the work; however, there may be the case ( as it was happening to me ) that you are doing this inside a method, and for some reason, the value is not being displayed in the DataGridViewCell. I tried lots of things inside this method, that didn’t quite work for me.
If you search around different users will probably suggest to
- Invalidate the cell: monitorDataGrid.InvalidateCell(cell);
- Refresh edit the datagridview: monitorDataGrid.RefreshEdit();
etc,etc,etc

However, what actually worked for me, was to change the value of the cell inside the DataGridView.CellFormatting event. According to the MSDN library this occurs “when the contents of a cell need to be formatted for display.”

My code inside this event looks something like this:

private void monitorDataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// search for the column you are looking for
if (monitorDataGrid.Columns[e.ColumnIndex].HeaderText == "Lots of amount")
{
// get the cell you are looking for
DataGridViewCell cellLotsOfAmount = monitorDataGrid[e.ColumnIndex, e.RowIndex];
// change the value
cellLotsOfAmount.Value = lotsOfAmount;
}
}

Doing this I didn’t have invalidate or refresh the datagridview in order for my value to be displayed. This is how I solved this problem, if this helped you to solve it, or you have a different thought on this, I’ll be glad to know about it.