直接向Datagridview添加新行 [英] Directly Adding New Row to Datagridview

查看:101
本文介绍了直接向Datagridview添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解决方案,可以将新行直接添加到c#中的datagridview中,
但是在此解决方案中,当我按保存按钮时,新行将添加到数据库中.
当我单击其他行或
时,如何在没有任何额外按钮的情况下执行此操作 添加另一行自动添加我的行.

保存按钮将调用以下过程:

I have a Solution for adding new row directly to datagridview in c#,
but in this solution when i press save button,new row added to database.
How can i do this without any extra button and when i click other row or
add another row my row added automatically.

The Save Button Calls following procedure:

private void AddRowToTable()
       {        
           if (passGrid.Rows[TotRowCount].Cells[1].Value.ToString() != "")
           {
               UserName = passGrid.Rows[TotRowCount].Cells[1].FormattedValue.ToString();
           }
           else {UserName = null; }
           if (passGrid.Rows[TotRowCount].Cells[2].Value.ToString() != "")
           {
               Password = passGrid.Rows[TotRowCount].Cells[2].Value.ToString();
           }
           else { Password = null; }
           try
           {
               var dbCnn = ConnClass.ConDB();
               {
                   PassWord pas = new PassWord();
                   if (UserName != null || Password != null)
                   {
                       pas.UserName = UserName;
                       pas.PassWord1 = Password;
                       dbCnn.GetTable<PassWord>().InsertOnSubmit(pas);
                       dbCnn.SubmitChanges();
                       BindDataGridView();
                       InitRowCount = passGrid.RowCount;
                   }
               }
           }//end try
           catch (Exception ex)
           {
               throw ex;
           }

       }




在哪里可以编写保存按钮的代码?

在哪个事件或代码中?

请帮帮我.




Where i can write the code of save button?

in which event or code?

Please help me.

Thanks

推荐答案

我将对数据库进行更改,清除Grid并重新加载表.这样,您可以避免一直打开连接,这是修改表的简便快捷方式.
I would make the changes on the database, clear the Grid and reload the table. In this way you avoid having a connection open all the time, and is an easy and fast way of modifying a table.


这篇关于直接向Datagridview添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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