删除C#中的内容后更新DataGridView

时间:2016-12-14 13:52:13

标签: c# datagridview

所以我试图从DataGridView删除一些选择其中几行的行,但我不能这样做。我将用代码解释它。

if ((bool)dtrow.Cells[0].Value == true)
{
    con = new SqlConnection(cs.DBConn);
    con.Open();

    int RowsAffected = 0;
    string queryDelete = "DELETE FROM InfoFile WHERE FileName='" + dtrow.Cells[2].Value.ToString() + "'";
    cmd = new SqlCommand(queryDelete);
    cmd.Connection = con;

    RowsAffected = cmd.ExecuteNonQuery();

    if (RowsAffected > 0)
    {
        test();

    }
    else
    {
        MessageBox.Show("Records not found!", "Try again", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    if (con.State == ConnectionState.Open)
    {
        con.Close();
    }
}

teste();方法会在删除内容后更新DataGridView

public void test()
    {
        try
        {
            con = new SqlConnection(cs.DBConn);
            con.Open();

            SqlDataAdapter da = new SqlDataAdapter(@"SELECT SelectedOrNot, Status, FileName, FileType, FileSize, FilePath,
                                                Created, Modified, Access, PCName FROM InfoFile", con);
            DataTable dt = new DataTable();
            con = new SqlConnection(cs.DBConn);
            con.Open();

            SqlDataAdapter da = new SqlDataAdapter(@"SELECT SelectedOrNot, Status, NomeFicheiro, TipoFicheiro, TamanhoFicheiro, CaminhoFicheiro,
                                                Criado, Modificado, Acedido, NomePC FROM InfoFicheiro", con);
            DataTable dt = new DataTable();
            dataGridView1.DataSource = null;
            da.Fill(dt);
            foreach (DataRow row in dt.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = false;
                dataGridView1.Rows[n].Cells[1].Value = row[1];
                dataGridView1.Rows[n].Cells[2].Value = row[2].ToString();
                dataGridView1.Rows[n].Cells[3].Value = row[3].ToString();
                dataGridView1.Rows[n].Cells[4].Value = row[4].ToString();
                dataGridView1.Rows[n].Cells[5].Value = row[5].ToString();
                dataGridView1.Rows[n].Cells[6].Value = row[6].ToString();
                dataGridView1.Rows[n].Cells[7].Value = row[7].ToString();
                dataGridView1.Rows[n].Cells[8].Value = row[8].ToString();
                dataGridView1.Rows[n].Cells[9].Value = row[9].ToString();

                if (dataGridView1.Rows.Count < 0)
                {
                    int rowIndex = dataGridView1.Rows.Count - 1;
                    dataGridView1.Rows[rowIndex].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[0];
                }
                else if (dataGridView1.Rows.Count > 0)
                {
                    int rowIndex = dataGridView1.Rows.Count - 1;
                    dataGridView1.Rows[rowIndex].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[0];
                }

                foreach (DataGridViewRow drow in dataGridView1.Rows)
                {
                    drow.Height = 18;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error\nDetalhes: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

如果我评论test();方法,它将删除所选行。如果我使用teste();方法,它只删除一行,但会更新。

所以你知道我能解决它吗?

0 个答案:

没有答案