Handle Buttons Click Event in DatagridView

No.of Views2700
Bookmarked1 times
Downloads 
Votes0
By  kirtan007   On  16 Feb 2010 00:02:48
Tag : CSharp , Windows Forms
Handle Buttons Click Event in DatagridView
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Background :

Some time user need to do Something like want to Delete row by Clicking Button inside the DataGridView.

Lets Start

Fisrt of All Drag one DataGridView With Some Column inside it You can do it by "Smart Tag" at the Right Hand Side Corner

at Form's Load Event Add Another ColumnThat Will have Button inside it

to do So

{codecitation class="brush: csharp; gutter: true;" width="650px"}

  private void Form1_Load(object sender, EventArgs e)
{
// Create New instance of the Column
DataGridViewButtonColumn bcol = new DataGridViewButtonColumn();

// this will be the Header of the New Added Column
bcol.HeaderText = "Button Column ";
// Text Displayed on the Button
bcol.Text = "Click Me";
bcol.Name = "btnClickMe";
// This Property will use Column Text as Button's Text Property
bcol.UseColumnTextForButtonValue = true;
//Finaly Add Column to the DataGridView
dataGridView1.Columns.Add(bcol);
}


{/codecitation}

Now We have Added Column with Button in it in DataGridView Now We Want to Handle its Click Event

Lets See how to do that

{codecitation class="brush: csharp; gutter: true;" width="650px"}

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{

//StateMents you Want to execute to Get Data
}
}


{/codecitation}

In Above Code we handle the Buttons Click event By CellContent Click So We have to Take Care
Which Cell's Content Was Click

We Do so by
DataGridViewCellEventArgs e 

Now we have Button Column in 2nd Column of the DataGridView So

We have to Execute code if Cell is from Second column

so

{codecitation class="brush: csharp; gutter: true;" width="650px"}

if ( e.ColumnIndex == 1)
{

// Code to Execute When Button is Clicked
}

{/codecitation}

Thats it happy coding
Thank You

About the Author



Kirtan Patel
- Bachelor of Computer Application .
- File Handling with C#
- Registry Programming With C#.
- Knowledge of Virus Programming .
- Working With third party controls like Telerik and Component Factory .
- P/invoke Win32 API.
- SQL Server Database Applications
- Client/Server Database Programming
Expertise in : C# , Asp.net ,Vb.net , SQL Server , Designing Elegant look Web interface
Familiar With : C/C++ , Core Java , Java Script , Dream weaver templates , fire Works ,PHP ,Photo shop
and Graphics
Databases: Microsoft SQL Server 2005 , Microsoft Access and Some MySQL
Occupation: Student

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top