【问题标题】:Edit and Delete option in datagridview column in c#c#中datagridview列中的编辑和删除选项
【发布时间】:2015-12-31 20:03:22
【问题描述】:

我对 c# 语言比较陌生。现在我的项目是从数据库中获取数据到 datagridview 并添加编辑和删除列。

我在学生表中有一个 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 上获取该行数据,我不明白它是什么以及我该怎么做。我在谷歌上搜索更多。但我没有得到明确的解释,这有助于我进行一些编码。

【问题讨论】:

  • 在form2中你包含文本框吗?
  • 是的,我在 form2 中包含 5 个文本框。

标签: c# datagridview


【解决方案1】:

试试这个编码:

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

它可以帮助你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多