DataGridViewButtonColumn in C#

Rajesh081725
Posted by Rajesh081725 under C# category on | Points: 40 | Views : 3303
using System;
using System.Windows.Forms;
using System.Drawing;

namespace DataGridView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btn_Click(object sender, EventArgs e)
{
myDataGridView.Columns.Clear();

myDataGridView.ColumnCount = 2;
myDataGridView.Columns[0].Name = "User ID";
myDataGridView.Columns[1].Name = "Password";

string[] row = new string[] { "abc", "abc"};
myDataGridView.Rows.Add(row);
row = new string[] { "pqr", "pqr"};
myDataGridView.Rows.Add(row);
row = new string[] { "ghanashyam", "ghanashyam"};
myDataGridView.Rows.Add(row);
row = new string[] { "jignesh", "jignesh"};
myDataGridView.Rows.Add(row);

DataGridViewButtonColumn dgvButton = new DataGridViewButtonColumn();

dgvButton.FlatStyle= FlatStyle.System;

dgvButton.HeaderText = "Button";
dgvButton.Name = "Button";
dgvButton.UseColumnTextForButtonValue = true;
dgvButton.Text = "New";

myDataGridView.Columns.Add(dgvButton);
}

private void myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
//Write here your code...
MessageBox.Show("You Have Selected " + (e.RowIndex + 1).ToString() + " Row Button");
}
}
}
}

Comments or Responses

Posted by: T.Saravanan on: 11/15/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Rajesh,

In your code, what is "myDataGridView"? Can you please explain your code?
Without explanation, viewers cannot understand uses of this code.

Please post your code inside the code tag.

Login to post response