简体   繁体   中英

Delete a row in DataGridView c# using textbox value

I have created a datagridview that gets all the data from MySQL Workbench database. Below this datagridview, I have created another datagrid with the same columns. For that, I have created a copy function, that on selecting the rows form the first datagridview, copies the selected rows to the second datgrid.

I have then created textboxes that displays the rows that are selected in the second datagridview.

All I want to do now is, get the values from the textboxes, match it in first datagridview, and after clicking on delete button, delete the respected row from first datagridview and respectievely from the database.

I am new to c#, so any help will be appreciated. 在此处输入图片说明

Source Code:

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)
        {

        }
    }
}

}

On delete button click event btn_OnDelete() find the gridview index then find the all id's by row index. Then compare each gridview row id to textbox item id like:

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

for full solution share code with me. see here demo link: CRUD Operation link

You should use this code:

<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");
            }
        }

please make your code like below

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");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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