Этот код делает то, что вы хотите:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BindingList<User> users = new BindingList<User>();
users.Add(new User(){Name = "Fred", Included = "False", Title="Mr"});
users.Add(new User(){Name = "Sue", Included = "False", Title="Dr"});
users.Add(new User(){Name = "Jack", Included = "False", Title="Mr"});
dataGridView1.DataSource = users;
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
if (hit.rowIndex >= 0)
{
dataGridView1.ClearSelection();
dataGridView1.Rows[hit.RowIndex].Selected = true;
contextMenuStrip1.Show(this.dataGridView1, new Point(e.X, e.Y));
}
}
}
private void includeToolStripMenuItem_Click_1(object sender, EventArgs e)
{
// Included was the name of the column to change in my example code,
// you could also use the index of the column if you know it.
dataGridView1.SelectedRows[0].Cells["Included"].Value = "Included";
}
}
public class User
{
public string Name { get; set; }
public string Title { get; set; }
public string Included { get; set; }
}
Я не мог придумать лучшего способа сообщить контекстному меню, какая строка была выбрана, чем фактически используя свойство выбранной строки в DataGridView - вы могли бытакже сохраните это в поле уровня класса, но я не думаю, что это так аккуратно.