如何手动将行添加到数据网格视图 [英] How to add row to datagrid view manually

查看:74
本文介绍了如何手动将行添加到数据网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示在C#Windows窗体上的datagridview.
当用户插入一些记录时,我想同时向dgv添加新行.
请帮助我...

I have a datagridview that is displayed on the C# windows form.
I want to add new row simultanously to the dgv when user insert some record .
Please help me .....

private void btnInsert_Click(object sender, EventArgs e)
        { 
            try
            {
            SqlConnection scon = new SqlConnection("Data Source=ASIC-02\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True");
            scon.Open();
            SqlCommand scom = new SqlCommand("rec",scon);
            scom.CommandType = CommandType.StoredProcedure;
            scom.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
            scom.Parameters.Add("@Reg", SqlDbType.VarChar).Value = txtReg.Text;
            scom.Parameters.Add("@Dep", SqlDbType.VarChar).Value = txtDep.Text;
            dataGridView1.Rows.Add(txtName.Text, txtReg.Text, txtDep.Text);
            scom.ExecuteNonQuery();
            MessageBox.Show("Information Successfully Inserted");
            scon.Close();
            this.Refresh();
            }
             catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }         
        }

推荐答案

为此,您需要将行添加到使用的数据集中,然后重新渲染网格.
For this, you need to add the row to the dataset used and then re-render the grid.


这篇关于如何手动将行添加到数据网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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