c#中datagridview列中的Edit and Delete选项
本文关键字:and Delete 选项 Edit datagridview | 更新日期: 2023-09-27 18:28:32
我对c#语言比较熟悉。现在我的项目是从数据库中提取数据到datagridview,并添加编辑和删除列。
我在表上有5个字段(身份、姓名、学位、学院、城市)
这是我的代码:
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;
现在我需要在datagridview上添加编辑和删除列的帮助。当我在datagridview上点击一个编辑时,我需要在我的表单2上获得行数据。我不明白它是什么,我该怎么做。我正在谷歌上搜索更多。但我没有得到一个明确的解释,这对我的一些编码有帮助。
尝试以下编码:
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();
}
}
表格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();
}
}
}
参考:http://www.dotnetsharepoint.com/2013/07/how-to-add-edit-and-delete-buttons-in.html
它对你有帮助。