当我单击“添加”按钮到gridview时,以下代码添加了重复记录

时间:2018-09-22 09:27:26

标签: c#

当我单击“添加”按钮到网格视图时,以下代码将添加双重记录...

int drow;

private void btn_poyeeNew_Click(object sender, EventArgs e)
{
    drow = Dg.Rows.Add();
    Dg.Rows[drow].Cells[0].Value = txtPayee.Text;
}

public void showdatainGridview()
{
    try 
    {
        SqlConnection con = new SqlConnection(HrGlobal.DbCon);
        query = "select payee_name from Tbl_payee";
        SqlDataAdapter sda = new SqlDataAdapter(query, con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        drow = 0;
        if(dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                drow = Dg.Rows.Add();
                Dg.Rows[drow].Cells[0].Value = row["payee_name"].ToString();
            }
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void PayeeBook_Load(object sender, EventArgs e)
{
    showdatainGridview();
}

0 个答案:

没有答案