gpt4 book ai didi

c# - c#中datagridview列中的编辑和删除选项

转载 作者:行者123 更新时间:2023-11-30 15:23:21 25 4
gpt4 key购买 nike

我刚接触 C# 语言。现在我的项目是从数据库中获取数据到 datagridview 并添加编辑和删除列。

我在 student 表中有一个 5 字段(id,name,degree,college,city)

这里是我的代码:

        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

它对你有帮助。

关于c# - c#中datagridview列中的编辑和删除选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34545139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com