如何使值自动添加到datagridview

时间:2017-06-18 17:02:25

标签: c# datagridview

我有两张桌子:订单表和书籍表。我在Orders表中输入了一些书籍详细信息(例如姓名,出版商,作者等),我希望在我开始输入书籍详细信息时自动将这些详细信息添加到Book表格中。

例如,如果我在Orders表中输入了详细信息:

姓名:哈利波特

作者:J.K.Roliing

年:2000

然后我在Books表格的datagridview中,在" Name"新行的一行"哈利波特",我希望程序通过在订单表中输入的值自动完成作者和年份。 我尝试使用CellEndEdit事件来实现这一点,但是当我在一个新行中写一个书名时,一个错误跳转,说:"位置x"中没有行。 这是我的代码:

private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        string headerText = dataGridView2.Columns[e.ColumnIndex].HeaderText;//header of the column of the cell edited

        if (headerText.Equals("Name"))//if a Name cell was edited
        {

            string BookName = ds.Tables["Books"].Rows[e.RowIndex]["Name"].ToString();//get the name typed
            DataRow Order = FindRowByValue(BookName, "BooksOrders", "Name");//get the order details of the typed book

            ds.Tables["Books"].Rows[e.RowIndex]["Author"] = Order["Author"];//updating the book details as written in the order.
            ds.Tables["Books"].Rows[e.RowIndex]["Publishing"] = Order["Publishing"];
            ds.Tables["Books"].Rows[e.RowIndex]["Supplier"] = Order["Supplier"];
        }

感谢所有回答的人

0 个答案:

没有答案