2013-04-23 17 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

相關問題