If you want to get cell value from DataGridview then first to bind grid with data table with any data source. Now, use this code to retrieve the cell value.
{
con.Open();
cmd.CommandText = "select * from [student_record]";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].ToString ();
}
{
string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
MessageBox.Show(name);
Binding Data
private void binddata(){
con.Open();
cmd.CommandText = "select * from [student_record]";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].ToString ();
}
Get cell value on message box
private void button1_Click(object sender, EventArgs e){
string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
MessageBox.Show(name);