繁体   English   中英

使用文本框值在DataGridView C#中删除一行

[英]Delete a row in DataGridView c# using textbox value

我创建了一个datagridview,它从MySQL Workbench数据库获取所有数据。 在此datagridview下面,我创建了另一个具有相同列的datagrid。 为此,我创建了一个复制功能,即在从第一个datagridview选择行时,将所选行复制到第二个datgrid。

然后,我创建了显示第二个datagridview中选择的行的文本框。

我现在要做的就是从文本框中获取值,在第一个datagridview中将其匹配,然后单击Delete按钮,然后从第一个datagridview中删除相应的行,并从数据库中删除相应的行。

我是C#的新手,因此将不胜感激。 在此处输入图片说明

源代码:

namespace panelApplication
{
    public partial class Form1 : Form
    {
        public Form1()
    {
        InitializeComponent();
    }
    private void copy_Click(object sender, EventArgs e)
    {
        // dataGridView2.Rows.Clear(); (code used if you want to delete previous selections)
        foreach (DataGridViewRow item in dataGridView1.Rows)
        {
            if (item.Selected == true)
            {
                int n = dataGridView2.Rows.Add();
                dataGridView2.Rows[n].Cells[0].Value = item.Cells[0].Value.ToString();
                dataGridView2.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
                dataGridView2.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
            }
        }
    }
    public void fetch_Click(object sender, EventArgs e)
    {
        this.productsTableAdapter.Fill(this.productsDataset.products);
        productsDataset dt = new productsDataset();
        foreach (DataRow item in dt.products.Rows)
        {
            int n = dataGridView1.Rows.Add();
            dataGridView1.Rows[n].Cells[1].Value = item["product_id"].ToString();
            dataGridView1.Rows[n].Cells[2].Value = item["product_name"].ToString();
            dataGridView1.Rows[n].Cells[3].Value = item["category_id"].ToString();
        }
    }
    private void delete_btn_Click(object sender, EventArgs e)
    {
        try
        {
            if (dataGridView1.Rows.Cell[0].Text == TextBox1.tex) // error on this line.
            {

            }
        }
        catch (System.Exception)
        {
            MessageBox.Show("Not able to Delete");
        }
    }
    private void dataGridView2_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow row = this.dataGridView2.Rows[e.RowIndex];
            product_idTxtbx.Text = row.Cells["product_id"].Value.ToString();
            proName_txtbx.Text = row.Cells["product_name"].Value.ToString();
            catID_txtbx.Text = row.Cells["category_id"].Value.ToString();
        }
    }
     private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow row = this.dataGridView2.Rows[e.RowIndex];
            product_idTxtbx.Text = row.Cells["product_id"].Value.ToString();
            proName_txtbx.Text = row.Cells["product_name"].Value.ToString();
            catID_txtbx.Text = row.Cells["category_id"].Value.ToString();
        }
    }
    private void delete2_Click(object sender, EventArgs e)
    {
   //     if (dataGridView1.Rows["row.index"].Cells["productidDG"].Value.ToString() == product_idTxtbx.Text)
        {

        }
    }
}

}

在删除按钮上,单击事件btn_OnDelete()找到gridview索引,然后按行索引找到所有id。 然后将每个gridview行ID与文本框ID进行比较,例如:

if(GridView1.Rows.Cell[0].Text == TextBox1.tex)
{
   // Delete Query here
}

与我共享完整的解决方案代码。 参见此处的演示链接: CRUD操作链接

您应该使用以下代码:

<asp:Button ID="delete" runat="server" CommandArgument='<%#Eval("Id") %>'  Text="Delete" />
    private void delete_btn_Click(object sender, EventArgs e)
        {
          //incase you need the row index 
        int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
        int Prod_Id= Convert.ToInt32(e.CommandArgument);
        //followed by your code 
            try
            {
                // Then Compare your Id here in this if condition
                // if (dataGridView1.Rows.Cell[0].Text == TextBox1.tex
                if (Prod_id == Convert.ToInt32(TextBox1.text)) // error on this line.
                {
                     // User Code Here
                }
            }
            catch (System.Exception)
            {
                MessageBox.Show("Not able to Delete");
            }
        }

请使您的代码如下

private void delete_btn_Click(object sender, EventArgs e)
    {
        try
        {
        foreach (GridViewRow row in dataGridView1.Rows) 
        {
          if (row.RowType == DataControlRowType.DataRow)
           {
           if (row.Cell[0].value == TextBox1.tex) 
            {
              //delete opration here for  TextBox1.tex/row.Cell[0].Text(Product_id)
              break;
            }
          }

        }

        }
        catch (System.Exception)
        {
            MessageBox.Show("Not able to Delete");
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM