1

I've found more articles that I can shake a stick at regarding the function being called, but there is nothing that shows how to declare this event and call it. Here is what I am using for the function:

void handler_dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

Here is what I am trying to accomplish:

dataGridView1.CellStateChanged += new System.Windows.Forms.DataGridViewCellStateChangedEventHandler(dataGridView1.CellStateChanged += new System.Windows.Forms.DataGridViewCellStateChangedEventHandler(handler_dataGridView1_CellFormatting(this.dataGridView1, ***what goes here?***));

Any help greatly appreciated, thanks!

Lord Helmet
  • 150
  • 14
  • 3
    it's just `dataGridView1.CellStateChanged += handler_dataGridView1_CellFormatting;` – LarsTech Jan 15 '16 at 16:54
  • You don't call it directly - the framework does it for you. You have to attach the handler to the object, and @LarsTech gives an example of how to do it. – Tim Jan 15 '16 at 17:20
  • It doesn't like that, either. I am pretty certain that I need to pass some arguments. – Lord Helmet Jan 15 '16 at 17:23
  • You don't call it. With the line a gave you, you told the grid to use that block of code whenever it needs to perform the CellFormatting event. – LarsTech Jan 15 '16 at 17:24
  • @Lars Tech: It kept giving me an error, but when I told it to autogenerate it then worked. They are spelled identically, so I am not sure why it didn't honor my function. I'll just move that code. I am learning C# and Visual Studio at the same time, so this has been a learning experience. Can you post your original comment as an answer so that I can mark it? Thanks! – Lord Helmet Jan 15 '16 at 17:29
  • @RyanHaley - In the future, it is of great help if you tell us what the error is that you get. – Tim Jan 15 '16 at 17:32

2 Answers2

1

You don't pass any parameters when you wire up the event. It should just be:

dataGridView1.CellStateChanged += handler_dataGridView1_CellFormatting;

Most developers would probably prefer the shortened version:

dataGridView1.CellStateChanged += dataGridView1_CellFormatting;

You would have to then create the code block:

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
  // do your formatting here with the information provided in the e variable.
}

If you just type dataGridView1.CellStateChanged += followed by the Tab key twice, Visual Studio will automatically create that code block for you.

Alternatively, you can use the Designer to wire up and create the code block for you, too, by clicking on the lightning bolt icon in the Properties box and double-clicking on one of the listed events.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
0

If you double click in the properties window on the event, the handler is hooked up and looks like this:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{

}
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • That's not the part I am having trouble with, it's the next piece, which attaches this function to the dataGridView. There's nothing that says what the "DataGridViewCellFormattingEventArgs" actually are. I've tried passing all sorts of stuff, and it hates everything I try to pass it. – Lord Helmet Jan 15 '16 at 17:10
  • YOU do not call the function. It is called by the DataGridView – Steve Wellens Jan 15 '16 at 21:15