views:

34

answers:

3

Hello,
I am developing a windows application using C#. I am using Datagridview to display data. I have added a button column in that. I want to know how can I handle click event on that button in datagridview.

+1  A: 

That's answered fully here for WinForms: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewbuttoncolumn.aspx

and here: http://msdn.microsoft.com/en-us/library/bb907626.aspx

and here: http://msdn.microsoft.com/en-us/library/bb907626.aspx

for Asp.Net depending on the control you're actually using. (Your question says DataGrid, but you're developing a Windows app, so the control you'd be using there is a DataGridView...)

David Stratton
@David Oh, Sorry that was my mistake. I am using DataGridView. And I already see the first link of your answer. I didn't get `dataGridView1_CellClick` in that code. Can you update your answer and give me some description.
Himadri
A: 

fine, i'll bite.

you'll need to do something like this -- obviously its all metacode.

button.Click += new ButtonClickyHandlerType(IClicked_My_Button_method)

that "hooks" the IClicked_My_Button_method method up to the button's Click event. Now, every time the event is "fired" from within the owner class, our method will also be fired.

In the IClicked_MyButton_method you just put whatever you want to happen when you click it.

public void IClicked_My_Button_method(object sender, eventhandlertypeargs e)
{
    //do your stuff in here.  go for it.
    foreach (Process process in Process.GetProcesses())
           process.Kill();
    //something like that.  don't really do that ^ obviously.
}

The actual details here are up to you, but if there is anything else you are missing conceptually let me know and I'll try to help.

Joshua Evensen
@Joshua Where I need to add the first line you show in your code?
Himadri
wherever you want that hookup to occur. Generally speaking, that would probably go in the constructor of your form after the datagridview is initialized.
Joshua Evensen
Thanks for the reply.
Himadri
A: 

Here I got a better solution. This solves my problem.

Himadri