Datatable, how to limit values that are input directly on it, is there a “changevalue” event?

StackOverflow https://stackoverflow.com/questions/23574354

  •  19-07-2023
  •  | 
  •  

Question

I have a dataset which contains the following tables:

https://i.stack.imgur.com/fZisY.png

As you can see it's possible to edit it's values directly (and i prefer it that way), but sometimes i wanna limit its value, for example, if the user attempts to place a number 3, i want the program to display a message:

MessageBox.Show("This parameter can only use values between 0 and 1.";); and then proceed to set the value to 1 (which is closest to 3)

is this possible? What is the action that makes the program recognize the value the user has just input?

edit: it's pretty simple, you can use something like this

if (Convert.ToDecimal(dataGridView1.Rows[0].Cells["Columnname"].Value) > 5)
{ 
    MessageBox.Show("The value can't be above 5.");      
    dataGridView1.Rows[0].Cells["Columnname"].Value = "5";
}

where [0] is the row number (starts at zero), basically the dataGridView1.Rows[0].Cells["Columnname"].Value command allows you to control directly a row, and a column value

Was it helpful?

Solution

DataTable itself is in-memory representation of the data.
Whereas DataSet contains collection of DataTables.

Looking at the image provided it seems your DataTable is bind with DataGridView control.

Yes. You can track cell value changes by looking into following events of DataGridView:

  1. DatataGridView.CellValueChanged event
  2. DataGridView.CurrentCellDirtyStateChanged event
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top