簡體   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