In following code u may try
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace TestWin
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection();
List<int> buttonPosition = new List<int>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
buttonPosition.Add(2); //the position in the column where the button should appear in the table
loadTable(); // load the table
}
public static void displayTable(DataGridView table, string query, List<int> b)
{
try {
DataSet dataset = new DataSet();
DataTable empTable = new DataTable();
DataRow dr;
empTable.Columns.Add(new DataColumn("Id"));
empTable.Columns.Add(new DataColumn("Name"));
empTable.Columns.Add(new DataColumn("Mobile"));
dr = empTable.NewRow();
dr["Id"] = "A";
dr["Name"] = "A....";
dr["Mobile"] = "12345";
empTable.Rows.Add(dr);
dr = empTable.NewRow();
dr["Id"] = "B";
dr["Name"] = "B....";
dr["Mobile"] = "12345656";
empTable.Rows.Add(dr);
dr = empTable.NewRow();
dr["Id"] = "C";
dr["Name"] = "C....";
dr["Mobile"] = "123343445";
empTable.Rows.Add(dr);
dataset.Tables.Add(empTable);
table.RowHeadersVisible = false;
//table.AutoGenerateColumns = true;
table.DataSource = dataset.Tables[0].DefaultView;
table.MultiSelect = false;
table.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
table.AllowUserToResizeRows = false;
table.AllowUserToResizeColumns = false;
table.AllowUserToAddRows = false;
table.ReadOnly = true;
if (b.Count > 0)
{
DataGridViewButtonColumn button = new DataGridViewButtonColumn();
button.Text = "hahaa";
button.HeaderText = "A Button";
button.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
button.Width = 30;
button.UseColumnTextForButtonValue = true;
table.Columns.Insert(b.ElementAt(0), button);
}
}catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
private void loadTable()
{
displayTable(dataGridView1, "select * from tbl_itemprice", buttonPosition);
}
private void tableCode_CellClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(dataGridView1[2, e.RowIndex].Value.ToString());
}
}
}
In line
MessageBox.Show(dataGridView1[2, e.RowIndex].Value.ToString());
in place of 2 u may pass index of any colnm of which value u want to dispay (0,1,2 or 3)