C#datagridview右键单击选择编辑选项,数据应显示在文本框中

时间:2012-12-10 10:30:49

标签: c# datagridview contextmenustrip

这是我在C#中DataGrid的代码。在右键单击单元格时,应显示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");
}

1 个答案:

答案 0 :(得分:1)

尝试在鼠标事件上触发

 private void dgvReport_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
 {

     If (e.Button = MouseButtons.Right){

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

     }

}