2013-04-23 46 views
1

我在C#中有一个DataGridView,它与我的Access数据库连接。我看到我的DataGridView上的所有书籍。我创建了一个鼠标点击事件处理程序来选择行比我能拯救该行,因为你看到下面:我有一个鼠标点击事件处理程序,它适用于我选择第一次但是..?

private void Mouse_Click(object sender, MouseEventArgs e) 
{ 
    try 
    { 
     temp = dataGridView1.SelectedRows[0].Index.ToSt… 

     //This one is selecting the row. To do that select panel>event> on MouseClick write name and double click: 

     MessageBox.Show("You have selected the row " + temp); 
    } 
    catch 
    { 
    } 
} 

它的工作原理,当我打开窗体。但是,在将所选行保存到另一个数据库后,鼠标单击事件处理程序停止工作。换句话说,保存第一个选定的行后,当我回来选择另一行并保存到数据库时,鼠标单击事件处理程序不起作用。消息说,行超出范围。

+0

的可能重复[鼠标点击事件处理无法正常工作(http://stackoverflow.com/questions/16172639/mouse-click-event-handler-does-not-work-properly) – 2013-04-23 17:40:55

回答

0

您没有提供太多与连接类型,事件处理程序相关的信息,更重要的是您保存/更新记录的方式,所以我只是在这里猜测:是不是不小心覆盖了网格的数据源?这是我对这种行为所能想象的唯一事情(“行超出范围” - 它也假定事件仍按预期工作,但函数中的代码无法再看到数据)。

+0

选择行后,我点击SaveButton将选中的行保存到另一个可用的数据库中,但是当我想从dataGridView1中选择不同的书籍时,MouseClick事件不起作用。以下是SaveButton的代码。 – 2013-04-23 14:19:17

+0

选择行后,我单击SaveButton将选定的行保存到可以工作的另一个数据库,但是当我想从我的dataGridView1中选择不同的书时,则MouseClick事件不起作用。以下是SaveButton的代码。我试图发布的代码,但它不让我我发布的代码的一部分在这里:private void SaveButton(object sender,EventArgs e) {InitializeComponent(); (@“Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ Users \ Desktop \ data.accdb; Persist Security Info = False;”); OleDbCommand command = new OleDbCommand(); – 2013-04-23 14:26:13

+0

OleDbCommand command = new OleDbCommand(); command.CommandType = CommandType.Text; command.CommandText =“INSERT INTO Copy(BookName,AuthorName,ISBN,Quantity,Price)Values('”+ bookNameVariable [Convert.ToInt16(temp)] +“','”+ authorName [Convert.ToUInt32(temp)] +“','”+ ISBN [Convert.ToInt16(temp)] +“','”+ Quantity [Convert.ToInt16(temp)] +“','”+ Price [Convert.ToInt16(temp)] + “)“; command.Connection = mycode; mycode.Open(); command.ExecuteNonQuery(); mycode.Close(); – 2013-04-23 14:29:39

相关问题