1

这是我DataGrid在 C# 中的代码。右键单击单元格,ContextMenu应显示编辑选项。但是,我无法执行右键单击操作。而且在点击编辑菜单后,我应该能够在文本框中显示数据。例如像NotesID 应该显示在textbox1 中。

/*this is for datagrid cell click code*/
private void dataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
     ContextMenuStrip mnu = new ContextMenuStrip();
     ToolStripMenuItem mnuedit = new ToolStripMenuItem("Edit");
     mnuedit.Click += new EventHandler(editToolStripMenuItem_Click);
     mnu.Items.AddRange(new ToolStripItem[] { mnuedit });
     dataGrid1.ContextMenuStrip = mnu;
}

private void editToolStripMenuItem_Click(object sender, EventArgs e)
{

    if (dataGrid1.SelectedRows.Count == 0)
    {
        textnotes.Text = Convert.ToString(dataGrid1.SelectedRows[0].Cells[0].Value);
        textclient.Text = Convert.ToString(dataGrid1.SelectedRows[0].Cells[1].Value);
        textdatetime.Value = Convert.ToDateTime(dataGrid1.SelectedRows[0].Cells[2].Value);
        textcombobox.Text = Convert.ToString(dataGrid1.SelectedRows[0].Cells[3].Value);
    }
}

private void EditClient_Click(object sender, EventArgs e) 
{
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
}

private void SaveButton_Click(object sender, EventArgs e)
{
    Service edtservice = new Service();
                         edtservice.EditNotes(Convert.ToInt32(textNotesID.Text),textnotes.Text,textclient.Text,textdatetime.Text,textcombobox.Text);
    MessageBox.Show("Records Updated");
}
4

1 回答 1

1

尝试在鼠标事件上触发它

 private void dgvReport_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
 {

     If (e.Button = MouseButtons.Right){

         DataGridViewRow row = this.dgvReport.Rows[e.rowIndex];

     }

}
于 2012-12-10T10:41:25.560 回答