Srihari
Srihari

Reputation: 2429

While getting a cell value in row am getting error Object reference not set to Instance of Object

Am trying to get focused row cell value and previous row cell value of focused row. I used this code

object eachprice = gridView1.GetRowCellValue(gridView1.FocusedRowHandle,gridView1.Columns["Price"]);

this code get the value of price in focused row

object eachprice = gridView1.GetRowCellValue(gridView1.FocusedRowHandle-1, gridView1.Columns["Price"]);

but this code didn't get the cell value of previous row. it show null value so i get error "Object reference not set to instance of Object"

this is my code

private void discountcol()
    {

        if ((getdisc == tempstr) && (rownumber >= 1))
        {
            int focusedcount = gridView1.FocusedRowHandle;

            t1.Text = "";
            t1.Text = getdisc;

            object eachprice = gridView1.GetRowCellValue(focusedcount-1, gridView1.Columns["Price"]);
            t2.Text = eachprice.ToString();

            if (!(t2.Text == string.Empty))
            {
                decimal pricedecimal = Convert.ToDecimal(t2.Text);
                object eachdiscount = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Price"]);
                decimal eachdisdecimal = Convert.ToDecimal(eachdiscount);
                decimal tempdiscountprice = -(pricedecimal * (eachdisdecimal / 100));  // -ve calculation
                t3.Text = tempdiscountprice.ToString("N2");

                gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Price"], t3.Text);

                if (getdisc == tempstr)
                {
                    PricQuan.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
                    PricQuan.UnboundExpression = " 1 * [Price]";

                    TaxinAmount.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
                    TaxinAmount.UnboundExpression = " ([Price] * ([TaxInPercentage] / 100.0)) * 1 ";
                }
                else
                {
                    //PricQuan.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
                    //PricQuan.UnboundExpression = " Round([Quantity] * [Price], 2)";

                    //TaxinAmount.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
                    //TaxinAmount.UnboundExpression = " ([Price] * ([TaxInPercentage] / 100.0)) * [Quantity] ";
                }

            }

        }
        else
        {

        }
    }

Upvotes: 0

Views: 1436

Answers (1)

Sebi
Sebi

Reputation: 3979

You should use the focusedrowchangedevent to get the previous rowhandle. How to get gridview cell value in RepositoryItemGridLookupEdit_ValueChanged Event and set in TextEdit?

In fact this line is working: object eachprice = gridView1.GetRowCellValue(focusedcount, gridView1.Columns["Price"]);

But why you dont use the magic of datasource ;) I will give you a small example:

Lets say we have a product which got a title and a price. So we need a class Product.

public class Product
{
  public string Title{get;set;}
  public decimal Price{get;set;}
}

Now we have a Productlist which should be shown in a grid. So we create a IList and bind it to the grid.

public partial class MyForm : XtraForm
{
  public MyForm()
  {
    InitializeComponent();
  }

  private List<Product> MyList()
  {
    List<Product> myList = new List<Product>();
    //Add all your Products
  }

  private void LoadGrid()
  {
     MyGrid.DataSource = MyList();
  }
}

Now the Gridview just show the Data from your List. Its only functionality is showing the data. All work with data you can handle with the datasource (your List). If you want to get the values of the focusedrow it works as follows:

private void MyGridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
   Product product = (Product)MyGridView.GetRow(e.FocusedRowHandle);
   //Now you have your complete Object again! You dont need to ask every value with        getrowcell value or sth.

   //For example
   MyTextEdit.Text = product.Price.toString();
}

Its just a small and ugly example to show how it works. This is more beautiful way to handle the work with Grid in my opinion. And it is much easier to handle.

Upvotes: 1

Related Questions