如何编辑单独的datagridview中显示的单个记录?

时间:2015-12-22 20:01:25

标签: c# sql-server database winforms datagridview

我在form1中有一个datagridview,当我双击该datagridview上的一条记录时,它会在该行的ID列中写入该数字并将其作为字符串发送给form2。在form2我有2个datagridview一个显示客户的基本信息表,第二个显示客户的详细信息从数据库表,所以我有2个不同的客户信息表,我在form2中显示它们但仅限于之前双击的单曲。现在在这种形式2我希望能够编辑我面前的信息。我得到了成功的消息,但数据库中没有任何变化。这是我的代码:

private void dataGridView1_CellMouseDoubleClick ( object sender , DataGridViewCellMouseEventArgs e )
            {
            var frm2 = new Form2 ( dataGridView1.CurrentRow.Cells [ 0 ].Value.ToString () );
            frm2.Show ();
            }

并且在form2中我有这个:

public partial class Form2 : Form
    {
    public Form2 (string s)
        {
        InitializeComponent ();
        MessageBox.Show (s);
            SqlConnection baglanti = new SqlConnection ( @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True;User Instance=True;" );
            DataTable customercontacttablo = new DataTable ();
            SqlDataAdapter customercontactadaptor = new SqlDataAdapter ( "select * from Customer_Contact where Cus_ID = '"+s+"'" , baglanti );
            customercontactadaptor.Fill ( customercontacttablo );
            customer_ContactDataGridView.DataSource = customercontacttablo;
            DataTable customertablo = new DataTable ();
            SqlDataAdapter customeradaptor = new SqlDataAdapter ( " SELECT * from Customer where ID = '"+s+"'",baglanti);
            customeradaptor.Fill ( customertablo );
            customerDataGridView.DataSource = customertablo;
        }

    private void Form2_Load ( object sender , EventArgs e )
        {
        // TODO: This line of code loads data into the 'database1DataSet1.Customer' table. You can move, or remove it, as needed.
        this.customerTableAdapter.Fill ( this.database1DataSet1.Customer );
        // TODO: This line of code loads data into the 'database1DataSet1.Customer_Contact' table. You can move, or remove it, as needed.
        this.customer_ContactTableAdapter.Fill ( this.database1DataSet1.Customer_Contact );

        }

    private void button1_Click ( object sender , EventArgs e )
        {
            this.Validate ();
            this.customerBindingSource.EndEdit ();
            this.customer_ContactBindingSource.EndEdit ();
            this.tableAdapterManager.UpdateAll ( this.database1DataSet1 );
            MessageBox.Show ( "Edit succesfull." );
        }
    }
}

0 个答案:

没有答案