-3

I want to Display the value on the database on cellclick event in the textbox but i got this error CS0029 " cannot implicitly convert type string to decimal "

private void datagridview_CellClick(object sender, DataGridViewCellEventArgs e)
{
    MaterialExpenseItemTemplate s = new MaterialExpenseItemTemplate();

    if (e.RowIndex < 0)
    {
    }
    else
    {
        projectid.Text = datagridview.Rows[e.RowIndex].Cells["id"].Value.ToString();
        txtcash.Text = datagridview.Rows[e.RowIndex].Cells["Contractor"].Value.ToString();
        dateTimePicker3.Text = datagridview.Rows[e.RowIndex].Cells["Date"].Value.ToString();

        txtpayee.Text = datagridview.Rows[e.RowIndex].Cells["ID_No"].Value.ToString();
        cmbrequest.Text = datagridview.Rows[e.RowIndex].Cells["Name"].Value.ToString();
        txtparticulars.Text = datagridview.Rows[e.RowIndex].Cells["Nature_Of_Work"].Value.ToString();

        cmbpayment.Text = datagridview.Rows[e.RowIndex].Cells["TD"].Value.ToString();
        txtamount0.Text = datagridview.Rows[e.RowIndex].Cells["FD"].Value.ToString();

        s.item = datagridview.Rows[e.RowIndex].Cells["ITEM"].Value.ToString();
        s.unit = datagridview.Rows[e.RowIndex].Cells["UNIT"].Value.ToString();
        s.price = datagridview.Rows[e.RowIndex].Cells["PRICE"].Value.ToString();  // ERROR LINE STARTS HERE
        s.qty = datagridview.Rows[e.RowIndex].Cells["QTY"].Value.ToString();
        s.amount = datagridview.Rows[e.RowIndex].Cells["AMOUNT"].Value.ToString();

        cmbterms.Text = datagridview.Rows[e.RowIndex].Cells["id"].Value.ToString();
        txtcheck.Text = datagridview.Rows[e.RowIndex].Cells["Contractor"].Value.ToString();
        txtbank.Text = datagridview.Rows[e.RowIndex].Cells["id"].Value.ToString();
        txtsi.Text = datagridview.Rows[e.RowIndex].Cells["Contractor"].Value.ToString();

        txtreceive.Text = datagridview.Rows[e.RowIndex].Cells["Contractor"].Value.ToString();
        cmbstatus.Text = datagridview.Rows[e.RowIndex].Cells["id"].Value.ToString();
        txtpo.Text = datagridview.Rows[e.RowIndex].Cells["Contractor"].Value.ToString();
    }
}
John V
  • 1,286
  • 9
  • 15
  • You need to indicate which line has the error. – John V Mar 31 '22 at 06:13
  • [`Decimal.Parse()`](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.parse?view=net-6.0), [`Decimal.TryParse()`](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryparse?view=net-6.0) – Cid Mar 31 '22 at 06:13
  • can you help me on how to code it in the error line im confuse where to put it. – Imba Gaming Mar 31 '22 at 06:17
  • the compiler tells you which line of code the error happened on. – John V Mar 31 '22 at 06:20
  • You've gotten into a bad habit - stop trying to treat everything as a string. Either those `Value`s are already strings and the `ToString()` calls are superfluous or they are of a *more appropriate* data type. – Damien_The_Unbeliever Mar 31 '22 at 07:18

1 Answers1

0

This question is already answered here: How do I convert string to decimal.

The error is telling you what is wrong. A string and a decimal are not interchangeable. A string is an array of characters and a decimal is number. Find the line(s) causing the error and convert the string to a decimal. There are parsing functions in the decimal class, and there is a general purpose Convert class that can also do it.

https://learn.microsoft.com/en-us/dotnet/api/system.decimal.parse?view=net-6.0

string decimalString = "4.20";
decimal decimalFromString = Convert.ToDecimal(decimalString, 
                                              CultureInfo.InvariantCulture);

Also, as an element of style, you can clean up your code a lot by saving a reference to the Cells array, and then referencing it.

var cells = datagridview.Rows[e.RowIndex].Cells;

projectid.Text = cells["id"].Value.ToString();
txtcash.Text = cells["Contractor"].Value.ToString();
dateTimePicker3.Text = cells["Date"].Value.ToString();

John V
  • 1,286
  • 9
  • 15