Kali ini saya ingin memposting bagaimana cara mengambil nilai dari datagridview dengan mengklik nilai yang ada di datagridview dan menampilkannya di form lain pada c#.
Okeh lanjut saja kita lihat contoh gambarnya
1. jalankan program
2. klik nilai pada datagridview yang akan ditampilkan
3. Dan nilai yang kita pilih tadi di datagridview akan muncul pada textbox tipekamar, nokamar, dan harga/hari
Source Code untuk contoh program diatas :
Tambahkan Class, dan berikan nama ambil pada class :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace reservasi
{
class ambil
{
static string tipe1, no1, harga1 ;
public static string tipe
{
get
{
return tipe1;
}
set
{
tipe1 = value;
}
}
public static string no
{
get
{
return no1;
}
set
{
no1 = value;
}
}
public static string harga
{
get
{
return harga1;
}
set
{
harga1 = value;
}
}
}
}
untuk FORM PEMESANAN :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace reservasi
{
public partial class Pemesanan : Form
{
public Pemesanan()
{
InitializeComponent();
}
MySqlConnection koneksi = new MySqlConnection("server=localhost;database=projek;uid=root;pwd=;");
private void btnlookup_Click(object sender, EventArgs e)
{
new TampilanKamar().ShowDialog();
textNokamar.Text = ambil.no;
textTipe.Text = ambil.tipe;
textHrg.Text = ambil.harga;
}
}
}
untuk FORM TAMPILANKAMAR :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace reservasi
{
public partial class TampilanKamar : Form
{
public TampilanKamar()
{
InitializeComponent();
}
MySqlConnection koneksi = new MySqlConnection("server=localhost;database=projek;uid=root;pwd=;");
private void TampilanKamar_Load(object sender, EventArgs e)
{
//menampilkan nilai pada datagridview
koneksi.Open();
MySqlCommand cmd;
cmd = koneksi.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = " select * from tb_kamar ";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
adapter.Fill(dt);
GridView1.DataSource = dt;
koneksi.Close();
}
private void GridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
ambil.nomor = GridView1.CurrentRow.Cells[1].Value.ToString();
ambil.tipe = GridView1.CurrentRow.Cells[0].Value.ToString();
ambil.harga = GridView1.CurrentRow.Cells[2].Value.ToString();
this.Close();
}
}
}
Maaf apabila ada kekurangan, Terimakasih.. Semoga Bermanfaat :) !


