简体   繁体   中英

Devexpress Gridview how get row value in decimal type cell?

I have a grid control and I wanna get the value of the selected row. I could get the value of varchar type cell in row but I'm having trouble in getting the value for cell in decimal type.

Here's my code for every row click:

private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
    {

        var productPrice = Convert.ToDecimal(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "product_price").ToString());

        MessageBox.Show(productPrice.ToString());
    }

For more info the product_price column is set to be in decimal type with length of (10,2) so the value its displaying is like 13,233.00

Thank you I hope you can help me to solve this problem

My first thought: Is there no focused row? Add this and see if it fixes the issue:

if (gridView1.SelectedRowsCount == 0)
    return;

That wasn't the issue? Okay, add this to your code and set a breakpoint:

object o = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "product_price");
Type t = o.GetType();

When you add a watch (or hover) over t , does it say System.Decimal or String?

If it's a decimal, you should be in good shape, and what I would suggest is to just change the code to the following:

var productPrice = Convert.ToDecimal(gridView1.GetRowCellValue(
    gridView1.FocusedRowHandle, "product_price"));

The only thing I omitted was the ToString() , so we can let the Convert function apply against the (raw) object. I don't know how or why, and it actually seems like a long shot, but maybe ToString() is formatting the output in accordance with the grid display.

I'm guessing that is not the case, and your original supposition is correct in that the data coming from MySQL is actually formatted as a string, or your C# code is reading it as a string. If this is the case, run the actual SQL and see what datatype is being rendered. Is it numeric? If so, is your C# reading it as numeric ( reader.GetDecimal(x) ) or, perhaps is there a reader.GetValue(x).ToString() that is ignoring datatypes?

In other words, is MySQL adding the formatting and sending a string, or is your C# changing a decimal to a string? Find out what's doing that and put a stop to it.

You can certainly unformat the string and then convert to a decimal, but that seems like a long, LONG path to your end goal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM