1

DataGridC# での私のコードは次のとおりです。セルを右クリックすると、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 に答える