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

标签 c# datagridview

我刚接触 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/

相关文章:

c# - DataGridView 上的自定义排序

c# - 数据 GridView C#

c# - 用户控件包含子控件,设计模式编辑子控件

c# - 正则表达式删除特殊字符

c# - 从 C# 中的组合框中删除特定项目的所有实例

c# - 在类之间编写共享枚举的最佳实践是什么

c# - Unity Survival Shooter Enemy 不受伤害 - Android

C# 任何人都明白为什么这不能正确选择 datagridview 中的行

.net - 如何在 DataGridView 中选择多个单元格

c# - 使用 DataSource 属性在 DataGridView 中排序