Reputation:
I am fresher in c# language .Now my project is to fetch the data from database to datagridview and add edit and delete column.
i had a 5 field(id,name,degree,college,city) in the table student
here my code:
MySqlConnection connection = new MySqlConnection("SERVER=hostaddress","DATABASE=DTBS","UID=UID","PASSWORD=PWDS");
MySqlCommand command = new MySqlCommand("SELECT * from student;", connection);
connection.Open();
DataTable dataTable = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(command);
da.Fill(dataTable);
dataGridView1.DataSource = dataTable;
now i need the help on adding an edit and delete column on the datagridview. when i clicking an edit on datagridview i need to get that rows data on my form 2 i did not understand what is it and how can i do it. i am searching more on google. but i did not get an clear explanation about it help me in some coding.
Upvotes: 3
Views: 11222
Reputation: 1004
Try this coding:
String MyConnection = "SERVER=********;" +
"DATABASE=dtabs;" +
"UID=usrname;" +
"PASSWORD=pswrd;" + "Convert Zero Datetime = True";
public string id { get; private set; }
private void Form1_Load(object sender, EventArgs e)
{
data();
//Edit link
DataGridViewLinkColumn Editlink = new DataGridViewLinkColumn();
Editlink.UseColumnTextForLinkValue = true;
Editlink.HeaderText = "Edit";
Editlink.DataPropertyName = "lnkColumn";
Editlink.LinkBehavior = LinkBehavior.SystemDefault;
Editlink.Text = "Edit";
dataGridView1.Columns.Add(Editlink);
//Delete link
DataGridViewLinkColumn Deletelink = new DataGridViewLinkColumn();
Deletelink.UseColumnTextForLinkValue = true;
Deletelink.HeaderText = "delete";
Deletelink.DataPropertyName = "lnkColumn";
Deletelink.LinkBehavior = LinkBehavior.SystemDefault;
Deletelink.Text = "Delete";
dataGridView1.Columns.Add(Deletelink);
}
//Make it as public for that only we call data() in form 2
public void data()
{
MySqlConnection connection = new MySqlConnection(MyConnection);
MySqlCommand command = new MySqlCommand("SELECT * from student;", connection);
connection.Open();
DataTable dataTable = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(command);
da.Fill(dataTable);
dataGridView1.DataSource = dataTable;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.Refresh();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MySqlConnection conn = new MySqlConnection(MyConnection);
conn.Open();
//edit column
if (e.ColumnIndex == 5)
{
id = Convert.ToString(dataGridView3.Rows[e.RowIndex].Cells["id"].Value);
Form2 frm2 = new Form3(this);
fm2.a = id;
fm2.Show();
dataGridView1.Refresh();
//delete column
if (e.ColumnIndex == 6)
{
id = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["id"].Value);
MySqlDataAdapter da = new MySqlDataAdapter("delete from student where id = '" + id + "'", conn);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.Refresh();
}
}
FORM 2:
public partial class Form2 : Form
{
// for this we can reload after closing the form 2 the datagridview get refresh
Form1 _owner;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 owner)
{
InitializeComponent();
_owner = owner;
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);
}
String MyCon = "SERVER=*******;" +
"DATABASE=dtbas;" +
"UID=userid;" +
"PASSWORD=paswrd;" + "Convert Zero Datetime = True";
public string a
{
get { return txtid.Text; }
set { txtid.Text = value; }
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
_owner.data();
}
private void Form2_Load(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(MyCon);
con.Open();
MySqlCommand Com = new MySqlCommand("Select * from student where id ='" + txtid.Text + "'", con);
MySqlDataReader dt = Com.ExecuteReader();
if (dt.Read())
{
// i assume (id textBox as txtid),(name textbox as txtname),(degree textbox as txtdegree),(college textbox as txtcollege),(city textbox as txtcity)
txtid.Text = dt.GetValue(0).ToString();
txtname.Text = dt.GetValue(1).ToString();
txtdegree.Text = dt.GetValue(2).ToString();
txtcollege.Text = dt.GetValue(3).ToString();
txtcity.Text = dt.GetValue(4).ToString();
con.Close();
}
//button in form2 to save it in the database. (button as btnsave)
private void btnsave_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(MyCon);
con.Open();
string query = string.Format("Update student set id='" + txtid.Text + "' , name='" + txtname.Text + "' , degree='" + txtdegree.Text + "' , college='" + txtcollege.Text + "' , city='" + txtcity.Text + "'where id='" + txtid.Text + "'");
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.ExecuteNonQuery();
}
}
}
Refrence:http://www.dotnetsharepoint.com/2013/07/how-to-add-edit-and-delete-buttons-in.html
it helps you.
Upvotes: 4