单击按钮后如何在数据库中添加数据后如何清除datagridview [英] How to clear datagridview after data has added in database on button click

查看:74
本文介绍了单击按钮后如何在数据库中添加数据后如何清除datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button2_Click(object sender, EventArgs e)
        {
            string prodname = null, batchno = null, type = null, md = null, ed = null;
            double pr = 0.0d, ta = 0.0d, mrp = 0.0d;
            int tqty = 0;
            SqlConnection conn = new SqlConnection();
            connection obj = new connection();
            conn.ConnectionString = obj.getconnstring();
            SqlDataReader rdr = null;
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                prodname = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
                batchno = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                type = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
                tqty = Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
                pr = Convert.ToDouble(dataGridView1.Rows[i].Cells[4].Value);
                ta = Convert.ToDouble(dataGridView1.Rows[i].Cells[5].Value);
                md = Convert.ToString(dataGridView1.Rows[i].Cells[6].Value);
                ed = Convert.ToString(dataGridView1.Rows[i].Cells[7].Value);
                mrp = Convert.ToDouble(dataGridView1.Rows[i].Cells[8].Value);
                
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("Insert into purchase_order values('" + vendorid + "','" + dateTimePicker1.Value + "','" + textBox7.Text + "','" + textBox8.Text + "','" + dateTimePicker3.Value + "','" + comboBox2.Text + "','" + prodname + "','" + batchno + "','" + type + "','" + tqty + "','" + pr + "','" + ta + "','" + md + "','" + ed + "','" + mrp + "')", conn);
                    SqlCommand cmd1 = new SqlCommand("Insert into Grand_total_vendor values('" + textBox7.Text + "','" + Convert.ToDouble(textBox13.Text) + "','" + Convert.ToDouble(textBox14.Text) + "','" + Convert.ToDouble(textBox12.Text) + "','" + Convert.ToDouble(textBox15.Text) + "','"+dateTimePicker2.Value+"')", conn);
                    SqlCommand cmd2 = new SqlCommand("Insert into stock values('" + batchno + "','" + prodname + "','" + type + "','" + md + "','" + ed + "','" + tqty + "')", conn);
                    cmd.ExecuteNonQuery();
                    cmd1.ExecuteNonQuery();
                    cmd2.ExecuteNonQuery();

                    
                }
                //catch (Exception e1) { }
                finally
                {
                    if (rdr != null)
                    {
                        rdr.Close();
                    }
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
                MessageBox.Show("Data updated Successfully", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
               
            }
        }

推荐答案

使用
Datagridview1.Rows.Clear();



清除选择使用



To Clear Selection Use

Datagridview1.ClearSelection();




试试这个...

Hi,

Try this...

dataGridView1.DataSource = null;
dataGridView1..DataBind();



如果您的asp页面定义了一个更新面板,并且被指定为



If your asp page has an update panel defined and was specified as

UpdateMode="Conditional


那么您需要更新面板...


then you need to update your panel...


尝试
if (this.dataGridView1.DataSource != null)
{
     this.dataGridView1.DataSource = null;
}
else
{
    this.dataGridView1.Rows.Clear();
}


这篇关于单击按钮后如何在数据库中添加数据后如何清除datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆