Hi satabeach,
Check this example. Now please take its reference and correct your code.
C#
using System.Data;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.CenterToScreen();
}
private void Form1_Load(object sender, EventArgs e)
{
this.BindGrid();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
if ((e.RowIndex + 2) < dataGridView1.Rows.Count)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex + 2];
MessageBox.Show("Name: " + row.Cells[0].Value + Environment.NewLine + "Quantity: " + row.Cells[1].Value);
}
}
}
private void BindGrid()
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.Clear();
DataGridViewColumn name = new DataGridViewTextBoxColumn();
name.HeaderText = "Name";
name.Name = "Name";
name.DataPropertyName = "Name";
name.Width = 75;
dataGridView1.Columns.Insert(0, name);
DataGridViewColumn country = new DataGridViewTextBoxColumn();
country.Name = "Quantity";
country.HeaderText = "Quantity";
country.DataPropertyName = "Quantity";
country.Width = 50;
dataGridView1.Columns.Insert(1, country);
dataGridView1.DataSource = null;
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Quantity");
dt.Rows.Add("Apple", 15);
dt.Rows.Add("Mango", 20);
dt.Rows.Add("Banana", 10);
dt.Rows.Add("Orange", 12);
dataGridView1.DataSource = dt;
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.HeaderText = "";
buttonColumn.Width = 40;
buttonColumn.Name = "buttonColumn";
buttonColumn.Text = "View";
buttonColumn.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Insert(2, buttonColumn);
}
}
VB.Net
Imports System.Data
Public Class Form1
Public Sub New()
InitializeComponent()
Me.CenterToScreen()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.BindGrid()
End Sub
Private Sub dataGridView1_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dataGridView1.CellContentClick
If e.ColumnIndex = 2 Then
If (e.RowIndex + 2) < dataGridView1.Rows.Count Then
Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex + 2)
MessageBox.Show("Name: " & row.Cells(0).Value + Environment.NewLine & "Quantity: " + row.Cells(1).Value)
End If
End If
End Sub
Private Sub BindGrid()
dataGridView1.AllowUserToAddRows = False
dataGridView1.Columns.Clear()
Dim name As DataGridViewColumn = New DataGridViewTextBoxColumn()
name.HeaderText = "Name"
name.Name = "Name"
name.DataPropertyName = "Name"
name.Width = 75
dataGridView1.Columns.Insert(0, name)
Dim country As DataGridViewColumn = New DataGridViewTextBoxColumn()
country.Name = "Quantity"
country.HeaderText = "Quantity"
country.DataPropertyName = "Quantity"
country.Width = 50
dataGridView1.Columns.Insert(1, country)
dataGridView1.DataSource = Nothing
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Name")
dt.Columns.Add("Quantity")
dt.Rows.Add("Apple", 15)
dt.Rows.Add("Mango", 20)
dt.Rows.Add("Banana", 10)
dt.Rows.Add("Orange", 12)
dataGridView1.DataSource = dt
Dim buttonColumn As DataGridViewButtonColumn = New DataGridViewButtonColumn()
buttonColumn.HeaderText = ""
buttonColumn.Width = 40
buttonColumn.Name = "buttonColumn"
buttonColumn.Text = "View"
buttonColumn.UseColumnTextForButtonValue = True
dataGridView1.Columns.Insert(2, buttonColumn)
End Sub
End Class
Screenshot