如何使用添加按钮使用存储过程C#将数据添加到datagridview

时间:2015-10-12 04:17:28

标签: c# winforms stored-procedures datagridview

This is my PhonebookSystem UI.

这些是我的电话簿系统代码。

private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtLastName.Text = DGPhonebook.CurrentRow.Cells["LastName"].Value.ToString();
            txtFirstName.Text = DGPhonebook.CurrentRow.Cells["FirstName"].Value.ToString();
            txtMiddleName.Text = DGPhonebook.CurrentRow.Cells["MiddleName"].Value.ToString();
            txtAge.Text = DGPhonebook.CurrentRow.Cells["Age"].Value.ToString();
            txtEmailAddress.Text = DGPhonebook.CurrentRow.Cells["EmailAddress"].Value.ToString();
            txtContactNo.Text = DGPhonebook.CurrentRow.Cells["ContactNo"].Value.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Person p = new Person
            {
                LastName = txtLastName.Text,
                FirstName = txtFirstName.Text,
                Middlename = txtMiddleName.Text,
                Age = Convert.ToInt32(txtAge.Text),
                EmailAddress = txtEmailAddress.Text,
                ContactNo = txtContactNo.Text
            };

            clsPerson.InsertPerson(p);
        }

        private void DGPhonebook_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            this.DGPhonebook.Rows.Add("1", "XX");
        }

这些是我的clsPerson代码。

public static void InsertPerson(Person p)
        {
            PhonebookSQLDataContext db = new PhonebookSQLDataContext();
            db.usp_InsertPerson(p.LastName, p.FirstName, p.Middlename, p.Age, p.EmailAddress, p.ContactNo);
        }
        public static void UpdatePerson(Person p)
        {
            PhonebookSQLDataContext db = new PhonebookSQLDataContext();
            db.usp_UpdatePerson(p.Numbering, p.LastName, p.FirstName, p.Middlename, p.Age, p.EmailAddress, p.ContactNo);
        }
        public static void DeletePerson(Person p)
        {
            PhonebookSQLDataContext db = new PhonebookSQLDataContext();
            db.usp_DeletePerson(p.Numbering);
        }

        public static List<usp_RetrievePersonResult> RetrievePerson = new List<usp_RetrievePersonResult>();

            PhonebookSQLDataContext db = new PhonebookSQLDataContext();
        }

我在使用存储过程通过单击添加按钮向datagridview添加数据时遇到问题。请帮忙..谢谢;)

1 个答案:

答案 0 :(得分:1)

它可能会用于你。我只拿了名字。

public void btn_1_Click(object sender, EventArgs e)
{
    Person p = new Person
    {
        nbame = txt1.Text.ToString()
    };
    InsertPerson(p);
    Loadgrid();
}
public void InsertPerson(Person p)
{
    DataClassesDataContext db = new DataClassesDataContext();
    Persontbl per = new Persontbl();
    per.name = p.nbame;
    db.Persontbls.InsertOnSubmit(per);
    db.SubmitChanges();
}
public void Loadgrid()
{
    DataClassesDataContext db = new DataClassesDataContext();
    var result = from res in db.Persontbls select name;
    GRD1.DataSource = result;
    GRD1.DataBind();
}

表:

 create table Persontbl(name varchar(20 )  primary key)