How do I get the value of an item in a datagridview (dgv) cell?
My project has a windows form with a datagridview. The data source for the dgv is the results of a query. The columns of the dgv displays information for items to be processed. The primary key from the database row is in a hidden column. The multiselect property of the dgv is set to false. The user selects a row in the dgv and clicks a button on the form to begin processing. I need to place the primary key value into a variable to use for SQL query parameters in other automations.
I tried using the dgv current row cells get item method, but the method returns a DataGridViewCell object.
I resolved my problem by extracting a proxy for the DataGridViewCell. The proxy exposes the properties for the cell object. However, now I get a runtime error that the link from the cell value property link to the integer variable cannot be created. There is not a variant type variable listed in the toolbox. There also are not any converters listed in the toolbox. I presume the error is because there is not an implicit conversion defined to convert the cell value object to int.
@DarrellE You are correct in that the value that comes out is a string. You can use the TryParse method of the Integer variable to convert a string to an integer.
I created a sample solution and started it as a discussion at the link below.
@ThomasSasnett Thank you! I was able to accomplish the task in a slightly different manner. I created a script to get the cell value using the current row object from the datagridview. When the button is clicked, I check the selected rows collection count property is greater than zero. The multiselect property is currently set to false, but that could change in the future. I selected the current row property from the datagridview and pass the DataGridViewRow object as the script parameter. The script simply uses the Convert.ToInt32(Object) overload to return the cell’s value as a 32-bit integer. I attached an image of the automation section where I get the cell value.