blah blah blah is here! blah blah » Close

up1down
link

Guys, I'm trying to add an event to a datagridview cell content click for when a user clicks a cell row it changes a value on that row.

Example my datagridview on colunm [2] has the word "start" i want the user to click the cell and the word changes to "stop" on colunm [2] is that possible?

last answered 2 months ago

1 answers

link

Something like this should work:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex == -1) return;
object obj = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if (obj != null && !DBNull.Value.Equals(obj))
{
string content = obj.ToString();
if (content.ToLower() == "start")
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "stop";
}
}
}

Hi vulpes - i tried the above it but it doesn't seem to change the value? would i some how need to include that the start/stop value is in: row.Cells[2]

vulpes
12813

Not unless you want to restrict this exercise to a particular column in which case you can simply return if e.ColumnIndex != 2 and use 2 for the column index in the rest of the code. I tried the code myself before posting it and it worked fine.Notice though that the contents of a cell need to have been committed (i.e. you need to have moved off the cell after entering 'start') before it will work. Also, if there's any possibility that 'start' could be preceded or followed by whitespace, I'd use content.ToLower().Trim() == "start".

Ah, yes that's working now, i used your edited idea as yes i wanted to focus it only on one column, it works great now though - thanks again vulpes.

Feedback